复制一系列自定义彩色单元格
Copy a range of custom colored cells
我需要编写代码才能执行以下操作:
从一列中,select只有彩色单元格(例如黄色),然后将它们复制到列表底部已经填满值的另一列下
这是我到目前为止编写的代码,但是我在编写将彩色单元格复制到另一个单元格的部分时遇到了麻烦sheet:
copycolor Sub m()
Dim wk As Workbook
Dim sh As Worksheet
Dim rng As Range
Dim C As Range
Set wk = ThisWorkbook
With wk
Set sh = .Worksheets("Base Dati Old")
End With
With sh
Set rng = .Range("A:A")
For Each C In rng
If C.Interior.ColorIndex = 46 Then
C.Copy
End If
Next C
End With
End Sub
假设您的数据中有 headers,我建议您做两件事:
- 不要循环 A 列中的所有单元格,这会显着降低速度。
- 如果 headers 存在,应用基于颜色的滤镜可能是更理想的方式。
例如:
Sub CopyColor()
Dim wk As Workbook: Set wk = ThisWorkbook
Dim sht As Worksheet: Set sht = wk.Worksheets("Base Dati Old")
Dim lr As Long, rng As Range
'Define last used row;
lr = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'Set range;
Set rng = sht.Range("A1:A" & lr)
'Filter your data on yellow;
rng.AutoFilter 1, RGB(255, 255, 0), xlFilterCellColor
'Copy filtered cells;
rng.SpecialCells(12).Offset.Copy wk.Worksheets("DestinationSheet").Range("A1")
'Turn off filter
rng.AutoFilter
End Sub
不要忘记更改要将数据复制到的 sheet 的名称。您可能还需要找到该 sheet 最后使用的行并使该部分动态化。
祝你好运。
我需要编写代码才能执行以下操作:
从一列中,select只有彩色单元格(例如黄色),然后将它们复制到列表底部已经填满值的另一列下
这是我到目前为止编写的代码,但是我在编写将彩色单元格复制到另一个单元格的部分时遇到了麻烦sheet:
copycolor Sub m()
Dim wk As Workbook
Dim sh As Worksheet
Dim rng As Range
Dim C As Range
Set wk = ThisWorkbook
With wk
Set sh = .Worksheets("Base Dati Old")
End With
With sh
Set rng = .Range("A:A")
For Each C In rng
If C.Interior.ColorIndex = 46 Then
C.Copy
End If
Next C
End With
End Sub
假设您的数据中有 headers,我建议您做两件事:
- 不要循环 A 列中的所有单元格,这会显着降低速度。
- 如果 headers 存在,应用基于颜色的滤镜可能是更理想的方式。
例如:
Sub CopyColor()
Dim wk As Workbook: Set wk = ThisWorkbook
Dim sht As Worksheet: Set sht = wk.Worksheets("Base Dati Old")
Dim lr As Long, rng As Range
'Define last used row;
lr = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'Set range;
Set rng = sht.Range("A1:A" & lr)
'Filter your data on yellow;
rng.AutoFilter 1, RGB(255, 255, 0), xlFilterCellColor
'Copy filtered cells;
rng.SpecialCells(12).Offset.Copy wk.Worksheets("DestinationSheet").Range("A1")
'Turn off filter
rng.AutoFilter
End Sub
不要忘记更改要将数据复制到的 sheet 的名称。您可能还需要找到该 sheet 最后使用的行并使该部分动态化。
祝你好运。