Excel 比较前拆分单元格列值
我有两个电子表格,vda.xlsx和main.xlsm。目前,我正在比较中的值: main.xlsm第J列 与 vda.xlsx A列 看看有没有匹配的。如果找到匹配项,则列中的值将以红色突出显示 但是,vda.xlsx列A中的数据格式已更改 以前是这样的 1234 现在看起来像这样 Test\1234或Best\1234或Jest\1234-它可以是任何东西 Sp I需要将Test\1234除以“\”并提取1234进行比较 你知道我怎样才能做到这一点吗。这是我目前的代码:Excel 比较前拆分单元格列值,excel,vba,Excel,Vba,我有两个电子表格,vda.xlsx和main.xlsm。目前,我正在比较中的值: main.xlsm第J列 与 vda.xlsx A列 看看有没有匹配的。如果找到匹配项,则列中的值将以红色突出显示 但是,vda.xlsx列A中的数据格式已更改 以前是这样的 1234 现在看起来像这样 Test\1234或Best\1234或Jest\1234-它可以是任何东西 Sp I需要将Test\1234除以“\”并提取1234进行比较 你知道我怎样才能做到这一点吗。这是我目前的代码: Sub VDA_Up
Sub VDA_Update()
Dim wshT As Worksheet
Dim wbk As Workbook
Dim wshS As Worksheet
Dim r As Long
Dim m As Long
Dim cel As Range
Application.ScreenUpdating = False
Set wshT = ThisWorkbook.Worksheets("Master")
On Error Resume Next
' Check whether vda.xlsx is already open
Set wbk = Workbooks("vda.xlsx")
On Error GoTo 0
If wbk Is Nothing Then
' If not, open it
Set wbk = Workbooks.Open("C:\Working\vda_test.xlsx")
End If
' Set worksheet on vda.xlsx
Set wshS = wbk.Worksheets("imac01")
m = wshT.Cells(wshT.Rows.Count, 1).End(xlUp).Row
' Loop though cells in column J on main.xlsm
For r = 1 To m
' Can we find the value in column C of vda.xlsx?
Set cel = wshS.Columns(1).Find(What:=wshT.Cells(r, 10).Value, _
LookAt:=xlWhole, MatchCase:=False)
If Not cel Is Nothing Then
' If we find a match, then change the text to red
wshT.Cells(r, 10).Font.ColorIndex = 3
End If
Next r
Application.ScreenUpdating = True
End Sub
使用Split(CellValue,“\”)
获取数组,然后检索数组中的最后一项
更改:
' Loop though cells in column J on main.xlsm
For r = 1 To m
' Can we find the value in column C of vda.xlsx?
Set cel = wshS.Columns(1).Find(What:=wshT.Cells(r, 10).Value, _
LookAt:=xlWhole, MatchCase:=False)
If Not cel Is Nothing Then
' If we find a match, then change the text to red
wshT.Cells(r, 10).Font.ColorIndex = 3
End If
Next r
例如:
' Loop though cells in column A on vda.xlsx
For r = 1 To m
' Can we find the value in column J of main.xlsm?
cellSplit = Split(wshS.Cells(r, 1).Value, "\")
Set cel = wshT.Columns(10).Find(cellSplit(UBound(cellSplit)), _
LookAt:=xlWhole, MatchCase:=False)
If Not cel Is Nothing Then
' If we find a match, then change the text to red
cel.Cells(1, 1).Font.ColorIndex = 3
End If
Next r
@Antoine LaurentLavoisier补充了一个例子,说明了如何做到这一点。可能包含一些错误,尽管我在
Set cel=wshT.Columns(10)中遇到运行时错误。查找(cellspit(UBound(cellspit)),查找:=xlother,MatchCase:=False)
请澄清,您的main
工作簿包含1234
,您的vda
工作簿包含类似test\1234
的数据,您需要确定1234
(来自main
wb)是否位于vda
工作簿的A
列中(格式为test\1234
)?我需要查看test\1234
中是否可以找到1234
(是的),通过将其拆分为一个数组并检索数组中的最后一项(如下面的答案所示),很清楚,但是哪个工作簿包含1234
,哪个test\1234
?main.xlsm包含1234
,uat.xlsx包含test\1234
,下面的代码似乎不起作用,我在Set cel=wshT.Columns(10).Find(cellspit(UBound(cellspit)),LookAt:=xlother,MatchCase:=False)处得到一个运行时错误9