I have a macro enabled Word Document which has a table in it. the cells of the table…
- User
I have a macro enabled Word Document which has a table in it. the cells of the table capture values from the active excel sheet. in CellB1 of the word document, It captures the value of M1 from the excel sheet. I want to add to this value, additional text. the text will be in this format &"No." & J1 value of excel sheet & Todays date and time in the following format mmddyyhrmin. This is the code in the Word document that adds the value to CellB1; cellB1 = xlApp.ActiveWorkbook.ActiveSheet.Range("M1").Value
- Assistant
To add additional text to the value captured from the excel sheet in CellB1 of the Word document, you can modify the code like this:
Dim valueFromExcel As String Dim additionalText As String valueFromExcel = xlApp.ActiveWorkbook.ActiveSheet.Range("M1").Value additionalText = "&No." & xlApp.ActiveWorkbook.ActiveSheet.Range("J1").Value & Format(Now, "mmddyyhhnn") cellB1 = valueFromExcel & additionalTextThis code will concatenate the value from cell M1 with the additional text in the format you specified and assign it to the CellB1 in the Word document. Make sure to adjust the format of the date and time to fit your requirements.
- User
will it not work if I just changed the line to this: cellB1 = xlApp.ActiveWorkbook.ActiveSheet.Range("M1").Value & “No.” & xlApp.ActiveWorkbook.ActiveSheet.Range(“J1”).Value & Format(Now, “mmddyyhhnn”)
- Assistant
Yes, you are correct. You can simplify the code by combining the concatenation of values and text in a single line as you have suggested. Here is the revised code:
cellB1 = xlApp.ActiveWorkbook.ActiveSheet.Range("M1").Value & "No." & xlApp.ActiveWorkbook.ActiveSheet.Range("J1").Value & Format(Now, "mmddyyhhnn")This code will achieve the same result as before, by capturing the value from cell M1, appending additional text in the specified format (including the value from cell J1 and the current date and time), and assigning the combined string to CellB1 in the Word document.