Loop based on number of rows

L

Legacy 53668

Guest
I've created a macro that manipulates data in the first row of my shirt, including copying that data to another file. I want to run that macro again for every row in the worksheet. However, the number of rows can be 2 one day and 100 the next. How can I tell the macro to continuously run for the amount of data I have, which happens to change every day?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try using this.

Code:
dim MyEndRow as Integer
dim CheckRow as Integer
'Find End Row of Column A
MyEndRow = Sheets("Sheet1").Range("a65536").End(xlUp).Row
'Carry Out code for each row
For CheckRow = 1 to MyEndRow
'---Your Code For That Row HERE---
Next CheckRow

Change the Sheet1 to your sheet name.

HTH
 
Upvote 0
Sorry forgot to mention that in your code you should put:
Code:
Sheets("sheet1").range("a" & CheckRow) = "TEST"
This will put TEST in each cell from a1 to the end cell in the a column.
 
Upvote 0
DEMO of 3 methods of finding the bottom row used on a sheet.
Please make sure that your activesheet is blank , before running the demo. :eek:

Code:
Public Sub demo()
' place some values in sheet
For rw = 5 To 40 Step 2
    col = col + 1
    If col > 4 Then col = 1
    
    Cells(rw, col).Value = rw
Next rw

' NOW SHOW DIFFERENT TECHNIQUES FOR FIND BOTTOM

    'ONLY GIVE TRUE BOTTOM IF NO EMPTY ROWS AT TOP
    MsgBox ActiveSheet.UsedRange.Rows.Count
    
    ' Must specify which row to look in
    MsgBox Cells(65536, 1).End(xlUp).Row
    
   
   ' Will find bottom row of any column
    MsgBox Range("a1:iv65536").Find("*", SearchDirection:=xlPrevious)
    
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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