how to loop through rows columns in excel VBA Macro
Here is my sugestion:
Dim i As integer, j as integerWith Worksheets("TimeOut") i = 26 Do Until .Cells(8, i).Value = "" For j = 9 to 100 ' I do not know how many rows you will need it.' .Cells(j, i).Formula = "YourVolFormulaHere" .Cells(j, i + 1).Formula = "YourCapFormulaHere" Next j i = i + 2 Loop End With
Try this:
Create A Macro with the following thing inside:
Selection.CopyActiveCell.Offset(1, 0).SelectActiveSheet.PasteActiveCell.Offset(-1, 1).SelectSelection.CopyActiveCell.Offset(1, 0).SelectActiveSheet.PasteActiveCell.Offset(0, -1).Select
That particular macro will copy the current cell (place your cursor in the VOL cell you wish to copy) down one row and then copy the CAP cell also.
This is only a single loop so you can automate copying VOL and CAP of where your current active cell (where your cursor is) to down 1 row.
Just put it inside a For loop statement to do it x number of times.like:
For i = 1 to 100 'Do this 100 times Selection.Copy ActiveCell.Offset(1, 0).Select ActiveSheet.Paste ActiveCell.Offset(-1, 1).Select Selection.Copy ActiveCell.Offset(1, 0).Select ActiveSheet.Paste ActiveCell.Offset(0, -1).SelectNext i
This one is similar to @Wilhelm's solution. The loop automates based on a range created by evaluating the populated date column. This was slapped together based strictly on the conversation here and screenshots.
Please note: This assumes that the headers will always be on the same row (row 8). Changing the first row of data (moving the header up/down) will cause the range automation to break unless you edit the range block to take in the header row dynamically. Other assumptions include that VOL and CAPACITY formula column headers are named "Vol" and "Cap" respectively.
Sub Loop3()Dim dtCnt As LongDim rng As RangeDim frmlas() As StringApplication.ScreenUpdating = False'The following code block sets up the formula output rangedtCnt = Sheets("Loop").Range("A1048576").End(xlUp).Row 'lowest date column populatedendHead = Sheets("Loop").Range("XFD8").End(xlToLeft).Column 'right most header populatedSet rng = Sheets("Loop").Range(Cells(9, 2), Cells(dtCnt, endHead)) 'assigns range for automationReDim frmlas(1) 'array assigned to formula strings 'VOL column formulafrmlas(0) = "VOL FORMULA" 'CAPACITY column formulafrmlas(1) = "CAP FORMULA"For i = 1 To rng.Columns.countIf rng(0, i).Value = "Vol" Then 'checks for volume formula column For j = 1 To rng.Rows.count rng(j, i).Formula= frmlas(0) 'inserts volume formula Next jElseIf rng(0, i).Value = "Cap" Then 'checks for capacity formula column For j = 1 To rng.Rows.count rng(j, i).Formula = frmlas(1) 'inserts capacity formula Next jEnd IfNext iApplication.ScreenUpdating = TrueEnd Sub