I am new to VBA and macros.
I got the repeated task of copy data from Excel and paste it in a particular location in the word document.
For example, my excel sheet has the data like this:
Col1 | Col2 |
---|---|
ID_1 | I'm_One |
ID_2 | I'm_Two |
ID_3 | I'm_Three |
Now i'm looking for a Word macro
- Get text in Word table with cell position 3
- Find the same text in Excel Col1
- Get the value of Col2 from Excel
- Paste the value of Col2 in word table with cell position 10
- Repeat the same process for another table in Word document
[Update] I have tried with multiple code snippets by google search but unable to construct the working macro.
Sub pull_from_Excel2()
'ref: https://www.macworld.com/article/211753/excelwordvisualbasic.html
Dim Month As String
ID_Range = "A2:A6" 'Select this as range like "A2:A16"
Offset_to_fetch = 1 'Select this to fetch comments etc. value starts with
Set xlSheet = GetObject("D:\Excel.xlsx")
'Snippets:
'Debug.Print VarType(xlSheet.Worksheets("Sheet1").Range("A3:A5").Value)
'8204
Dim Cell As Range, rng As Range
Debug.Print VarType(xlSheet.Worksheets("Sheet1").Range(ID_Range).Value2)
Set rng = xlSheet.Worksheets(1).Range(ID_Range)
For Each Cell In rng
Debug.Print Cell.Text
Next Cell
End Sub
I used this url to construct my skeleton code: https://www.macworld.com/article/211753/excelwordvisualbasic.html
When i try to get the values from the range of cells in excel, i got the following error for the code.
Set rng = xlSheet.Worksheets(1).Range(ID_Range).Value2
The above line gives "Object required" error when running.
Set rng = xlSheet.Worksheets(1).Range(ID_Range)
The above line gives "Type Mismatch" error when running. Notes: For this error, I tried to use for each loop as this is array but the error is showing before executing the for loop.
Kindly assist.
cell position 3
, is it a single row/column table ? – CDP1802cell position 3
means the cell count in a table where all the cells will be counted as whole but not based on rows or columns. For example, if a table with 2 rows and 3 columns(R2XC3 Matrix), then the location ofcell position 3
is R1XC3. – Vinay