0

我是 VBA 的新手,但我上瘾了!我创建了一个工作簿,以 2 周为单位跟踪加班,每个工作表有一个 2 周的时间段。我正在尝试调试的宏旨在将工作表中所做的任何更改传递到以下工作表。诀窍是一行中的数据可能在以下工作表中的另一行中,因此我尝试在宏中使用 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 代码行中的语法错误。发布的许多帮助都接近我正在寻找的内容,只是没有让我越过终点线。

任何帮助,将不胜感激!

4

2 回答 2

1

有点不清楚您要做什么,但我认为在字里行间阅读

  • 您想查找包含在名为的单元格中的值a吗?
  • 并将结果放在工作表索引上i

此外,还有很多改进代码的机会:请参阅下面的嵌入评论

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
于 2012-12-29T01:25:15.963 回答
1

我怀疑您可能希望将 vlookup 语句替换为类似

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

目前看起来你只是在对一些字符串 B5、B6、B7 等而不是这些单元格中的值进行 vlookup

于 2012-12-29T12:42:56.967 回答