相对循环迭代计数
Relative loop iteration counting
我正在使用 Excel 来格式化 .csv 文件的输出,该文件创建多个 'blocks' 数据,但都与相同的时间戳有关。为了使用这些数据,我必须设置格式,使时间戳位于 B 列,然后将数据重新定位到它右侧的列。
我需要将相同的代码行循环一定次数 (n),但我需要嵌套循环在每次循环迭代中循环 (n+1) 次。
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
查看 MSDN 库中关于 For
语句的 this 页。
你可以这样做:
For loop_ctr = 1 To n
'' Do stuff here, like nest another For Loop
Next
如果这需要进一步解释,请告诉我。
我正在使用 Excel 来格式化 .csv 文件的输出,该文件创建多个 'blocks' 数据,但都与相同的时间戳有关。为了使用这些数据,我必须设置格式,使时间戳位于 B 列,然后将数据重新定位到它右侧的列。
我需要将相同的代码行循环一定次数 (n),但我需要嵌套循环在每次循环迭代中循环 (n+1) 次。
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
查看 MSDN 库中关于 For
语句的 this 页。
你可以这样做:
For loop_ctr = 1 To n
'' Do stuff here, like nest another For Loop
Next
如果这需要进一步解释,请告诉我。