简体   繁体   English

从Word调用VBA excel宏以粘贴基于Word中的下拉列表进行更新的表

[英]Call VBA excel macro from word to paste a table that updates based on drop-down list in word

Here's a good one. 这是一个很好的。 First I began to learn VBA TODAY so be gentle. 首先,我今天开始学习VBA,所以要保持柔和。 So I have this vba macro in EXCEL 2007 that automatically hides/shows rows in a table based on the user's choice from 3 drop-down menus. 因此,我在EXCEL 2007中有了这个vba宏,它可以根据用户从3个下拉菜单中的选择自动隐藏/显示表中的行。 FYI, the table automatically updates from a master table that contains all the possible choices by hiding the unwanted rows based on the user's choice. 仅供参考,该表会根据用户的选择隐藏不需要的行,从而从包含所有可能选择的主表中自动更新。

Now I want to bring this to another level. 现在,我想将其提升到另一个层次。 What I would want is to be able to call the macro from WORD 2007 so that the table is copied to the word document at a desired location. 我想要的是能够从WORD 2007调用宏,以便将该表复制到Word文档的所需位置。 In order to achieve that, the drop-down menus would need to be in the word document and the EXCEL macro would have to "read" the selected value from the word document to construct the table. 为了实现这一点,下拉菜单将需要在Word文档中,并且EXCEL宏将必须从Word文档中“读取”所选值以构造表。 In other words, the user would never see the EXCEL part and would obtain a table in his word document based on his choices from the drop-down menus. 换句话说,用户将永远不会看到EXCEL部分,并且将基于他在下拉菜单中的选择而在Word文档中获得一个表格。

I inserted the excel macro that builds up the table. 我插入了建立表格的excel宏。 The end result would be such that C1, C2 and C3, which are the variables that drive the IF statements, would be variables from the WORD drop-down menus 最终结果将是驱动IF语句的变量C1,C2和C3将成为WORD下拉菜单中的变量

  Sub Sequence()

    Application.ScreenUpdating = False

    ActiveSheet.Cells.EntireRow.Hidden = False

    If Range("C2").Value = 2 Then
        Rows("48:67").Hidden = True
    Else
        Rows("48:67").Hidden = False
    End If

    If Range("C3").Value = 2 Then
        Rows("6:7").Hidden = True
    Else
        Rows("6:7").Hidden = False
    End If

    If Range("C1").Value = 1 Then

        Rows("9:12").Hidden = True
        Rows("14:17").Hidden = True
        Rows("19:22").Hidden = True
        Rows("24:27").Hidden = True
        Rows("29:47").Hidden = True
        Rows("48:51").Hidden = True
        Rows("53:56").Hidden = True
        Rows("58:61").Hidden = True
        Rows("63:66").Hidden = True

        j = 1
        k = -2

        For i = 6 To 67
            If Rows(i).Hidden = False Then

                Range("A" & i) = j
                j = j + 1
                Range("B" & i) = k
                k = k + 1
            End If

        Next i

    End If

    If Range("C1").Value = 2 Then

        Rows("9").Hidden = True
        Rows("11").Hidden = True
        Rows("13").Hidden = True
        Rows("15").Hidden = True
        Rows("17").Hidden = True
        Rows("19").Hidden = True
        Rows("21").Hidden = True
        Rows("23").Hidden = True
        Rows("25").Hidden = True
        Rows("27").Hidden = True
        Rows("29:31").Hidden = True
        Rows("33:35").Hidden = True
        Rows("37:39").Hidden = True
        Rows("41:43").Hidden = True
        Rows("45:47").Hidden = True
        Rows("48:51").Hidden = True
        Rows("53:56").Hidden = True
        Rows("58:61").Hidden = True
        Rows("63:66").Hidden = True

        j = 1
        k = -2

        For i = 6 To 67
            If Rows(i).Hidden = False Then

                Range("A" & i) = j
                j = j + 1
                Range("B" & i) = k
                k = k + 1
            End If

        Next i

    End If

    If Range("C1").Value = 3 Then

        Rows("9").Hidden = True
        Rows("11").Hidden = True
        Rows("13").Hidden = True
        Rows("15").Hidden = True
        Rows("17").Hidden = True
        Rows("19").Hidden = True
        Rows("21").Hidden = True
        Rows("23").Hidden = True
        Rows("25").Hidden = True
        Rows("27").Hidden = True
        Rows("29").Hidden = True
        Rows("31").Hidden = True
        Rows("33").Hidden = True
        Rows("35").Hidden = True
        Rows("37").Hidden = True
        Rows("39").Hidden = True
        Rows("41").Hidden = True
        Rows("43").Hidden = True
        Rows("45").Hidden = True
        Rows("47").Hidden = True
        Rows("48:50").Hidden = True
        Rows("52:54").Hidden = True
        Rows("56:58").Hidden = True
        Rows("60:62").Hidden = True
        Rows("64:66").Hidden = True

        j = 1
        k = -2

        For i = 6 To 67
            If Rows(i).Hidden = False Then

                Range("A" & i) = j
                j = j + 1
                Range("B" & i) = k
                k = k + 1
            End If

        Next i

    End If

    If Range("C1").Value = 4 Then

        j = 1
        k = -2

        For i = 6 To 67
            If Rows(i).Hidden = False Then

                Range("A" & i) = j
                j = j + 1
                Range("B" & i) = k
                k = k + 1
            End If

        Next i

    End If

    Range("A5:D67").Copy

    Application.ScreenUpdating = True

   End Sub

Do you mean you want to call a macro in Excel from Word? 您是否要从Word调用Excel中的宏? It that's that case, you can do this in Word: 就是这种情况,您可以在Word中执行此操作:

Sub excel()
    Dim excel As Object
    Dim wb As Object
    Dim str As String
    Set excel = CreateObject("Excel.Application")
    Set wb = excel.workbooks.Open("D:\Profiles\scklam\Desktop\Book2.xls")
    'Run the macro you want
    excel.Run "test2"
    str = wb.sheets(1).Range("A1")
    Selection.TypeText (str)
    wb.Save
    wb.Close
    Set wb = Nothing
    Set excel = Nothing
End Sub

In this example, I do something in my excel and then copy the value of a cell back to Word. 在此示例中,我在Excel中执行了一些操作,然后将单元格的值复制回Word。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM