在macros中使用VLookup

我是新来的VBA,但我迷上了! 我已经创build了一个工作簿,用于跟踪每个工作表两个星期块的加class时间。 我正在尝试debugging的macros旨在将在工作表中进行的任何更改转换为以下工作表。 诀窍是,一行中的数据可能在以下工作表中的不同行中,所以我试图在macros中使用VLookup来保持它的准确性。

Sub CarryForward() Dim Answer As String Answer = MsgBox("This should only be used for a PERMANENT crew change." & vbNewLine & "If you are adding a new person to the list," & vbNewLine & "please use the Re-Sort function." & vbNewLine & "Do you want to continue?", vbExclamation + vbYesNo, "Caution!") If Answer = vbNo Then Exit Sub End If Application.ScreenUpdating = False Dim ActiveWorksheet As String ActiveWorksheet = ActiveSheet.Name For i = (ActiveSheet.Index + 1) To Sheets("DATA").Index - 1 For x = 5 To 25 Dim a As String Dim b As String a = "B" & x b = "C" & x ActiveSheet.Range(b).Value = Application.WorksheetFunction.VLookup(a, Sheets(ActiveWorksheet).Range("B5:C25"), 2, False) Next x Range("A3").Select Next i Sheets(ActiveWorksheet).Select Application.CutCopyMode = False Range("A3").Select Application.ScreenUpdating = True End Sub 

我很确定这只是VLookup代码行中的一个语法错误。 张贴了很多帮助接近我正在寻找,它只是没有让我在终点线。

任何帮助,将不胜感激!

这是有点不清楚你想做什么,但阅读之间的思路我认为

  • 你想查找包含在单元格中的值?
  • 并把结果放在图表索引i

此外,还有很多机会来改进您的代码:请参阅下面的embedded式注释

 Sub CarryForward() Dim Answer As VbMsgBoxResult ' <-- Correct Datatype Answer = MsgBox("This should only be used for a PERMANENT crew change." & vbNewLine & _ "If you are adding a new person to the list," & vbNewLine & _ "please use the Re-Sort function." & vbNewLine & _ "Do you want to continue?", _ vbExclamation + vbYesNo, "Caution!") If Answer = vbNo Then Exit Sub End If Application.ScreenUpdating = False ' Dim ActiveWorksheet As String <-- Don't need this 'ActiveWorksheet = ActiveSheet.Name <-- use object variables Dim wbActive As Workbook ' <-- don't select, use variables for sheet objects Dim shActive As Worksheet Set wbActive = ActiveWorkbook Set shActive = ActiveSheet 'Dim a As String ' <-- no point in putting these inside the loop in VBA. And don't need these anyway 'Dim b As String Dim SearchRange As Range Set SearchRange = shActive.Range("B5:C25") ' <-- Use variable to hold range Dim shDest As Worksheet Dim i As Long, x As Long '<-- dim all your variables For i = (shActive.Index + 1) To wbActive.Worksheets("DATA").Index - 1 ' <-- qualify references Set shDest = wbActive.Sheets(i) For x = 5 To 25 'a = "B" & x <-- no need to create cell names 'b = "C" & x ' I think you want to lookup the value contained in cell named by a? ' and put the result on sheet index i? ' Note: if value is not found, this will return N/A. Add an error handler wbActive.Sheets(i).Cells(x, 3).Value = Application.VLookup(shActive.Cells(x, 2).Value, SearchRange, 2, False) Next x 'Range("A3").Select Next i 'Sheets(ActiveWorksheet).Select ,-- don't need these 'Application.CutCopyMode = False 'Range("A3").Select Application.ScreenUpdating = True End Sub 

我怀疑你会想要replacevlookup语句是类​​似的

 Application.WorksheetFunction.VLookup(ActiveWorksheet.Range(a).value, ActiveWorksheet.Range("B5:C25"), 2, False) 

目前看起来你只是在对一些stringB5,B6,B7等进行查找,而不是在这些单元格中的值