Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 比较前拆分单元格列值_Excel_Vba - Fatal编程技术网

Excel 比较前拆分单元格列值

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

我有两个电子表格,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_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