VBA LOOPS
The two macros in this homework should be assigned to separate buttons and should be located on the same worksheet.
When you are done, upload your saved workbook below.
Be sure to write your code in a module in the workbook you will submit, not in your personal workbooks or sheets. To access the correct module, go to your “Visual Basic,” right-click “Microsoft Excel Objects” in the workbook you are submitting, and then click “Insert” and “Module.”
Problem 1: Write a Sub that prints out numbers 1 through 25 in an Excel worksheet using any type of Loop (e.g., Do Until, Do While, For).
Problem 2: Create a nested-loop program (you can use any type of loop you would like).
This nested loop should take the names input (below) and print out every possible combination of first and middle names. These input names will need to appear in your workbook somewhere. Then, output every possible combination of first and middle names somewhere in the worksheet. Your program should work correctly with more or less first names and middle names. Below is an example of input and output. To save space, only the names associated with the first two first names are shown in the example output.
Input
First names | Middle names |
Penelope | Jill |
Gwyneth | Diane |
Alice | Heidi |
Anna | Lilly |
Victoria |
Example Output
First names | Middle names |
Penelope | Jill |
Penelope | Diane |
Penelope | Heidi |
Penelope | Lilly |
Penelope | Victoria |
Gwyneth | Jill |
Gwyneth | Diane |
Gwyneth | Heidi |
Gwyneth | Lilly |
Gwyneth | Victoria |
etc. | etc. |
Problem 1:
Sub Button1_Click()
' Generating numbers from 1 to 25
' Using For loop
For i = 1 To 25
' Writing data to Cell
Cells(i, 1).Value = i
Next i
End Sub
Sample Run:
_________________________________________________________________________________________
Problem 2:
Sub Button2_Click()
' Prints the combination of First and Last names
' Declaring Variables
Dim first, last As String
Dim k As Integer
k = 3
' Looping over first names
For i = 3 To 6
'Fetching first name
first = Cells(i, 7).Value
' Looping over Last names
For j = 3 To 7
'Fetching last name
last = Cells(j, 8).Value
'Printing into another cells
Cells(k, 10).Value = first
Cells(k, 11).Value = last
k = k + 1
Next j
Next i
End Sub
Sample Run:
Get Answers For Free
Most questions answered within 1 hours.