我已经全天选择了不同范围的单元格,并通过Vlookup
检查是否存在帐户关联。
如果存在,我的vlookup
在F和G列中表示“ true”;如果不是,我的vlookup
返回“ false”
但是,即使帐户关联匹配,vlookup
仍返回“ false”的可能原因是由于间距问题
因此,我在下面生成了代码,以检查特定的列F和G是否包含带有单词“ False”的inStr
。
如果检测到任何单词“ False”,则将调用某个修整子过程以消除选择中的间距。
再次执行“ False”字符串检测,以确保由于消除了间距问题,现在“ False”为“ True”。如果循环迭代后字符串“ False”仍然存在,则说明帐户关联错误。出现一个消息框,代码结束
请,仅当我在相邻的F和G列中检测到任何字符串“ False”时,才想修剪我的选择。例如,我的选择是A10:E14,我只想检测任何“ F10:G14中的“ False”字符串
Sub myCode()
Dim iRow As Range, cell As Range
Set iRow = Range("F2:G100") '<<<this should be only columns F and G with
'rows adjacent to my manual selection
For Each cell In iRow 'for each cell in F and G adjacent to my
'selection
If InStr(cell.Value, "FALSE") > 0 Then
Call Trim_Code
End If
Next cell
For Each cell In iRow
If InStr(cell.Value, "FALSE") > 0 Then
MsgBox ("Account Mis-association Found!")
End If
Next cell
End Sub
Sub Trim_Code()
Dim Rng As Range
Set Rng = Selection
For Each Cell In Rng
Cell.Value = Trim(Cell)
Next Cell
End Sub
如何将我的iRow设置为仅与选择有关的F和G的相邻行,以及如何清理此代码以更快地执行?
\\\
在PeterT的友好协助下解决了!
Sub Test()
Dim thisWS As Worksheet
Set thisWS = ActiveSheet
Dim Association As Boolean
Association = True
Dim firstRow As Long
Dim lastRow As Long
firstRow = Selection.Cells.Row
lastRow = firstRow + Selection.Cells.Rows.Count - 1
Dim accountChecks As Range
With thisWS
Set accountChecks = .Range(.Cells(firstRow, 6), .Cells(lastRow, 7))
End With
Dim account As Range
For Each account In accountChecks
If account = False Then
Call Trim_Code
End If
If account = False Then
MsgBox "Account Mis-association Found in row " & account.Row & "!"
Association = False
End If
Next account
If Association = False Then
Exit Sub
End If
'proceed to do some crazy code
结束子
Sub Trim_Code()
Dim Rng As Range
Set Rng = Selection
For Each cell In Rng
cell.Value = Trim(cell)
Next cell
结束子
答案 0 :(得分:2)
您必须调整确定相邻选择区域的第一行和最后一行的方式,并且必须明确定义如何定义要检查的范围。下面的代码示例可以帮助您...
Sub Test()
Dim thisWS As Worksheet
Set thisWS = ActiveSheet
Dim firstRow As Long
Dim lastRow As Long
firstRow = Selection.Cells.Row
lastRow = firstRow + Selection.Cells.Rows.Count - 1
Dim accountChecks As Range
With thisWS
Set accountChecks = .Range(.Cells(firstRow, "F"), .Cells(lastRow, "G"))
End With
Trim_Code accountChecks
Dim account As Range
For Each account In accountChecks
If account = False Then
MsgBox "Account Mis-association Found in row " & account.Row & "!"
End If
Next account
End Sub
Sub Trim_Code(ByRef theseCells As Range)
Dim cell As Range
For Each cell In theseCells
cell.Value = Trim(cell)
Next cell
End Sub