查找不同工作簿中的匹配值并根据第三个值更改行的一部分

问题描述 投票:1回答:4

我在不同的工作簿中有两个工作表。每张纸只能有几行到几千行。他们从来没有相同数量的线。

在Capital工作表的E列中,我想找到包含ITS ####的任何和所有单元格,其中####是数字字符。识别出一个单元格后,我想转到该行的A列并识别该值。然后,我想在Trans工作表的第J列中找到我刚刚识别的值(列A),该列位于不同的工作簿中。如果找到匹配项,我希望将Trans工作簿中第I列的值更改为“已售商品/费用的成本”。

我已经在互联网上搜索了几个星期,并尝试过许多不同的解决方案来解决类似的问我相信如果有人能让我超过指定的线路,我可以弄明白。我一直在接受

运行时错误1004方法对象_worksheet的范围失败。

以下代码是我正在处理的代码,但我只是想要克服错误,所以它甚至没有尝试解决整个问题。

感谢您提供的任何帮助。

Sub ITSTRANSCOM()
'
' ITSTRANSCOM Macro
'
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Variant
Dim C As Variant
Dim Lrow As Variant
Dim Lastrow As Variant

Set ws1 = Worksheets("Capital")
Set ws2 = Worksheets("Trans")
Lrow = ws1.Cells(ws1.Rows.Count, "A:A").End(xlUp).Row
Lastrow = ws2.Cells(ws2.Rows.Count, "j:j").End(xlUp).Row

'Run-time error occurs on next row.
For Each i In ws1.Range("A:A", Lrow)
    For Each C In ws2.Range("J:J", Lastrow)
        If i.Cells.Value = C.Cells.Value Then
            If C.Cells.Value = "ITS####" Then
               i.Cells.Interior.ColorIndex = xlNone
            End If
        End If
        Next C
    Next i
End Sub
vba excel-vba excel
4个回答
0
投票

尝试这样的事情:

Dim I as Integer, C as Integer
Dim Tmp_Val as Variant
For I = 1 to LRow
    If Left(UCase(Ws1.Range(“E” & I).Value),3) = “ITS” then
        Tmp_Val = Ws1.Range(“A” & I).Value
        For C = 1 to LastRow
            If Ws2.Range(“J” & C).Value = Tmp_Val then
                Ws2.Range(“I” & C).Value = “Cost of Goods Sold/Expense”
                Exit For
            End if
        Next C
    End if
Next I

0
投票

您的解决方案看起来就像在那里...只是尝试更换线路:

If C.Cells.Value = "ITS####" Then

随着线:

If UCase(Left(C.Cells.Value,3)) = "ITS" Then

我认为这将允许您识别您想要的单元格,并且您似乎应该能够开发代码以在工作表之间移动这些值(基于您的其他代码)。


0
投票

尝试改变

For Each i In ws1.Range("A:A", Lrow)

For Each i In ws1.Range("A1", "A" & Lrow)

For Each C In ws2.Range("J:J", Lastrow)相同:

For Each C In ws2.Range("J1", "J" & Lastrow)


0
投票

看起来很简单也许我没有得到你的解释吗?

Dim c As Range, r As Range
For Each c In ws1.Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues).Cells
    If c.Value Like "ITS[0-9][0-9][0-9][0-9]" Then
        Set r = ws2.Range("J:J").Find(c.Value)
        If Not r Is Nothing Then
            r.Offset(0, -1) = "Cost of Goods Sold/Expense"
        Else
            Debug.Print c.Value, " not found"
        End If
    End If
Next c
Debug.Print "Done"
© www.soinside.com 2019 - 2024. All rights reserved.