gpt4 book ai didi

vba - 使用 Word VBA 自动化 Excel,我得到运行时错误 '13' : Type mismatch when using the . 查找函数

转载 作者:行者123 更新时间:2023-12-04 20:11:54 24 4
gpt4 key购买 nike

我正在尝试将数据从 Excel 工作表获取到 Word 文档。我尝试使用“查找”功能,但是在此行上我不断收到相同的错误“类型不匹配”:

Set FoundRange = .Cells.Find(260707)

这是我正在运行的子程序。
    Sub GetID()
Dim oXL As Object
Dim oWB As Object
Dim oSheet As Object
Dim WorkbookToWorkOn As String
Dim FoundRange As Range
Dim dummyvar As String

'Start a new instance of Excel
Set oXL = CreateObject("Excel.Application")
'Line to make Excel Visible or not
oXL.Visible = False
'Open the workbook
'Set the file path to access the 'Certified Personnel' table
WorkbookToWorkOn = "\\DataSource\CertifiedPersonnel.xlsx"
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
Set oSheet = oXL.ActiveWorkbook.Sheets("tblCertifiedPersonnel")
'End of Excel Automation. Everything from this point on can reference Excel.

With oSheet
dummyvar = .Cells(1, 2).Text
.Cells(1, 2).Select
'Set the range of the cell containing the ID number
'If the ID was found
Set FoundRange = .Cells.Find(260707)

If Not FoundRange Is Nothing Then
'Set the NTlogin equal to the value of column 1, and row corresponding to the FoundRange row
NTlogin = .Cells(FoundRange.Rows, 1).Text
Role = .Cells(FoundRange.Rows, 4).Text
End If
End With

'End Excel reference
oXL.ActiveWorkbook.Close SaveChanges:=False
oXL.Application.Quit
Set oXL = Nothing
Set oWB = Nothing
Set oSheet = Nothing


End Sub

我知道它正在访问正确的工作簿,因为虚拟变量 (dummyvar) 正在返回我期望的值。我已经尝试了一些与“查找”功能相关的事情,但是我无法让它工作。有任何想法吗?非常感激。

最佳答案

您正在使用后期绑定(bind)并且拥有 FoundRange声明为 Range .由于这是在 Word 文档中,因此您将其隐式声明为 Word.Range这里:

Dim FoundRange As Range
.Find正在返回 Excel.Range .将其更改为:
Dim FoundRange As Object

关于vba - 使用 Word VBA 自动化 Excel,我得到运行时错误 '13' : Type mismatch when using the . 查找函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38747963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com