Need help writing a VBA function that prints a loop

pab88

New Member
Joined
Oct 17, 2017
Messages
1
Hi everyone,
I am messing around with Excel and as an assignment I need to create a VBA function that prints my name 10 times in a loop. Then I need to create a button that will trigger the function. How can I do this?

I tried:

Module Hello
Sub Main()
MsgBox ("Hi, my name is Pat")
End Sub
End Module

But it did not work. I am not sure how to get the button to work either. If you could offer any help I would really appreciate it. thank you!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the board.

For the code, try:
Code:
Sub Main()

    Dim x   As Long
    
    Application.ScreenUpdating = False
    
    For x = 1 To 10
        Cells(x, 1).Value = "Hi, my name is Pat"
    Next x
    
    Application.ScreenUpdating = True

End Sub
You can search online on how to attach a macro to a button: http://tinyurl.com/y8426jmz
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,011
Members
449,204
Latest member
tungnmqn90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top