2015-10-07 91 views
1

我正在使用Excel來格式化.csv文件的輸出,該文件創建幾個「塊」數據,但都是相對於相同的時間戳。爲了使用這些數據,我必須格式化,使時間戳記在B列中,然後將數據重新定位到其右側的列中。我需要循環相同的代碼行次數(n),但我需要嵌套循環(s)爲每個循環迭代循環(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

你能告訴我們一個輸入數據的例子,以及對於那些想要的輸出嗎? –

+0

不能超過註釋char限制;-) – HumanoidTyphoon

+0

嗯,你可以隨時編輯你的問題 –

回答

1

從MSDN的有關For聲明庫退房this頁。

你可以這樣做:

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

讓我知道這需要進一步的解釋。