我有以下代碼。我將逐步解釋這一點。在數組中轉換和重新排列範圍操作
6001 1001 3001
3001 1002 2001
2001 1003 3002
3002 1004 2002
2002 1005 3003
3003 1006 2003
該數據被佈置在單行中,以便於去除重複序列來如下的:
6001 1001 3001 1002 2001 1003 3002 1004 2002 1005 3003 1006 2003 1007 3004 1008 6002 2001 1009
此外,這種設置在以下格式:
6001 2003 1012 3006
1001 1007 2005 1018
3001 3004 1013 2002
1002 1008 3010 2005
2001 6002 1014 1019
1003 2001 2006 3008
3002 1009 1015 1020
1004 3005 3009 2006
2002 1010 1016
1005 2004 2003
3003 1011 2004
1006 3007 1017
請幫助我將此代碼轉換爲使用數組而不是將數據保存到不同工作表中的單元格。
Sub ARRANGE()
Dim InputRng As Range, OutRng As Range
Dim row As Integer
Dim rng As Range, j As Long
Dim lastRow As Long
Set InputRng = Sheet1.Range("A1:C20") 'A1 to C20 range is selected for operation
Set OutRng = Sheet2.Cells(1, 1) 'Cell A2 on another sheet
'---as indicated below data is converted to single row
Application.ScreenUpdating = False
xRows = InputRng.Rows.Count
xcols = InputRng.Columns.Count
For i = 1 To xRows
InputRng.Rows(i).Copy OutRng
Set OutRng = OutRng.Offset(0, xcols + 0)
Next
Application.ScreenUpdating = True
' duplicates comming one after other are deleted by below code
row = 0 ' Initialize variable.
For i = 1 To 3 * 20
If Sheet2.Cells(1, i).Value = Sheet2.Cells(1, i + 1).Value Then
Sheet2.Cells(1, i).Delete
End If
Next i
' data is rearranged to creat 12 number of rows and dynamic number of colums
j = 1
For i = 1 To Sheet2.Cells(1, Columns.Count).End(xlToLeft).Column Step 12
Set rng = Sheet2.Range(Sheet_Pipe_Config.Cells(1, i), Sheet2.Cells(1, i + 12))
Sheet3.Cells(1, j).Resize(rng.Count - 1, 1) = Application.Transpose(rng)
j = j + 1
Next i
End Sub
我建議你試一下,並與您遇到任何特定的問題回來。 –