vba - Automatically add rows into a MS Word table in a loop as long as you read data from the excel sheet -


i'm vba newbie , having lots of errors , problems go on task. appreciated.

basically have textbox (textbox1) in word doc. have users type in. it'll number , text references folder excel files numbers name.

all excel files in folder has same formatting (created same template), number of rows in each file differs 1 another.

my boss wants me create vba code in word import data excel word doc, problem occurs varying number of rows in each file, users can select existing file in folder.

someone suggested creating rows in loop long there data pull excel, cannot code work. keeps failing i'm setting word table cell , excel cell equal each other (which don't know i'm doing right).

here have far:

private sub commandbutton1_click() dim tbl table dim row row  set tbl = activedocument.tables(3) set row = tbl.rows.add(beforerow:=tbl.rows(1))     tbl.rows(1).range.formattedtext = tbl.rows(2).range.formattedtext     '~~~> required above code inserts blank row in between     tbl.rows(2).delete  dim objexcel excel.application dim exwb excel.workbook on error resume next set exwb = objexcel.workbooks.open("s:\electro-protocol\mot_protocols\" & textbox1 & ".xls")  row.cells(0) = exwb.sheets("tabelle1“).cells(4,1) 'now need define loop in execute previous mentioned code. here's pseudo code got adapt in vba yourself:  counter = 0 excelsheetlength step 1         row.cells(1) = exwb.sheets("tabelle1").cells(counter, 0)        row.cells(2) = exwb.sheets("tabelle1").cells(counter, 1)        row.cells(3) = exwb.sheets("tabelle1").cells(counter, 2)  next counter  counter = 0 excelsheetlength step 1 if counter = 25 or counter = 26 or counter = 27     'nothing  else     set row = tbl.rows.add(beforerow:=tbl.rows(2))  end if  next counter end sub 

so basically, it's mess. i'm trying import data selection of excel worksheets same format, varying number of rows word table that'll dynamically add rows in loop long have data read in excel sheet.

the information want excel starts @ a4 , goes d4, want in word doc tables(3), starting @ row 1, column 1 column 4. excel template repeats on , on until finished data. there 5 repeated unnecessary rows after every 8 rows. not sure how loop around since i'm planning on ignoring/skipping 5 repeating rows of unnecessary info while importing data word.

i know sounds confusing, let me know if have questions. in advance!

you can add rows read excel. inside loop.

for counter = 0 excelsheetlength step 1      row.cells(1) = exwb.sheets("tabelle1").cells(counter, 0)     row.cells(2) = exwb.sheets("tabelle1").cells(counter, 1)     row.cells(3) = exwb.sheets("tabelle1").cells(counter, 2)      set row = tbl.rows.add(beforerow:=tbl.rows(2))     'set table column values here row.cells 1-3  next counter 

i've preferred style loop

dim icount integer dim ws excel.worksheet set ws = application.activesheet lrow = 4 while lrow <= ws.usedrange.rows.count      value1 = ws.range("a" & lrow).value     value2 = ws.range("b" & lrow).value      set row = tbl.rows.add(beforerow:=tbl.rows(2))     'set table column values here row.cells 1-3      'here deal extras     icount = icount  + 1     if icount = 8         icount = 0         lrow = lrow + 5     else         lrow = lrow + 1     end if      ws.range("a" & lrow).activate loop 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -