Can I repeat macro attached to data validation button 1000 times and then stop?

ned lud

New Member
Joined
Jan 30, 2024
Messages
15
Office Version
  1. 2019
I have a macro attached to a data validation button.

The macro first generate some random results (based on previous inputs) and then copies those random results into a different worksheet on the next open line. Thanks to others who were kind enough to help me get to this point.

My code is below.

My problem is I need 1,000 examples so I need the macro attached to the button to run 1000 times and then stop.

Is there a way to do this or do I have to click the button 1000 times?

Here is the code I have already.

Thank you for any help you may be able to provide.

----------------------


Sub macro1()
'
' macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'
ActiveWindow.SmallScroll Down:=18
Range("A21:E73").Select
ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Add2 Key:=Range( _
"E22:E73"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Front Page").Sort
.SetRange Range("A21:E73")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-18
Range("E2").Select

Range("A17:af17").Copy
Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I can't test so pleas try this:

VBA Code:
Sub macro1()
' Keyboard Shortcut: Ctrl+a
  Dim X As Long
  
  For X = 1 To 1000
    ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Add2 Key:=Range( _
    "E22:E73"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Front Page").Sort
      .SetRange Range("A21:E73")
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
    
    Range("A17:AF17").Copy
    Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
  Next X

End Sub
 
Upvote 0
Solution
I can't test so pleas try this:

VBA Code:
Sub macro1()
' Keyboard Shortcut: Ctrl+a
  Dim X As Long
 
  For X = 1 To 1000
    ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Front Page").Sort.SortFields.Add2 Key:=Range( _
    "E22:E73"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Front Page").Sort
      .SetRange Range("A21:E73")
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
   
    Range("A17:AF17").Copy
    Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
  Next X

End Sub

You are a god. That worked perfectly. Took about 30 seconds to run - much shorter than I'd have thought.

Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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