Excel VBA使用单词表 [英] Excel VBA using word table

查看:113
本文介绍了Excel VBA使用单词表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA的新手,需要帮助.谢谢
我有两个单词表,一个表包含零件号和ID,
另一个包含ID和其他信息,
如何使用表2中的ID从表1中搜索零件号
并将零件号保存到表2
最后,我需要将表导出为文本文件
在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..

推荐答案

是的,可以在VBA中使用,但是我们需要更多详细信息来帮助您!

示例代码如下:
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
Next


Exit_MergeTables:
    On Error Resume Next
    'clean up ;)
    Set rng = Nothing
    Set dstTbl = Nothing
    Set srcTbl2 = Nothing
    Set srcTbl1 = Nothing
    Exit Sub

Err_MergeTables:
    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


这篇关于Excel VBA使用单词表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆