使用inputbox在vba中引用工作表

时间:2015-02-25 03:36:05

标签: excel vba excel-vba

我使用宏录制器获取此代码,但我需要运行此宏从不同的工作表中提取数据,我想要实现的是启动输入框以告诉公式在哪里查找数据,我的工作表名称是1,2,3,4等等,表示每个工作表对应一个月中的某一天。 在这个例子中,VLOOKUP公式引用了工作表5(' 5')你可以帮我找出替换5(在这种情况下)使用输入框设置的任何其他数字的代码吗? 我尝试过类似的东西:Dim myNum As String
        myNum = Application.InputBox("输入工作表编号",类型:= 1)     但无法让它发挥作用 这个示例代码:

   Sub GetMPRE_Data()
'
' GetMPRE_Data Macro
' Gets MPRE data to add to MLEA total
'
' Keyboard Shortcut: Ctrl+Shift+G
'
ActiveCell.FormulaR1C1 = "Staffed MPRE"
Range("K10").Select
ActiveCell.FormulaR1C1 = "Non-Prod MPRE"
Range("L10").Select
ActiveCell.FormulaR1C1 = "Staffed Time"
Range("M10").Select
ActiveCell.FormulaR1C1 = "Staffed Time Decimal"
Range("N10").Select
ActiveCell.FormulaR1C1 = "Non-Productive"
Range("O10").Select
ActiveCell.FormulaR1C1 = "Non-Productive Decimal"
Range("J11").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-9],'5'!R[-9]C[-9]:R[89]C[-5],2,FALSE)"
Range("K11").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],'5'!R[-9]C[-10]:R[90]C[-6],4,FALSE)"
Range("L11").Select
ActiveCell.FormulaR1C1 = "=RC[-7]+RC[-2]"
Range("L11").Select
Selection.NumberFormat = "[h]:mm:ss"
Range("M11").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*24"
Range("M11").Select
Selection.NumberFormat = "0.0"
Range("N11").Select
ActiveCell.FormulaR1C1 = "=RC[-8]+RC[-3]"
Range("N11").Select
Selection.NumberFormat = "[h]:mm:ss"
Range("O11").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*24"
Range("O11").Select
Selection.NumberFormat = "0.0"
Range("J11:O11").Select
Selection.AutoFill Destination:=Range("J11:O30"), Type:=xlFillDefault
Range("J11:O30").Select
Range("R28").Select
ActiveWindow.SmallScroll Down:=-12
Range("J11:J30").Select
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Range("J11").Select
End Sub

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我已删除了您的.Select语句,并将公式解析为xlA1等效项。你应该仔细证明这些,因为我在手指上向后计数字母(列)。

Sub GetMPRE_Data()
    ' GetMPRE_Data Macro
    ' Gets MPRE data to add to MLEA total
    ' Keyboard Shortcut: Ctrl+Shift+G
    Dim vWS As Variant
    vWS = InputBox("Select a worksheet", "Workshgeet Slector", Default:=ActiveSheet.Name)
    Debug.Print vWS
    If Len(vWS) Then
        With ActiveSheet
            .Range("J10").Resize(1, 6) = _
              Array("Staffed MPRE", "Non-Prod MPRE", "Staffed Time", "Staffed Time Decimal", "Non-Productive", "Non-Productive Decimal")
            .Range("J11").Formula = "=VLOOKUP(A11,'" & vWS & "'!$A$2:E100, 2, FALSE)"
            .Range("K11").Formula = "=VLOOKUP(A11,'" & vWS & "'!$A$2:E101, 4, FALSE)"
            .Range("L11").Formula = "=SUM(E11, J11)"
            .Range("L11").NumberFormat = "[h]:mm:ss"
            .Range("M11").Formula = "=L11*24"
            .Range("M11").NumberFormat = "0.0"
            .Range("N11").Formula = "=SUM(F11, K11)"
            .Range("N11").NumberFormat = "[h]:mm:ss"
            .Range("O11").Formula = "=M11*24"
            .Range("O11").NumberFormat = "0.0"
            With .Range("J11:O30")
                .FillDown
                With .Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End With
        End With
    End If
End Sub

VLOOKUP公式是使用输入框返回的变量从连接字符串构造的。如果您收到文件►打开对话框,则工作表名称不正确。如果要在工作表指定过程中应用某些错误控制,您应该能够编写一个验证提供的名称的短循环。