【问题标题】:Excel Macro Vlookup between two workbooks两个工作簿之间的 Excel 宏 Vlookup
【发布时间】:2017-06-20 23:43:11
【问题描述】:

我已经启动了一个简单的宏来清理报告。我需要添加一个 vlookup,并且很难弄清楚该往哪个方向发展,是否应该使用 match 或 vlookup 表达式,以及如何解决动态且每周都在变化的文件名。

我更像是一个 SharePoint 工作流编写者,并且倾向于需要一个变量来存储(第二个打开的工作表的)文件名,以便稍后在 vlookup 中回忆,但我不确定这是否是正确的想法,或者确切的执行方式。由于我是初学者,您会看到我一行一行地写了这篇文章,其中包含许多 cmets,诗句必然是浓缩的步骤。

目标:

获取已经被用户打开的审计报告,运行宏清理文件,选择以前版本的文件(在对话框中由用户选择),并查看列,以原始格式文档返回结果,粘贴值vlookup,清理所有#n/a 值,查找后关闭源文件,保存目标文件。 Vlookup 信息位于代码中的 cmets 中应该写入的位置。

Sub AuditRptCleanup()

 

    'Verify Correct File Is Open

    If ActiveWorkbook.Name Like "*Audit*" Or ActiveWorkbook.Name Like "*AuditReport*" Or   

    ActiveWorkbook.Name Like "*audit*" Or ActiveWorkbook.Name Like "*auditreport*" Then

    On Error GoTo ErrorFileIncompatiable:

   

    'Verify Macro has not ran on Workbook Previously

    If Cells(1, 1).Value = "Product Number" And Cells(1, 2).Value = "Prod Type" Then

    MsgBox "Macro has already been used on this workbook"

    Exit Sub

    End If

           

    'Select Starting Cell

    Range("A1").Select

   

    'Unmerge all Cells in Worksheet

    ActiveSheet.Cells.UnMerge

       

    'Delete Columns A1 thru D1

    Range("$A$1:$D$1").EntireColumn.Delete

   

    'Delete Rows A1 thru A9

    Range("$A$1:$A$9").EntireRow.Delete

 

    'Cut and Paste Cells

    Range("$A$2").Cut Range("$A$1")

    Range("$G$1").Cut Range("$F$1")

    Range("$P$1").Cut Range("$O$1")

    Range("$AA$1").Cut Range("$Z$1")

 

    'Sort by Column A to Remove Extra Rows from View

    Columns("$A:$AM").Sort key1:=Range("$A:$A"), order1:=xlAscending, Header:=xlYes

 

    'Auto Fit Contents in Columns and Rows

    ActiveCell.Columns("$A:$AG").EntireColumn.Select

    ActiveCell.Columns("$A:$AG").EntireColumn.AutoFit

    ActiveSheet.Rows.EntireRow.AutoFit

   

    'Delete Empty Columns

    Range("$B:$B, $D:$D, $G:$I, $K:$L, $N:$N, $P:$Q, $T:$V, $X:$Y, $AA:$AB, $AD:$AF").EntireColumn.Delete

  

    'Remove Wrap Text from Cell B1

    Range("$B$1").WrapText = False

   

    'Autofit Contents of Columns

    Range("$A$1:$AF$1").Columns.AutoFit

   

    'Autofit Row A2 Contents

    Range("$A$2:$A$2").Rows.AutoFit

   

    'Delete Columns B and C

    Range("$B:$C").EntireColumn.Delete

   

    'Remove Wrap Text on L1 and M1

    Range("$L$1:$M$1").WrapText = False

 

    'Label Cell L1

    Range("$L$1").Value = "Qty from Previous Report"

   

    'Label Cell M1

    Range("$M$1").Value = "Change in Qty"

   

    'Label Cell N1

    Range("$N$1").Value = "Date New"

   

    'Label Cell O1

    Range("$O$1").Value = "Comments"

   

    'Label Cell P1

    Range("$P$1").Value = "Action"

   

    'Label Cell Q1

    Range("$Q$1").Value = "Status"

   

    'Label Cell R1

    Range("$R$1").Value = "Production Storage Quantity"

   

    'Autofit Contents of Columns

    Range("$L$1:$R$1").Columns.AutoFit

   

      

    'Open Previous Day Source file from User Selection   

    Dim val As String

    Dim intChoice As Integer

    Dim strPath1 As String

   

    'Alert User to open file

    MsgBox ("Please browse to the previous day Audit file you wish to use for the VlookUp")

   

    'Open File Dialog Box

    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False

  

    'Open File Dialog Box and prompt User to select single file

    intChoice = Application.FileDialog(msoFileDialogOpen).Show

 

    'Determine what file User selected

    If intChoice <> 0 Then

 

    'Get File Path selected by User

    strPath1 = Application.FileDialog( _

    msoFileDialogOpen).SelectedItems(1)

 

   'VLookup Column L thru R and Paste to Target File

   ‘This is where I need to take the file opened at the very beginning, and look up value A2, from the new    

   ‘file the user just opened, from Sheet 1! A:L, col index 12, FALSE, and paste into column L2 in the file

   ‘opened from the beginning, to the last row that has data (last row containing data changes each

   ‘time).

  ‘I need to then do the same for M-R, changing the col index to 13 for M, 14 for O, so forth and so on,    

  ‘and end each look up to the last row with data in the column.

  ‘I need to be able to IFERROR,0 in my steps to avoid #n/a

  ‘I then to need to copy and paste the values I just entered from row 2 – L:R, to last Row of data, and

  ‘paste value.

 

   'Save File As

    Application.GetSaveAsFilename

    End If

   

    'Notify User of Incompatible File

ErrorFileIncompatiable:

    MsgBox "This is not an Audit Report"

    Exit Sub

    End If

   

   

End Sub

 

Results destination file macro after running]1

【问题讨论】:

  • 您不需要 .Select 。对于第一部分,您可以使用 Option Compare Text(不区分大小写)和 If ActiveWorkbook.Name Like "*Audit*" 然后. Why do you need a vlookup? You just seem to copy and paste values into another workbook. For Lastrow use sht.Cells(sht.Rows.Count, "A").End(xlUp).Row(注意:必须将 sht 声明为您要使用的工作表)
  • 复制和粘贴步骤是独立的,发生在需要进行 vlookup 的第二张工作表打开之前。完成复制和粘贴格式化步骤后,我需要调用打开另一个文件,并完成 vlookup。我遇到的问题是知道哪个工作表处于活动状态并且能够分别调用源文件和目标文件,这就是为什么我认为我可能需要一个变量作为文件名的更改并且它从来没有一周又一周地被命名.
  • 如果您在屏幕截图之前和之后添加带有垃圾数据的截图会很好。
  • 我想我不明白。前后拍什么?上面所有步骤都是正确的,我只需要你在我的代码的注释部分看到的地方,了解,从文件对话框调用打开文件后,如何引用它(因为名称不会是静态的,并且在 Vlookup 中每周更改)。在运行宏之前,此宏在用户将打开的一个文件上运行。我也没有编程,同样,文件名每周都在变化。
  • 他们从打开的审计报告开始。运行宏。宏清除从另一个报告系统(不是基于 excel 或其他我可以访问的)导出的文件。宏执行您看到的复制、粘贴和删除步骤,以从原始版本格式化文件,因为它是一团糟。它清理格式并为 vlookup 创建占位符列。然后我让用户打开他们选择的第二个文件(源文件),用于需要执行的 vlookup,最终结果放在原始打开的文件中。如果我不知道名称,如何从用户选择的文件中启动 vlookup?

标签: vba excel excel-formula excel-2010


【解决方案1】:

在用户运行宏之前,他们会:

步骤 1. 打开当日审计报告 步骤 2. 运行宏(格式化) 步骤 3. 宏应提示用户从对话框中打开前一天或上次使用的审计文件 步骤 4. 在前一天的报告中查找当日报告的 A2,并返回 Q 列中的任何内容,然后将该值逐行粘贴到当日报告中,从 A2 开始,然后是 A3,然后是 A4,依此类推, 直到碰到一个空行(或最后一行有文本)。然后它应该再次查找 A2,但这一次,将源中的值 R 返回到目标文件的 M 行,依此类推,直到目标的 R 行已满。

【讨论】:

  • 我注释掉了错误,结果还是一样。好像它并没有真正打开我选择的文件。它通过动作,但我从未看到它打开。如果它真的是开放的,它肯定不会与它执行任何动作、vlookup、匹配或其他操作。我将模拟 2 个文件,并向您展示屏幕截图。只有这样我才能更好地解释
【解决方案2】:

我已经这样做了,现在它给了我很好的 ole'“编译错误”.. 如果没有 end if 则阻塞,并突出显示 end sub,我们宏中的最后一行。它执行 lRow=20 然后是下一行(因为所有其他人都被注释掉了)..

其他 MsgBox "这不是审计报告" 万一 结束子

这是它突出显示的结束子

【讨论】:

    【解决方案3】:

    然后转到 If intChoice 0 Then strPath1 = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

    并跳过所有语句/变量/Dim 但是.. 直接从 mso 语句转到 将 j 调暗为整数:j = 12

    然后跳过下一行并转到

    Set wb = Workbooks.Open(strPath1)
    Set sht2 = wb.Worksheets (1)
    lRow = 20
    End If
    End If
    Exit Sub
    

    【讨论】:

    • 这实际上跳过了我所有的格式化步骤,提示我打开文件,并说查找完成,并且文件保持在运行宏之前的原始状态。它没有,它直接跳到打开文件3次,让我打开它,然后直接按打开后,它说查找完成。哪个..它什么也没做。没有价值观。什么都没有。
    【解决方案4】:

    尝试从这个开始:

        Option Compare Text
    
    Sub AuditRptCleanup()
    Dim sht As Worksheet
    
        Set sht = ThisWorkbook.ActiveSheet
        'Verify Correct File Is Open
        If ActiveWorkbook.Name Like "*Audit*" Then
    
        'Verify Macro has not ran on Workbook Previously
    
        With sht
    
        If .Cells(1, 1).Value = "Product Number" And .Cells(1, 2).Value = "Prod Type" Then
    
        MsgBox "Macro has already been used on this workbook"
    
        Exit Sub
    
        End If
    
    
        'Unmerge all Cells in Worksheet
        .Cells.UnMerge
    
        'Delete Columns A1 thru D1
        .Range("$A$1:$D$1").EntireColumn.Delete
    
        'Delete Rows A1 thru A9
        .Range("$A$1:$A$9").EntireRow.Delete
    
    
        'Cut and Paste Cells
        .Range("$A$2").Cut .Range("$A$1")
        .Range("$G$1").Cut .Range("$F$1")
        .Range("$P$1").Cut .Range("$O$1")
        .Range("$AA$1").Cut .Range("$Z$1")
    
        'Sort by Column A to Remove Extra Rows from View
        .Columns("$A:$AM").Sort key1:=.Range("$A:$A"), order1:=xlAscending, Header:=xlYes
    
        'Auto Fit Contents in Columns and Rows
        .Columns("$A:$AG").EntireColumn.AutoFit
        .Rows.EntireRow.AutoFit
    
        'Delete Empty Columns
        .Range("$B:$B, $D:$D, $G:$I, $K:$L, $N:$N, $P:$Q, $T:$V, $X:$Y, $AA:$AB, $AD:$AF").EntireColumn.Delete
    
        'Remove Wrap Text from Cell B1
        .Range("$B$1").WrapText = False
    
        'Autofit Contents of Columns
        .Range("$A$1:$AF$1").Columns.AutoFit
    
        'Autofit Row A2 Contents
        .Range("$A$2:$A$2").Rows.AutoFit
    
        'Delete Columns B and C
        .Range("$B:$C").EntireColumn.Delete
    
        'Remove Wrap Text on L1 and M1
        .Range("$L$1:$M$1").WrapText = False
    
        'Label Cell L1
        .Range("$L$1").Value = "Qty from Previous Report"
    
        'Label Cell M1
        .Range("$M$1").Value = "Change in Qty"
    
        'Label Cell N1
        .Range("$N$1").Value = "Date New"
    
        'Label Cell O1
        .Range("$O$1").Value = "Comments"
    
        'Label Cell P1
        .Range("$P$1").Value = "Action"
    
        'Label Cell Q1
        .Range("$Q$1").Value = "Status"
    
        'Label Cell R1
        .Range("$R$1").Value = "Production Storage Quantity"
    
        'Autofit Contents of Columns
        .Range("$L$1:$R$1").Columns.AutoFit
    
        End With
        'Open Previous Day Source file from User Selection
        Dim val As String
        Dim intChoice As Integer
        Dim strPath1 As String
    
        'Alert User to open file
        MsgBox ("Please browse to the previous day Audit file you wish to use for the VlookUp")
    
        'Open File Dialog Box
        Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    
        'Open File Dialog Box and prompt User to select single file
        intChoice = Application.FileDialog(msoFileDialogOpen).Show
    
        'Determine what file User selected
        If intChoice <> 0 Then
    
        'Get File Path selected by User
        strPath1 = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    
        'VLookUp
        Dim xl As New Excel.Application
        Dim wb As Workbook
        Dim sht2 As Worksheet
        Dim lRow As Long
        Dim j As Integer: j = 12
        Dim rng As Range
    
        Set wb = xl.Workbooks.Open(strPath1)
        Set sht2 = wb.Worksheets(1) 'First Sheet in WB adjust if needed
    
        xl.Visible = True
    
        lRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
    
        On Error Resume Next
        For i = 2 To lRow
        If sht.Cells(i, 1).Value <> "" Then
            Set rng = sht2.Range("A:A").Find(sht.Cells(i, 1).Value)
            If Not rng Is Nothing Then
                sht.Cells(i, 12).Value = sht2.Cells(rng.Row, 12).Value
                sht.Cells(i, 13).Value = sht2.Cells(rng.Row, 13).Value
                sht.Cells(i, 14).Value = sht2.Cells(rng.Row, 14).Value
                sht.Cells(i, 15).Value = sht2.Cells(rng.Row, 15).Value
                sht.Cells(i, 16).Value = sht2.Cells(rng.Row, 16).Value
                sht.Cells(i, 17).Value = sht2.Cells(rng.Row, 17).Value
                sht.Cells(i, 18).Value = sht2.Cells(rng.Row, 18).Value
            Else
                MsgBox ("No Data Found in Source File for A" & i)
            End If
        End If
        Next
    
       'Save File As
        wb.Close
        xl.Quit
        MsgBox ("Look Up Done")
        End If
    
    Else
    MsgBox "This is not an Audit Report"
    End If
    End Sub
    

    正如我已经提到的,我将使用Option Compare Text 来检查正确的文件。除此之外,我删除了On Error Statement,因为它在那里没有多大意义。我包含了一个Else 声明。

    【讨论】:

    • 我想我不明白为什么需要第三个文件。我不能以某种方式从对话框的 open.file 输入中将文件名保存到变量中吗?
    • 我做到了,你也做到了。
    • 从目标文件中返回源文件中 Q 的值,并将该值放入 L 中。并用结果填充 L 列中的所有单元格。查找 A3,找到时返回 Q 中的值,A4 等。换句话说,如果我手动执行,我将拖动查找并将其复制到所有 L,然后选择 L:L 并仅粘贴值,删除公式。
    • 我看起来像:
    • 其实看起来一团糟
    【解决方案5】:

    创建一个新的工作簿。称它为 audit.xlsm。粘贴这段代码:

        Option Compare Text
    
    Sub AuditRptCleanup()
    Dim sht As Worksheet
    
        Set sht = ThisWorkbook.ActiveSheet
        'Verify Correct File Is Open
        If ActiveWorkbook.Name Like "*Audit*" Then
    
        'Verify Macro has not ran on Workbook Previously
    
        With sht
    
        If .Cells(1, 1).Value = "Product Number" And .Cells(1, 2).Value = "Prod Type" Then
    
        MsgBox "Macro has already been used on this workbook"
    
        Exit Sub
    
        End If
    
    
        'Unmerge all Cells in Worksheet
        .Cells.UnMerge
    
        'Delete Columns A1 thru D1
        .Range("$A$1:$D$1").EntireColumn.Delete
    
        'Delete Rows A1 thru A9
        .Range("$A$1:$A$9").EntireRow.Delete
    
    
        'Cut and Paste Cells
        .Range("$A$2").Cut .Range("$A$1")
        .Range("$G$1").Cut .Range("$F$1")
        .Range("$P$1").Cut .Range("$O$1")
        .Range("$AA$1").Cut .Range("$Z$1")
    
        'Sort by Column A to Remove Extra Rows from View
        .Columns("$A:$AM").Sort key1:=.Range("$A:$A"), order1:=xlAscending, Header:=xlYes
    
        'Auto Fit Contents in Columns and Rows
        .Columns("$A:$AG").EntireColumn.AutoFit
        .Rows.EntireRow.AutoFit
    
        'Delete Empty Columns
        .Range("$B:$B, $D:$D, $G:$I, $K:$L, $N:$N, $P:$Q, $T:$V, $X:$Y, $AA:$AB, $AD:$AF").EntireColumn.Delete
    
        'Remove Wrap Text from Cell B1
        .Range("$B$1").WrapText = False
    
        'Autofit Contents of Columns
        .Range("$A$1:$AF$1").Columns.AutoFit
    
        'Autofit Row A2 Contents
        .Range("$A$2:$A$2").Rows.AutoFit
    
        'Delete Columns B and C
        .Range("$B:$C").EntireColumn.Delete
    
        'Remove Wrap Text on L1 and M1
        .Range("$L$1:$M$1").WrapText = False
    
        'Label Cell L1
        .Range("$L$1").Value = "Qty from Previous Report"
    
        'Label Cell M1
        .Range("$M$1").Value = "Change in Qty"
    
        'Label Cell N1
        .Range("$N$1").Value = "Date New"
    
        'Label Cell O1
        .Range("$O$1").Value = "Comments"
    
        'Label Cell P1
        .Range("$P$1").Value = "Action"
    
        'Label Cell Q1
        .Range("$Q$1").Value = "Status"
    
        'Label Cell R1
        .Range("$R$1").Value = "Production Storage Quantity"
    
        'Autofit Contents of Columns
        .Range("$L$1:$R$1").Columns.AutoFit
    
        End With
        'Open Previous Day Source file from User Selection
        Dim val As String
        Dim intChoice As Integer
        Dim strPath1 As String
    
        'Alert User to open file
        MsgBox ("Please browse to the previous day Audit file you wish to use for the VlookUp")
    
        'Open File Dialog Box
        Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    
        'Open File Dialog Box and prompt User to select single file
        intChoice = Application.FileDialog(msoFileDialogOpen).Show
    
        'Determine what file User selected
        If intChoice <> 0 Then
    
        'Get File Path selected by User
        strPath1 = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    
        'VLookUp
        Dim xl As New Excel.Application
        Dim wb As Workbook
        Dim sht2 As Worksheet
        Dim lRow As Long
        Dim j As Integer: j = 12
        Dim rng As Range
    
        Set wb = xl.Workbooks.Open(strPath1)
        Set sht2 = wb.Worksheets(1) 'First Sheet in WB adjust if needed
    
        xl.Visible = True
    
        sht.Cells(2, 1).Value = "F"
        sht.Cells(3, 1).Value = "H"
        lRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
    
        On Error Resume Next
        For i = 2 To lRow
        If sht.Cells(i, 1).Value <> "" Then
            Set rng = sht2.Range("A:A").Find(sht.Cells(i, 1).Value)
            If Not rng Is Nothing Then
                sht.Cells(i, 12).Value = sht2.Cells(rng.Row, 12).Value
                sht.Cells(i, 13).Value = sht2.Cells(rng.Row, 13).Value
                sht.Cells(i, 14).Value = sht2.Cells(rng.Row, 14).Value
                sht.Cells(i, 15).Value = sht2.Cells(rng.Row, 15).Value
                sht.Cells(i, 16).Value = sht2.Cells(rng.Row, 16).Value
                sht.Cells(i, 17).Value = sht2.Cells(rng.Row, 17).Value
                sht.Cells(i, 18).Value = sht2.Cells(rng.Row, 18).Value
            Else
                MsgBox ("No Data Found in Source File for A" & i)
            End If
        End If
        Next
    
       'Save File As
        wb.Close
        xl.Quit
        MsgBox ("Look Up Done")
        End If
    
    Else
    MsgBox "This is not an Audit Report"
    End If
    End Sub
    

    然后创建另一个工作簿。称之为audit2.xlsx。让它看起来像这样:

    然后运行代码并选择 audit2.xlsx 作为要打开的工作簿。

    预期结果:

    【讨论】:

    • 好的。我就是这样做的,它产生了相同的最终结果。这个和另一个之间的唯一区别是我的宏没有存储在文件中,而是我从我的个人宏工作簿中调用它。我不能将它存储在每个文件中,我需要一个可以从中调用它的中心位置,以及有一种方法来“安装”它(也就是将它分配给用户工具栏,并放入他们的个人宏工作簿中)以便在何时调用我们想使用它。这是个问题吗?
    • 是的……就是这样。当我将宏放入实际的工作簿时,它可以工作。它告诉我 1000 次无法找到某些值,但它一直有效,直到 Q。它不做 R 列,我需要它扩展到 R。当我尝试从个人工作簿执行时,它不起作用。我需要将其写入个人工作簿中。从您的示例或从我的角度来看,源和目标中使用的文件审计和审计2 每天都在变化。我不能让用户需要将宏复制并粘贴到工作簿中。我需要能够通过复制和粘贴将其安装在他们的计算机上
    • 进入他们的个人宏工作簿。我需要它从这个角度工作。然后他们需要做的就是打开他们当前的审计报告,并运行宏来清理它,选择其他文件,它会vlookup,他们可以保存完成的最终产品
    • 好的。那么当它粘贴时,有没有办法让文本arial unicode 8 pt?它以不同的字体大小添加值。如果需要,我可以清理 uo 格式,但我想知道在查找期间是否有办法做到这一点。
    • 1drv.ms/x/s!Al30ey-vmWkFgTHMt4es2ABDz6t0 下载此文件,您可以将其添加为加载项。它将添加一个功能区以从每个工作簿启动它。这是一个如何添加它的教程:support.office.com/en-us/article/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多