Click here to Skip to main content
11,931,104 members (64,172 online)
Rate this:
Please Sign up or sign in to vote.
See more: VBA
new to VBA and need help..Thanks
I have two word tables, one table contains part number and id,
and the other one contains id and other information,
how could use the id in table 2 to search the part number from table 1
and save the part number to table 2
at the end, I need to export the table as text file
Is this possible in VBA? Thanks..
Posted 5-Aug-12 19:27pm
losmac 17-Sep-12 16:09pm
More details, please... Example data...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Yes, it's possible in VBA, but we need more details to help you!

Example code would looks like:
Option Explicit
Sub MergeTables()
Dim srcDoc As Document, dstDoc As Document
Dim srcTbl1 As Table, srcTbl2 As Table, dstTbl As Table
Dim rng As Range, sTmp As String
Dim r As Long, rc As Long
'on error got error handler
On Error GoTo Err_MergeTables
'this document
Set srcDoc = ThisDocument
'table 1
Set srcTbl1 = srcDoc.Tables(1)
'table 2
Set srcTbl2 = srcDoc.Tables(2)
'add new document
Set dstDoc = Documents.Add
'add table in to new document
Set dstTbl = dstDoc.Tables.Add(dstDoc.Range, srcTbl1.Rows.Count, srcTbl1.Columns.Count)
'get row count
rc = srcTbl1.Rows.Count
'go through the collection of rows
'start from 2. row, ignore header ;)
For r = 2 To rc
    'get ID from column 1
    Set rng = srcTbl1.Cell(r, 1).Range
    dstTbl.Cell(r, 1).Range.Text = rng.Text
    'find ID in the 2. table and get some information
    sTmp = FindIDAndGetInfo(rng.Text)
    'insert info...
    dstTbl.Cell(r, 2).Range.Text = sTmp

    On Error Resume Next
    'clean up ;)
    Set rng = Nothing
    Set dstTbl = Nothing
    Set srcTbl2 = Nothing
    Set srcTbl1 = Nothing
    Exit Sub
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_MergeTables
End Sub

Function FindIDAndGetInfo(sID As String) As String
Dim sRetVal As String
'you need to implement the body of function
'tip: go through the collection of cells ;)

FindIDAndGetInfo = sRetVal
End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100