使用VBA将带表引用的公式插入到单元格中

时间:2018-06-25 14:11:20

标签: excel vba excel-vba

我正在尝试将公式插入到单元格中,以便将MsgBox函数捕获的变量插入到公式中。

TmpPT = "=IF(NOT(ISBLANK(INDEX(" & "Table10[Recvd Date]" & ",MATCH("" * """ & prjNum & """ * "" ," & "Table10[Project Name]" & ",0)))),INDEX(" & "Table10[Recvd Date]" & ",MATCH("" * """ & prjNum & """ * ""," & "Table10[Project Name]" & ",0))," & "Not Yet Received" & ")"

下面我的代码的总体目标是取消隐藏其他工作表并将数据插入该工作表中的范围。如果不相关,请忽略此信息。

Private Sub CommandButton1_Click()
Dim SetPT As Variant
Dim TmpStg As Variant
Dim r As Integer
Dim c As Integer
Dim prjNum As Variant
Dim titlerng As Range
Dim outptrng As Range

r = 0
c = 0

SetPT = "A1"

Sheets("MG Rpt").Visible = True
Sheets("MG Rpt").Activate
ActiveSheet.Range(SetPT).Select

r = 8
ActiveCell.Offset(r, c).Activate
ActiveCell.Value = "Project #:"
ActiveCell.Offset(1, c).Activate
ActiveCell.Value = "Received Date:"
ActiveCell.Offset(1, c).Activate
ActiveCell.Value = "Elapsed Time:"
ActiveCell.Offset(1, c).Activate
ActiveCell.Value = "Expected Completion:"
ActiveCell.Offset(1, c).Activate
ActiveCell.Value = "Feedback:"

prjNum = InputBox("Please enter the Project # you are requesting down below.", "Project Search Query")
ActiveCell.Offset(-4, 1).Value = prjNum
ActiveCell.Offset(-4, 0).Activate
TmpStg = "=IF(NOT(ISBLANK(INDEX(" & "Table10[Recvd Date]" & ",MATCH("" * """ & prjNum & """ * "" ," & "Table10[Project Name]" & ",0)))),INDEX(" & "Table10[Recvd Date]" & ",MATCH("" * """ & prjNum & """ * ""," & "Table10[Project Name]" & ",0))," & "Not Yet Received" & ")"

Range("B10").Formula = TmpPT

End Sub

我是缺少对象还是无法格式化行?我正在学习VBA:)

公式应如下所示:

=IF(NOT(ISBLANK(INDEX(Table10[Recvd Date],MATCH("*"&prjNum&"*",Table10[Project Name],0)))),INDEX(Table10[Recvd Date],MATCH("*"&prjNum&"*",Table10[Project Name],0)),"Not Yet Received")

解决方案:

知道了!这是使其工作的最终输出:

TmpStg = "=IF(NOT(ISBLANK(INDEX(Table10[Recvd Date],MATCH(""*""&" & prjNum & "&""*"",Table10[Project Name],0)))),INDEX(Table10[Recvd Date],MATCH(""*""&" & prjNum & "&""*"",Table10[Project Name],0)),""Not Yet Received"")"

感谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

尝试使用Avoiding Select and Activate,同时将字符串连接最小化到绝对必要的程度。

Option Explicit

Private Sub CommandButton1_Click()
    Dim tmpStg As Variant, prjNum As Variant

    prjNum = InputBox("Please enter the Project # you are requesting down below.", "Project Search Query")
    tmpStg = "=IF(LEN(INDEX(Table10[Recvd Date], MATCH(""*" & prjNum & "*"", Table10[Project Name], 0))), " & _
                             "INDEX(Table10[Recvd Date], MATCH(""*" & prjNum & "*"", Table10[Project Name], 0)), ""Not Yet Received"")"

    Debug.Print tmpStg

    With Worksheets("MG Rpt")
        .Visible = True

        .Cells(9, "A").Resize(5, 1) = _
          Application.Transpose(Array("Project #:", "Received Date:", "Elapsed Time:", _
                                      "Expected Completion:", "Feedback:"))

        .Cells(9, "B") = prjNum
        .Cells(10, "B").Formula = tmpStg
        .Cells(10, "B").NumberFormat = "mm/dd/yyyy"

        .Activate
    End With

End Sub

您真的需要用通配符括住项目编号吗?