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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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