標題:
[發問]
如何依序取出有底色的資列列
[打印本頁]
作者:
luke
時間:
2013-6-3 22:49
標題:
如何依序取出有底色的資列列
各位大大
[Sheet1表]和[Sheet2表]的「A:D欄」各有數量不相等的資料列(約200列), 如何去抓取該兩表「B:D欄」欄中帶有紅底與黃底為順序的儲存格並複製至[Sheet3表]即先取出紅底的儲存格後再去取出黃底的儲存格(詳如[Sheet3表]所示).
例如:
[Sheet1表]的「B:D欄」有3個紅底資料列和4個黃底資料列要複製至[Sheet3表] 的「A:C欄」
[Sheet2表]的「B:D欄」有3個紅底資料列和6個黃底資料列要複製至[Sheet3表] 的「D:E欄」
VBA應如何寫出來
煩請先進 大大指導
[attach]15155[/attach]
作者:
Hsieh
時間:
2013-6-4 00:03
回復
1#
luke
Sub ex()
Dim Rng As Range, A As Range
k = 1
For Each sh In Sheets(Array("sheet1", "sheet2"))
With sh
For i = 1 To 2
Set Rng = Nothing
For Each A In .Range(.[A1], .Cells(.Rows.Count, 1).End(xlUp))
If A.Interior.ColorIndex = i * 3 Then
If Rng Is Nothing Then Set Rng = A.Offset(, 1).Resize(, 3) Else Set Rng = Union(Rng, A.Offset(, 1).Resize(, 3))
End If
Next
r = Application.CountA(sheet3.Columns(k)) + 1
If Not Rng Is Nothing Then Rng.Copy sheet3.Cells(r, k)
Next
End With
k = k + 3
Next
End Sub
複製代碼
作者:
luke
時間:
2013-6-4 18:42
回復
2#
Hsieh
謝謝超版答覆
若[Sheet3表]有合併的欄位如B:D欄(1欄)和G:I欄(1欄), 計算欄位時會產生1004錯誤.
[attach]15163[/attach]
即:
[Sheet1表]的「B:D欄」要複製至[Sheet3表] 的「A欄、B欄和E欄」
[Sheet2表]的「B:D欄」要複製至[Sheet3表] 的「F欄、G欄和J欄」
應如何修改語法?
[attach]15164[/attach]
作者:
Hsieh
時間:
2013-6-4 23:19
回復
3#
luke
Sub ex()
Dim Rng As Range, A As Range, Ar()
k = 1
For Each sh In Sheets(Array("sheet1", "sheet2"))
With sh
For i = 1 To 2
Set Rng = Nothing
For Each A In .Range(.[A1], .Cells(.Rows.Count, 1).End(xlUp))
If A.Interior.ColorIndex = i * 3 Then
ReDim Preserve Ar(s)
Ar(s) = Array(A, A.Offset(, 1), "", "", A.Offset(, 2))
s = s + 1
End If
Next
r = Application.CountA(sheet3.Columns(k)) + 1
If s > 0 Then
With sheet3.Cells(r, k).Resize(s, 5)
.Value = Application.Transpose(Application.Transpose(Ar)): Erase Ar: s = 0
.Interior.ColorIndex = i * 3
End With
End If
Next
End With
k = k + 5
Next
End Sub
複製代碼
作者:
luke
時間:
2013-6-5 05:11
回復
4#
Hsieh
感謝超版答覆
執行後發現[Sheet3表]的「A:J欄」所顯示的欄位有點出入,
請參見附檔【Sheet3結果】表所示
煩請指導修正語法
[attach]15168[/attach]
作者:
lilytracy
時間:
2013-6-5 06:56
回復
5#
luke
改 Ar(s) = Array(A.Offset(, 1), A.Offset(, 2), "", "", A.Offset(, 3))
複製代碼
供參考
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)