动态文件名和突出显示行

时间:2018-04-08 10:28:23

标签: excel vba excel-vba

在stackoverflow的帮助下,我设法完成了我想要完成的一部分,但是我有另外两部分...我试着升技但是我在这方面有多糟糕我可以&#39 ;弄清楚

这应该做的是从2个工作簿to_update_example_1它将在列H上搜索相同的名称与采购列表的列H进行比较,如果找到则将QTY列G(to_update_example_1)复制到列F(采购列表)代码做得很好。

我只是想知道是否更新失败,然后突出显示失败的行,并在列O或P上写下文本"找不到项目"

当我们从我们的在线下载我们的to_update列表时,文件名是动态的,虽然工作表名称是静态的,有没有办法避免进入编码端更改文件名,而是可以在宏运行时输入文件名或选择文件或其他东西......

提前致谢!

Option Explicit

Sub Macro1()
    Dim cell As Range, FindRng As Range, ErrorRng As Range

    Dim purchListSht As Worksheet
    Set purchListSht = Workbooks("Purchasing List.xls").Worksheets("Liltots") '(change "purchaseData" to your actual "purchase" sheet name)

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    With Workbooks("to_update_example_1").Sheets("GoodsSelInfo_LIST_SELL_INVENTOR") ' reference your "source" worksheet in "source" workbookworkbook (change "SourceData" to your actual "source" sheet name)
            For Each cell In .Range("H1", .Cells(.Rows.Count, 8).End(xlUp)).SpecialCells(xlCellTypeConstants) ' loop through referenced "source" sheet column "H" not empty cells
            Set FindRng = purchListSht.Columns("H").Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns) ' try finding current cell content in "purchase" sheet column "G"
            If Not FindRng Is Nothing Then '<-- make sure Find was successful finding vVal1
             FindRng.Offset(, -2).Value = cell.Offset(, -1).Value ' if successful, write the value of the cell one column left of the current cell to the cell two columns to the left of found cell
        Else ' raise some kind of notification
            MsgBox "Unable to find " & cell, vbInformation
        End If

        Next
    End With
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

提前感谢!

1 个答案:

答案 0 :(得分:0)

您需要在For Each cell循环中进行一些更改:

    For Each cell In .Range("H1", .Cells(.Rows.Count, 8).End(xlUp)).SpecialCells(xlCellTypeConstants) ' loop through referenced "source" sheet column "H" not empty cells
        Set FindRng = purchListSht.Columns("G").Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns) ' try finding current cell content in "purchase" sheet column "G"
        If FindRng Is Nothing Then ' if no match found
            cell.Offset(, 7).Value = "item not found" ' write in column "O" of current cell row
            Intersect(.UsedRange, cell.EntireRow).Interior.ColorIndex = 6 ' yellow highlight current cell row
        Else ' if match found
            FindRng.Offset(, -2).Value = cell.Offset(, -1).Value ' write the value of the cell one column left of the current cell to the cell two columns to the left of found cell
        End If
    Next