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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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