gpt4 book ai didi

excel - VBA 中的 .Find() 方法出现“类型不匹配”错误

转载 作者:行者123 更新时间:2023-12-04 21:24:19 26 4
gpt4 key购买 nike

我正在 Excel VBA 中编写一个子程序,当尝试将 .Find() 的结果分配给 Range 变量时,它一直给我一个“类型不匹配”错误消息。我很有信心我的类型是合适的,所以也许某处存在语法错误?

非常感谢您的帮助:
(星号前面的行是引发错误的地方)

Sub totalTiger(fCode As String, project As String, ttls() As Double)

'Set shorcuts to Worksheets
Dim this As Workbook: Set this = ThisWorkbook
Dim proj As Worksheet: Set proj = this.Worksheets(project)

'Dim req variables
Dim tRng As Range: Set tRng = proj.Range("A:A").Find(What:="Program Description") 'Establish where Staff data ends and Tiger data begins
***Dim rng As Range: Set rng = proj.Range("C:C").Find(What:=fCode, After:=tRng) 'First fCode entry***

'For each fCode entry BEFORE the Tiger data, sum the assignments by month
Do While Not rng Is Nothing And rng.row > tRng.row

'For each month
For col = 4 To 15

'Add this month's assignment to our running total
ttls(col - 4) = ttls(col - 4) + CDbl(proj.Cells(rng.row, col).Value)

Next

'Update the rng reference
Set rng = proj.Range("C:C").Find(What:=fCode, After:=rng)

Loop

End Sub

最佳答案

我认为问题出在“After:=tRng”:它可能超出了“find”的范围

Dim rng As Range:   Set rng = proj.Range("C:C").Find(What:=fCode, After:=tRng)

尝试删除“After:=tRng”,如果删除后有效,则尝试插入正确的范围。
Dim rng As Range:   Set rng = proj.Range("C:C").Find(What:=fCode)

我不确定这是否是您需要的,但您可以尝试:
Dim rng:    Set rng = proj.Range("C:C").Find(What:=fCode, After:=proj.Range("C" & tRng.Row))

它从找到“程序描述”的行开始找到第一个“fCode”

关于excel - VBA 中的 .Find() 方法出现“类型不匹配”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28460641/

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