利用VBA实现WORD特定位置中录入EXCEL中数字,用的WPS 5
利用VBA实现WORD特定位置中录入EXCEL中某些数字,比如有WORD文件路径“F:\编程学习\3月\3.doc”,EXCEL文件同路径“F:\编程学习\3月\2016...
利用VBA实现WORD特定位置中录入EXCEL中某些数字,比如有WORD文件路径“F:\编程学习\3月\3.doc”,EXCEL文件同路径“F:\编程学习\3月\2016年3月.xls”,需要将EXCEL中的B3和C3数字,替换WORD中的“该月有AA例和BB例”中的AA和BB。求高手。我用的WPS专业版。万分感谢
展开
1个回答
2016-04-14 · 知道合伙人软件行家
关注
展开全部
只会用excel,wps没用过。
这vba要根据需求来定的。下面这个例子供参考。
Option Explicit
Sub カチネ。ミナマ「(MYFILE As String, MYROW As Integer)
Dim myS As String
Dim wdApp, wdDocument
Dim wdTable
Dim i, j, k, L, M As Integer
Dim myArr(1 To 10000) As String
Dim dArr(1 To 10, 1 To 3)
Dim ii As Integer
'myfile = Cells(2, 4)
'myfile = ThisWorkbook.Path & "\" & myfile & ".docx"
'Set wdApp = GetObject(, "word.Application") 'ネ。オテトソヌーエェオトwordモヲモテ
'wdApp.Quit
100 Set wdApp = CreateObject("word.application") 'エエスィメサクordモヲモテ
Dim ocell
k = 1
Set wdDocument = wdApp.Documents.Open(MYFILE)
wdDocument.Select
i = wdDocument.Tables.Count
Dim mysArr
For j = 1 To i
Set wdTable = wdDocument.Tables(j)
'For Each ocell In ActiveDocument.Tables(J).Range.Cells 'ラ「メ磴ellsヌーモミクange
For Each ocell In wdTable.Range.Cells 'ラ「メ磴ellsヌーモミクange
' MsgBox ocell.Range.Text
myS = ocell.Range.Text
'mysArr = Split(myS, vbCrLf)
mysArr = Split(myS, Chr(7))
'If Right(myS, 1) = Chr(13) Then
'Do While Right(myS, 1) = Chr(7) Or Right(myS, 1) = Chr(32) Or Right(myS, 1) = Chr(13)
' myS = Mid(myS, 1, Len(myS) - 1)
'Loop
'End If
For ii = 0 To UBound(mysArr)
If Trim(mysArr(ii)) <> "" And Trim(mysArr(ii)) <> Chr(13) Then
'ネ・ウモメイ狃リウオキ釞ーソユク・
If Right(mysArr(ii), 1) = Chr(13) Then
Do While Right(mysArr(ii), 1) = Chr(7) Or Right(mysArr(ii), 1) = Chr(32) Or Right(mysArr(ii), 1) = Chr(13)
mysArr(ii) = Mid(mysArr(ii), 1, Len(mysArr(ii)) - 1)
Loop
End If
myArr(k) = Trim(mysArr(ii))
k = k + 1
End If
Next ii
'myArr(k) = myS
'k = k + 1
Next ocell
Next j
wdApp.Quit
k = k - 1 'kシヌツシwordホトオオオトハセンク
Sheets("sheet3").Select
For i = 1 To k
Cells(i, 1) = i
Cells(i, 2) = myArr(i)
Next i
Sheets("tools").Select
i = 2
Do While Cells(i, 7) <> ""
dArr(i - 1, 1) = Cells(i, 7)
dArr(i - 1, 2) = Cells(i, 8)
dArr(i - 1, 3) = Cells(i, 9)
i = i + 1
Loop
i = i - 2 'Iシヌツシカチネ。カヤマトク
Dim tArr
For j = 1 To i
ReDim tArr(1 To dArr(j, 3))
'イ鲽メケリシ・ヨ
For L = 1 To k
If dArr(j, 1) = myArr(L) Then
Exit For
End If
Next L 'Lシヌツシイ鲂スオスケリシ・ヨオトホサヨテ
L = L + dArr(j, 2) + dArr(j, 3) 'ホサヨテオ訒ャユメオスオレメサクセンオトホサヨテ
For M = 1 To dArr(j, 3)
tArr(M) = myArr(L + M - 1)
Next M
'スォイ鲽メオトハヨオミエネ・クカィホサヨテ
Select Case j
Case 1
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 2) = tArr(1)
Cells(MYROW, 3) = tArr(2)
Cells(MYROW, 4) = tArr(3)
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 2) = tArr(1)
Cells(MYROW, 3) = tArr(2)
Case 2
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 5) = tArr(1)
Cells(MYROW, 6) = tArr(2)
Cells(MYROW, 7) = tArr(3)
Cells(MYROW, 8) = tArr(4)
Cells(MYROW, 10) = tArr(6)
Cells(MYROW, 12) = tArr(7)
Cells(MYROW, 13) = tArr(8)
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 6) = tArr(5)
Case 3
Case 4
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 11) = tArr(2)
Case 5
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 4) = tArr(2)
Cells(MYROW, 5) = tArr(3)
Case 6
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 7) = tArr(2)
Cells(MYROW, 8) = tArr(3)
Cells(MYROW, 9) = tArr(4)
Cells(MYROW, 10) = tArr(5)
Cells(MYROW, 11) = tArr(6)
End Select
Next j
End Sub
这vba要根据需求来定的。下面这个例子供参考。
Option Explicit
Sub カチネ。ミナマ「(MYFILE As String, MYROW As Integer)
Dim myS As String
Dim wdApp, wdDocument
Dim wdTable
Dim i, j, k, L, M As Integer
Dim myArr(1 To 10000) As String
Dim dArr(1 To 10, 1 To 3)
Dim ii As Integer
'myfile = Cells(2, 4)
'myfile = ThisWorkbook.Path & "\" & myfile & ".docx"
'Set wdApp = GetObject(, "word.Application") 'ネ。オテトソヌーエェオトwordモヲモテ
'wdApp.Quit
100 Set wdApp = CreateObject("word.application") 'エエスィメサクordモヲモテ
Dim ocell
k = 1
Set wdDocument = wdApp.Documents.Open(MYFILE)
wdDocument.Select
i = wdDocument.Tables.Count
Dim mysArr
For j = 1 To i
Set wdTable = wdDocument.Tables(j)
'For Each ocell In ActiveDocument.Tables(J).Range.Cells 'ラ「メ磴ellsヌーモミクange
For Each ocell In wdTable.Range.Cells 'ラ「メ磴ellsヌーモミクange
' MsgBox ocell.Range.Text
myS = ocell.Range.Text
'mysArr = Split(myS, vbCrLf)
mysArr = Split(myS, Chr(7))
'If Right(myS, 1) = Chr(13) Then
'Do While Right(myS, 1) = Chr(7) Or Right(myS, 1) = Chr(32) Or Right(myS, 1) = Chr(13)
' myS = Mid(myS, 1, Len(myS) - 1)
'Loop
'End If
For ii = 0 To UBound(mysArr)
If Trim(mysArr(ii)) <> "" And Trim(mysArr(ii)) <> Chr(13) Then
'ネ・ウモメイ狃リウオキ釞ーソユク・
If Right(mysArr(ii), 1) = Chr(13) Then
Do While Right(mysArr(ii), 1) = Chr(7) Or Right(mysArr(ii), 1) = Chr(32) Or Right(mysArr(ii), 1) = Chr(13)
mysArr(ii) = Mid(mysArr(ii), 1, Len(mysArr(ii)) - 1)
Loop
End If
myArr(k) = Trim(mysArr(ii))
k = k + 1
End If
Next ii
'myArr(k) = myS
'k = k + 1
Next ocell
Next j
wdApp.Quit
k = k - 1 'kシヌツシwordホトオオオトハセンク
Sheets("sheet3").Select
For i = 1 To k
Cells(i, 1) = i
Cells(i, 2) = myArr(i)
Next i
Sheets("tools").Select
i = 2
Do While Cells(i, 7) <> ""
dArr(i - 1, 1) = Cells(i, 7)
dArr(i - 1, 2) = Cells(i, 8)
dArr(i - 1, 3) = Cells(i, 9)
i = i + 1
Loop
i = i - 2 'Iシヌツシカチネ。カヤマトク
Dim tArr
For j = 1 To i
ReDim tArr(1 To dArr(j, 3))
'イ鲽メケリシ・ヨ
For L = 1 To k
If dArr(j, 1) = myArr(L) Then
Exit For
End If
Next L 'Lシヌツシイ鲂スオスケリシ・ヨオトホサヨテ
L = L + dArr(j, 2) + dArr(j, 3) 'ホサヨテオ訒ャユメオスオレメサクセンオトホサヨテ
For M = 1 To dArr(j, 3)
tArr(M) = myArr(L + M - 1)
Next M
'スォイ鲽メオトハヨオミエネ・クカィホサヨテ
Select Case j
Case 1
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 2) = tArr(1)
Cells(MYROW, 3) = tArr(2)
Cells(MYROW, 4) = tArr(3)
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 2) = tArr(1)
Cells(MYROW, 3) = tArr(2)
Case 2
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 5) = tArr(1)
Cells(MYROW, 6) = tArr(2)
Cells(MYROW, 7) = tArr(3)
Cells(MYROW, 8) = tArr(4)
Cells(MYROW, 10) = tArr(6)
Cells(MYROW, 12) = tArr(7)
Cells(MYROW, 13) = tArr(8)
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 6) = tArr(5)
Case 3
Case 4
Sheets("クヒシーナ菁シノ昞ンセモラ。ミナマ「タ・).Select
Cells(MYROW, 11) = tArr(2)
Case 5
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 4) = tArr(2)
Cells(MYROW, 5) = tArr(3)
Case 6
Sheets("ケ、ラナマ「タ・).Select
Cells(MYROW, 7) = tArr(2)
Cells(MYROW, 8) = tArr(3)
Cells(MYROW, 9) = tArr(4)
Cells(MYROW, 10) = tArr(5)
Cells(MYROW, 11) = tArr(6)
End Select
Next j
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |