When an excel sheet opens, I want a vba code to open a specific word document doc1 only…
- User
When an excel sheet opens, I want a vba code to open a specific word document doc1 only if the values in the last row of column B and Column D are different. If the particular word document doc1 is left open the vba code should not attempt to open the already open document doc1 again. Only opening the word document doc1 if it is not or no longer open therefore only having one instance of the word document doc1.
After finishing and closing the word document, I will manually enter a date in column E of the last row entry. After entering the date a differnet word document doc2 should open. While the word document doc2 is open, if I click on different cell in column E it should not attempt to open the already open document doc2 again. Only opening the word document doc2 if it is not or no longer open therefore only having one instance of the word document doc2.
I also need a code that runs everytime I select or enter a value in a cell in Column E. I want the vba code to copy 10 values of 10 cells in the same row as the cell in column E I selected (B,C,D,E,F,G,H,I,J,L) and appended their values to column M in the following order M1 to M10. The values changing for every cell I select in column E and the values cleared when I also leave the sheet. The word document doc2 that is opened by activity on column E as mentioned above will capture data from colum M1 to M10.
All these codes will by about 50 excel sheet for the same purpose. Can the codes be called from modules?
- Assistant
- User
Can I use the workbook open event in a worksheet activate event so that it is the worksheet that opens the word document
- Assistant
Yes, you can use the
Worksheet_Activateevent instead of theWorkbook_Openevent to open the Word document associated with each worksheet. Here's an example implementation:- Create a new module and define the following variables:
Dim wordApp As Word.Application Dim doc1 As Word.Document- In the
Workbook_Openevent, initialize theWord.Applicationobject and opendoc1:
Private Sub Workbook_Open() Set wordApp = New Word.Application wordApp.Visible = True Set doc1 = wordApp.Documents.Open("path\to\doc1.docx") End Sub- In each worksheet module, add the following code to the
Worksheet_Activateevent:
- User
I do not want the workbook open event to open the word document. I want the document to only be opened by a worksheet and only when I have clicked on a cell in column E
- Assistant
- User
The code that runs every time I select or enter a value in a cell in Column E does not work. I want the vba code to copy 10 values of 10 cells in the same row as the cell in column E I selected (B,C,D,E,F,G,H,I,J,L) and appended their values to column M in the following order M1 to M10. The values changing for every cell I select in column E and the values cleared when I also leave the sheet. Here is the code: Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then Dim rowValues As Range Set rowValues = Range("B" & Target.Row & ":L" & Target.Row) If Not Intersect(Target, rowValues) Is Nothing Then Dim mValues As String Dim i As Long For i = 1 To 10 mValues = mValues & rowValues(1, i).Value & " " Next i Range("M" & Target.Row).Value = mValues End If End If
End Sub
- Assistant
The code you provided for copying the row data to column M when a cell in column E is changed looks correct. Here are a few things you can check to make sure it works:
-
Make sure that you have placed the code in the correct worksheet module. It should be in the worksheet module for the sheet where you want this functionality.
-
Make sure that macros are enabled in your Excel file. If macros are disabled, the code will not execute.
-
Make sure that you are changing the value of a cell in column E, and not any other column, when you test the code. The
Target.Column = 5condition is what restricts the code to cells in column E.
-
- User
still not working
- Assistant
I'm sorry to hear that. Here are a few more things you can check:
-
Make sure that you are changing the value of a cell in the same sheet where the code is running. If you change the value of a cell in a different sheet, the code in this sheet will not execute.
-
Make sure that you have not accidentally turned off events processing in Excel. This can happen if you run code that turns off events and does not turn them back on. You can check if events processing is on by going to the VBA Immediate Window and typing
Application.EnableEvents. If the result is , events processing is on. If the result is , type to turn events processing back on.
-
- User
it worked but it put he values of B,C, D,E,F,G,H,L into the same row starting at m. I want it to put the values into M1, M2,M3, M4 up to M10
- Assistant
To write the values to M1 to M10 instead of M in the same row, you can modify the loop in the
Worksheet_Changeevent as follows:Dim mValues As Variant mValues = Application.Transpose(rowValues) For i = 1 To 10 Range("M" & i).Value = mValues(i) Next iThis code uses the
Transposefunction to convert the range into a 2D array where each column represents one of the values. Then, it iterates over the first 10 values in the first row of themValuesarray, and writes them to cells M1 to M10.Make sure to remove or comment out the previous code that writes the concatenated string to a single cell in column M.
I hope this solves the issue! Let me know if you have any other questions.
- User
I am getting an error on if TaskRequest is nothing Then : Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then If colELastVal <> Target.Value Then On Error Resume Next Set TaskRequest = wordApp.Documents("TaskRequest.docm") On Error GoTo 0 If TaskRequest Is Nothing Then Set TaskRequest = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\TaskRequest.docm") End If colELastVal = Target.Value End If End If
End Sub
- Assistant
It seems like the
TaskRequestvariable is not defined in your code. Please add the following line to the beginning of your code module to define it:Dim TaskRequest As Word.DocumentThis will declare
TaskRequestas a variable that can hold a reference to a Word document. Make sure it is at the top of the module, before any other procedures or functions.Also, make sure that you have a reference to the Microsoft Word Object Library in your Excel project. To check this, go to the VBA Editor, click on Tools > References, and see if Microsoft Word is checked in the list.
Let me know if this helps, or if you have any other questions or issues.