此代碼具有兩個for
環路,一個用於每個參與者(列),一個用於30個值中的每個塊。它複製第一個參與者的前30個值並將其粘貼到另一個工作表中。然後對30的下一個塊和30的第三個塊重複該操作。然後,外部循環移至下一個參與者。
在這個例子中,我有C
列B
,三個參與者和D
在工作表「Sheet2的」,並且數據值開始在行2.輸出被粘貼到「工作表Sheet」,在列開始F
Sub transpose()
Dim i As Integer
Dim j As Integer
Dim outCol As Integer
Dim outRow As Integer
Dim intStart As Integer
Dim intEnd As Integer
Dim wksData As Worksheet
Dim wksOut As Worksheet
Dim strParticipant As String
Dim strRange As String
Set wksData = Worksheets("Sheet2")
Set wksOut = Worksheets("Sheet3")
outRow = 2 'starting in row 2
For i = 2 To 5 'columns of participants'
strParticipant = wksData.Cells(1, i).Value
outCol = 6 'output begins in column 6 ("F")
For j = 1 To 3 'blocks of values per participant
intStart = (j - 1) * 30 + 2 'increment for every block of 30 (starting at row 2)
intEnd = intStart + 29
wksData.Range(Cells(intStart, i), Cells(intEnd, i)).Copy
wksOut.Cells(outRow, outCol).PasteSpecial Paste:=xlValues
outCol = outCol + 1
Next j
'The two lines below will output the participant's name - uncomment if required.
' strRange = "E" & outRow & ":E" & outRow + 29
' wksOut.Range(strRange) = strParticipant
outRow = outRow + 30 'change the output row
Next i
Set wksData = Nothing
Set wksOut = Nothing
End Sub
你怎麼知道哪個值對每個參與者屬於任何'V',A''或'D'?你有沒有嘗試過使用「數據透視表」 - 無需代碼? – tospig 2014-12-11 02:12:05
對於每個參與者,第一30倍垂直的值是V,在接下來的30是A,在接下來的30是D.這有可能打破一列到3個不同的列與透視表?我必須做這個過程數百次,所以它必須很容易自動化或循環。 – 2014-12-11 02:16:30
透視表** **可以讓你有,但是這將取決於你如何想用輸出(如果你有與標貼'VV配...,AA的列會更容易.. ,...)'。至於代碼,你是否寫過任何'VBA'? – tospig 2014-12-11 02:22:14