excel – VBA – 范围对象仅设置一次循环

我正在编写与日期匹配的代码(来自文件),将其放入集合中,然后尝试在电子表格中找到它.一旦找到它,它会将以下两个项目放在两个单元格的集合中.当我运行它时,我收到以下错误:“对象变量或未设置块变量”.我试图调试我的代码,它显示在下面的代码的第一个循环之后,范围对象“rthecell”变为正确的值.一旦循环的第二次迭代发生,“rthecell”的值变为“Nothing”.

例如:

    Set rtheCell = Range("A:A").Find(What:=LineItem1)
    rtheCell.Offset(, 1).Value = LineItem3 
    rtheCell.Offset(, 2).Value = LineItem2
    Set rtheCell = Nothing

同样,一切都在循环的第一次迭代中按预期工作,但是一旦第二次迭代发生,我就会收到错误.

这是完整的代码:

Sub InputData()

'Declare variables

Dim sFilePath As String
Dim sLineFromFile As String
Dim saLineItems() As String
Dim element As Variant
Dim col As Collection
Dim LineItem1 As String
Dim LineItem2 As String
Dim LineItem3 As String
Dim rtheCell As Range

Set col = New Collection

'Insert file path name here, this file will be overwritten each morning

sFilePath = "P:\Billing_Count.csv"

Open sFilePath For Input As #1

Do Until EOF(1)

    Line Input #1, sLineFromFile

    'Split each line into a string array
    'First replace all space with comma, then replace all double comma with single comma
    'Replace all commas with space
    'Then perform split with all values separated by one space

    sLineFromFile = Replace(sLineFromFile, Chr(32), ",")
    sLineFromFile = Replace(sLineFromFile, ",,", ",")
    sLineFromFile = Replace(sLineFromFile, ",", " ")
    saLineItems = Split(sLineFromFile, " ")

    'Add line from saLineItem array to a collection
    For Each element In saLineItems
        If element <> " " Then
        col.Add element
        End If
    Next

Loop

Close #1

'Place each value of array into a smaller array of size 3
Dim i As Integer
i = 1

Do Until i > col.Count


    'Place each value of array into a string-type variable

    'This line is the date
    LineItem1 = col.Item(i)
    i = i + 1
    'This line should be the BW count make sure to check
    LineItem2 = col.Item(i)
    i = i + 1
    'This line should be the ECC count make sure to check
    LineItem3 = col.Item(i)
    i = i + 1

    'Find the matching date in existing Daily Billing File (dates on Excel must be formatted as
    'general or text) and add ECC and BW counts on adjacent fields

    Set rtheCell = Range("A3:A37").Find(What:=LineItem1)
    rtheCell.Offset(, 1).Value = LineItem3 'This is LineItem3 since we can ECC data to appear before BW
    rtheCell.Offset(, 2).Value = LineItem2
    Set rtheCell = Nothing
    LineItem1 = 0

Loop

'Format cells to appear as number with no decimals
'Format cells to have horizontal alignment
Sheets(1).Range("B3:C50").NumberFormat = "0"
Sheets(1).Range("C3:C50").HorizontalAlignment = xlRight


End Sub

最佳答案 当您使用 Range.Find method时,通常您可以在后续调用中使用After:=参数,或者使用假设After:=最后找到的项目的 Range.FindNext method.由于您没有以任何方式修改实际找到的单元格的值,因此您需要记录原始找到的单元格(通常是地址),因为最终您将循环回原始单元格.

dim fndrng as range, fndstr as string
set fndrng = Range("A:A").Find(What:=LineItem1, after:=cells(rows.count, "A"))
if not fndrng is nothing then
    fndstr = fndrng.address
    do while True

        'do stuff here

        set fndrng = Range("A:A").FindNext(after:=fndrng)
        if fndstr = fndrng.address then exit do
    loop
end if

这应该让你想到循环所有匹配的调用,直到你循环回到原始. tbh,很难充分扩展所提供的少量代码.

点赞