gpt4 book ai didi

VBA - 范围对象在循环中仅设置一次

转载 作者:行者123 更新时间:2023-12-02 11:55:31 26 4
gpt4 key购买 nike

我正在编写与日期(来自文件)匹配的代码,将其放入集合中,然后尝试在电子表格上找到它。一旦找到它,它就会将集合中的以下两项放入两个单元格中。当我运行此命令时,出现以下错误:“未设置对象变量或 With block 变量”。我尝试调试我的代码,它显示在下面的代码的第一个循环之后,范围对象“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:= 参数或使用 Range.FindNext method假定 After:= 最后找到的项目。由于您没有以任何方式修改实际找到的单元格的值,因此您需要记录原始找到的单元格(通常是地址),因为最终您将循环回到原始单元格。

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

这应该会让您想到循环所有匹配的调用,直到循环回原始调用。老实说,很难充分扩展所提供的少量代码。

关于VBA - 范围对象在循环中仅设置一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34211703/

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