我的问题:
我想遍历一个范围,每当它找到一个彩色单元格时,它应该将左边的单元格复制到它右边的单元格。然后将其粘贴到另一个工作表中。
我的名为“Compare”的工作表比较了两组数据,而 FormatConditions 作为 xlUniqueValues 应用......这两组数据应该包含相同的数据,但有时会有一些不在另一个范围内。我有兴趣用我的循环查找这些单元格,然后在满足条件的情况下进行处理。
我的代码不会遍历单元格并向我返回此消息:
运行时错误“1004”:对象“WorksheetFunction”的方法“CountIfs”失败
“比较”表中部分数据的截图:
我的代码:
Sub LoopForCondFormatCells()
Dim sht3, sht4 As Worksheet
Dim ColB, ColG, ColBG c As Range
Set sht3 = Sheets("Compare")
Set sht4 = Sheets("Print ready")
Set ColG = sht3.Range("B3:B88")
Set ColB = sht3.Range("G3:G86")
HosKvik = sht4.Columns("B").Find("Hos Kvik, men ikke bogføring", Lookat:=xlWhole).Address(False, False, xlA1)
HosKvikOff = sht4.Range(HosKvik).Offset(1, 0).Address(False, False, xlA1)
Set HosKvikOffIns = sht4.Range(HosKvikOff).Offset(1, -1)
ColBG1 = ColB & "," ColG
Set ColBG = Range(ColBG1)
'In the following For Each strings, I would like it to look in the range ColBG, _
but it should only return the value it finds in ColB... _
But I don't know how to write the code to do so
For Each c In ColB.Cells
If Not IsEmpty(c) Then
n = Application.WorksheetFunction.CountIfs(ColBG, c) 'Error here
If n = 1 Then
c.Offset(0, -1).Resize(1, 3).Copy
HosKvikOffIns.PasteSpecial xlPasteAll
Set HosKvikOffIns = HosKvikOffIns.Offset(1, 0)
End If
End If
Next
目标:
我希望宏遍历单元格,并找到任何具有 FormatConditions 类型“xlUniqueValues”的单元格。每当遇到 FormatConditions 类型为“xlUniqueValues”的单元格时,它应该执行以下步骤:
For Each c In ColB.Cells
If Not IsEmpty(c) Then
n = Application.WorksheetFunction.CountIfs(ColBG, c) 'Error here
If n = 1 Then
c.Offset(0, -1).Resize(1, 3).Copy
HosKvikOffIns.PasteSpecial xlPasteAll
Set HosKvikOffIns = HosKvikOffIns.Offset(1, 0)
End If
End If
Next
我应该在“If c Is”行中写什么来让宏执行我想要它做的事情?我什至可以循环两个不同的范围,并且只返回 G 范围内的任何 xlUniqueValue 吗?
Countif 的范围不正确。
Sub LoopForCondFormatCells()
Dim sht3 As Worksheet, sht4 As Worksheet
Dim ColB As Range, ColG As Range, ColBG As Range, c As Range
Dim Wf As WorksheetFunction
Dim vR() As Variant
Dim k As Long, j As Integer
Set Wf = WorksheetFunction
Set sht3 = Sheets("Compare")
Set sht4 = Sheets("Print ready")
Set ColG = sht3.Range("B3:B88")
Set ColB = sht3.Range("G3:G86")
'ColBG1 = ColB & "," ColG
'Set ColBG = Union(ColG, ColB)
'In the following For Each strings, I would like it to look in the range ColBG, _
but it should only return the value it finds in ColB... _
But I don't know how to write the code to do so
For Each c In ColB.Cells
If Not IsEmpty(c) Then
With Wf
n = .CountIfs(ColG, c) 'Error here
If n = 0 Then
k = k + 1
ReDim Preserve vR(1 To 3, 1 To k)
For j = 1 To 3
vR(j, k) = c.Offset(0, j - 2)
Next j
End If
End With
End If
Next
sht4.Range("a1").Resize(k, 3) = Wf.Transpose(vR) '<~~The unique values are written below cell a1 in Sheet 4.
End Sub
上面使用数组更快,下面是如何复制范围。
Sub LoopForCondFormatCells()
Dim sht3 As Worksheet, sht4 As Worksheet
Dim ColB As Range, ColG As Range, ColBG As Range, c As Range
Dim Wf As WorksheetFunction
Dim vR() As Variant
Dim k As Long, j As Integer
Dim HosKvikOffIns As Range '<~~Declare a variable
Set Wf = WorksheetFunction
Set sht3 = Sheets("Compare")
Set sht4 = Sheets("Print ready")
Set ColG = sht3.Range("B3:B88")
Set ColB = sht3.Range("G3:G86")
Set HosKvikOffIns = sht4.Range("a1") '<~~ First, set the varialble
For Each c In ColB.Cells
If Not IsEmpty(c) Then
With Wf
n = .CountIfs(ColG, c) 'Error here
If n = 0 Then
c.Offset(0, -1).Resize(1, 3).Copy HosKvikOffIns
Set HosKvikOffIns = HosKvikOffIns.Offset(1, 0)
End If
End With
End If
Next
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句