如何将某些单元格从Sheet2复制并粘贴到Sheet1,并让它们保持相同的属性,如彩色文本和背景颜色?我要做的是从两个工作表中找到F列中的相同序列号(工作表1是没有任何注释的新工作表,而工作表2是带注释的旧工作表),然后通过匹配它的程序集确定它是正确的项目在E列(因为有许多重复的序列号和不同的程序集编号),最后,将R,S,T中的注释从Worksheet2复制到Worksheet1。这似乎有效,但它只复制文本,而不是复制的某些单词和背景颜色。这就是我目前所拥有的。任何帮助将不胜感激。
Dim i As Integer
Dim ii As Integer
i = 2
ii = 2
For i = 2 To a
For ii = 2 To a
If Worksheets("Sheet1").Cells(i, 6) = Worksheets("Sheet2").Cells(ii, 6) Then
If Worksheets("Sheet1").Cells(i, 5) = Worksheets("Sheet2").Cells(ii, 5) Then
Worksheets("Sheet1").Cells(i, 18) = Worksheets("Sheet2").Cells(ii, 18)
Worksheets("Sheet1").Cells(i, 19) = Worksheets("Sheet2").Cells(ii, 19)
Worksheets("Sheet1").Cells(i, 20) = Worksheets("Sheet2").Cells(ii, 20)
Worksheets("Sheet1").Cells(i, 21) = Worksheets("Sheet2").Cells(ii, 21)
End If
End If
Next ii
Next i
答案 0 :(得分:0)
而不是四行代替使用这一行:
Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(ii, 18),Worksheets("Sheet2").Cells(ii, 21)).copy Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(i, 18),Worksheets("Sheet1").Cells(i, 21))
或者您可以将它们放在With
块中:
With Worksheets("Sheet2")
.Range(.Cells(ii,18),.cells(ii,21)).copy Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(i, 18),Worksheets("Sheet1").Cells(i, 21))
End With
或者让它更短;将工作表声明为变量:
Dim ows as worksheet
Dim tws as worksheet
Set ows = Worksheets("Sheet2")
Set tws = Worksheets("Sheet1")
With ows
.Range(.Cells(ii,18),.cells(ii,21)).copy tws.Range(tws.Cells(i, 18),tws.Cells(i, 21))
End With