我正在使用Excel格式化.csv文件的输出,该文件会创建几个'块。数据但相对于相同的时间戳。为了使用这些数据,我必须格式化,使时间戳在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
答案 0 :(得分:1)
在MSDN的图书馆中查看有关For
声明的this页面。
您可以执行以下操作:
For loop_ctr = 1 To n
'' Do stuff here, like nest another For Loop
Next
如果需要进一步解释,请告诉我。