如何检测与我的选择相邻的范围内的字符串?
How do I detect a string within a range contiguous with my Selection?
我已经 select 全天不同范围的单元格并通过 Vlookup
检查帐户关联是否存在。
如果存在,我的 vlookup
在 F 和 G 列中显示 "true";如果不是我的 vlookup
returns "false"
但是,即使帐户关联匹配,vlookup
returns "false" 的一个可能原因是间距问题
所以,我生成了下面的代码,检查特定列 F 和 G 是否有 inStr
和单词 "False"。
如果检测到任何单词 "False",则会调用某个 trim 子过程来消除我的 selection 中的空格。
再次检测 "False" 字符串以确保 "False" 现在由于消除了间距问题而变为 "True"。如果在循环迭代后字符串 "False" 仍然存在,那么我们有一个帐户错误关联。后面是一个消息框,代码结束
拜托,只有当我在相邻的 F 和 G 列中检测到任何字符串 "False" 时,我才想 trim 我的 selection ....所以我的 selection,例如,是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 设置为仅与 selection 相邻的 F 和 G 行,以及如何清理此代码以更快地执行?
\\\
在 PeterT 的帮助下解决了!
子测试()
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
结束子
子Trim_Code()
Dim Rng As Range
Set Rng = Selection
For Each cell In Rng
cell.Value = Trim(cell)
Next cell
结束子
您必须调整确定相邻选择区域的第一行和最后一行的方式,并且必须明确定义要检查的范围的方式。下面的代码示例可以帮助您...
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
我已经 select 全天不同范围的单元格并通过 Vlookup
检查帐户关联是否存在。
如果存在,我的 vlookup
在 F 和 G 列中显示 "true";如果不是我的 vlookup
returns "false"
但是,即使帐户关联匹配,vlookup
returns "false" 的一个可能原因是间距问题
所以,我生成了下面的代码,检查特定列 F 和 G 是否有 inStr
和单词 "False"。
如果检测到任何单词 "False",则会调用某个 trim 子过程来消除我的 selection 中的空格。
再次检测 "False" 字符串以确保 "False" 现在由于消除了间距问题而变为 "True"。如果在循环迭代后字符串 "False" 仍然存在,那么我们有一个帐户错误关联。后面是一个消息框,代码结束
拜托,只有当我在相邻的 F 和 G 列中检测到任何字符串 "False" 时,我才想 trim 我的 selection ....所以我的 selection,例如,是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 设置为仅与 selection 相邻的 F 和 G 行,以及如何清理此代码以更快地执行?
\\\
在 PeterT 的帮助下解决了!
子测试()
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
结束子
子Trim_Code()
Dim Rng As Range
Set Rng = Selection
For Each cell In Rng
cell.Value = Trim(cell)
Next cell
结束子
您必须调整确定相邻选择区域的第一行和最后一行的方式,并且必须明确定义要检查的范围的方式。下面的代码示例可以帮助您...
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