Here's a good one. First I began to learn VBA TODAY so be gentle. 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. 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. 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. 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.
I inserted the excel macro that builds up the table. 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
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? It that's that case, you can do this in 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.