如果有可能将 LOOKUP 函数插入到 .打字部分?
现在,我创建了另一个单元格,它基本上是在A1单元格上创建的查找功能。这是100%有效的。
我想删除整个查找列,并将其直接合并到代码中。
想法:从单元格A1中,在另一张工作表中查找并提取一些值,该值通过书签导入单词。
现在:
.GoTo What:=wdGoToBookmark, Name:=BOOKMARK8
.TypeText personList(i, 8)`
我的想法:
.GoTo What:=wdGoToBookmark, Name:=BOOKMARK8
.TypeText Text:= LOOKUP((i, 1),Sheet2!A:A,Sheet2!B:B)`
谢谢。
p.s 告诉我我是否清楚,否则我会附加.xls或完整的代码!
您正在寻找WorkSheetFunction.LOOKUP
您可以在工作表的编辑栏中使用的任何函数都可以在VBA中使用,WorkSheetFunction
这是 WorkSheetFunction
对象上的 MSDN
感谢您的快速回复。但是,我决定插入INDEX&MATCH而不是LOOKUP。
我在网上找到了一些代码,我正在通过一些小的调整来测试它。
Sub Test()
Dim x As String
Dim ws1, ws2 As Worksheet
Dim i As Long
Dim limit As Range
Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")
With ws1
On Error Resume Next
x = Application.WorksheetFunction.Index(Range("Sheet2!$B$1:$B$5"), Application.WorksheetFunction.Match(Range("Sheet1!$A$1"), Range("Sheet2!$A$1:$A$5"), 0))
'=INDEX(Sheet2!$B$1:$B$5;MATCH(Sheet1!A1;Sheet2!$A$1:$A$5;0))'
If Err = 0 Then
ws1.Range("B1") = x
Else
MsgBox "Not found"
Err.Clear
End If
On Error GoTo 0
End With
End Sub
但没有运气。我将其放在评论中的公式运行良好,现在我正在尝试将其合并为 vba 形状。
谢谢。
这是代码的一部分。
For i = 2 To total 'for each person: open the template file and update details
Set wDoc = wApp.Documents.Open(FILE_PATH & FILE_NAME & FILE_EXT, ReadOnly:=True)
With wApp.Selection
datPremFormat = Cells(i, 11).Value
mesDanaKamFormat = Cells(i, 12).Value
x = Application.WorksheetFunction.Index(Sheets("Sheet3").Range("$C$2:$C$1000"), Application.WorksheetFunction.Match(Sheets("Sheet1").Range("B" & i), Sheets("Sheet3").Range("$A$2:$A$1000"), 0), 1)
.GoTo What:=wdGoToBookmark, Name:=BOOKMARK 'go to Bookmark "FirstName"
.TypeText personList(i, 1) 'type the value from column 2
End With
With wDoc 'sava and close the new Word file
.SaveAs2 FILE_PATH & " person " & personList(i, 1) & FILE_EXT
.Close
End With
Next
wApp.Quit
Set wDoc = Nothing
Set wApp = Nothing
MsgBox "Created " & total & " files in " & FILE_PATH
所以,重点是:x 字符串工作正常,我只是通过 MsgBox 检查了它。当然,由于这个循环,我无法纠正它。
运行时错误:1004无法获取工作表函数类的匹配属性