HumanoidTyphoon
HumanoidTyphoon

Reputation: 13

Relative loop iteration counting

I am using Excel to format the output of a .csv file that creates several 'blocks' of data but all relative to the same timestamp. In order to use this data, I have to format such that the timestamp is in column B and then data is relocated to the columns to the right of it.

I need to loop the same lines of code a set number of times (n), but I need the nested loop(s) to loop (n+1) times for each loop iteration.

Sub Format Data ()
Dim n as Integer

loop_ctr = 1
Do
' Create Header Data For Channel 1
' Find CH# and use offset commands to select and copy pertinent channel label and data information
'

' Set cursor to Cell A1
    Sheets("Raw Data Input").Select
    Range("A1:A1").Select

'Finds first instance of CH#, which will be used as an anchor cell for      copying and pasting relevant channel information into the formatted data spreadsheet.
    loop_ctr = 1
    Do
        Cells.Find(What:="CH#", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Activate

            loop_ctr = loop_ctr + 1
    Loop While loop_ctr < n

'Offset command to select cell containing Channel Name,set the variable type to string (to allow units
'and measurement type to be appended to the channel name for data table header.

    'Get Channel Name
    ActiveCell.Offset(1, 1).Select
    Selection.Copy

    Sheets("Formatted Data Output").Select
    Sheets("Formatted Data Output").Range("1:1").End(xlToRight).Offset(0, 1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Loop_ctr = loop_ctr + 1 = n
Loop While n <13
End Sub

Upvotes: 1

Views: 89

Answers (1)

tbm0115
tbm0115

Reputation: 419

Check out this page from MSDN's library about the For statement.

You could do something like:

For loop_ctr = 1 To n
   '' Do stuff here, like nest another For Loop
Next

Let me know if this needs further explanation.

Upvotes: 1

Related Questions