VBA sheets.activate 不会更改 sheet 它从中获取信息
VBA sheets.activate will not change the sheet it takes information from
我试图在 运行 期间更改工作表,所以我在 VBA:
中编写了这段代码
Dim Item As Variant
Dim Info As Variant
Dim Stat As Integer
Stat = 2
Dim Line As Integer
Line = 1
Item = "Cases"
Sheets(Item).Activate
Do
Line = Line + 1
Loop While Cells(Line, "B") <> Null
Do
Info = Cells(1, Stat)
Info = InputBox("what is the item " & Info & "?")
Cells(Line, Stat) = Info
Loop While Cells(1, Stat) <> Null
激活函数确实打开了名为 "Cases" 的工作表,就像我想要的那样,但是当这部分 运行s "Info = Cells(1, Stat)" 它仍然从最后一个工作表中获取信息,即使我请参阅 "Cases" 工作表。
我查看了我编写的另一个 vba 代码,我使用了相同的方法,它在那里工作,但我找不到我在那里做的任何不同的事情可以使它在那里工作,但在这里丢失了。
将 sheet 拉入它自己的局部变量,你不需要 Activate
任何东西(如果你有一个 Workbook
对象,用它代替 ActiveWorkbook
来限定 Worksheets
调用)。
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Cases")
现在不循环获取 last/next 行:
Dim line As Long
line = ws.Range("B" & ws.Rows.Count).End(xlUp).Row + 1
现在,您的循环条件将不起作用,因为 Excel 中的任何内容都不会成为 Null
。相反,查找 Empty
单元格,并使用 ws
变量来限定 每个 单个 Cells
成员调用:
Do
Info = ws.Cells(1, Stat).Value
Info = InputBox("what is the item " & Info & "?")
ws.Cells(Line, Stat).Value = Info
Loop While Not IsEmpty(ws.Cells(1, Stat).Value)
我试图在 运行 期间更改工作表,所以我在 VBA:
中编写了这段代码 Dim Item As Variant
Dim Info As Variant
Dim Stat As Integer
Stat = 2
Dim Line As Integer
Line = 1
Item = "Cases"
Sheets(Item).Activate
Do
Line = Line + 1
Loop While Cells(Line, "B") <> Null
Do
Info = Cells(1, Stat)
Info = InputBox("what is the item " & Info & "?")
Cells(Line, Stat) = Info
Loop While Cells(1, Stat) <> Null
激活函数确实打开了名为 "Cases" 的工作表,就像我想要的那样,但是当这部分 运行s "Info = Cells(1, Stat)" 它仍然从最后一个工作表中获取信息,即使我请参阅 "Cases" 工作表。
我查看了我编写的另一个 vba 代码,我使用了相同的方法,它在那里工作,但我找不到我在那里做的任何不同的事情可以使它在那里工作,但在这里丢失了。
将 sheet 拉入它自己的局部变量,你不需要 Activate
任何东西(如果你有一个 Workbook
对象,用它代替 ActiveWorkbook
来限定 Worksheets
调用)。
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Cases")
现在不循环获取 last/next 行:
Dim line As Long
line = ws.Range("B" & ws.Rows.Count).End(xlUp).Row + 1
现在,您的循环条件将不起作用,因为 Excel 中的任何内容都不会成为 Null
。相反,查找 Empty
单元格,并使用 ws
变量来限定 每个 单个 Cells
成员调用:
Do
Info = ws.Cells(1, Stat).Value
Info = InputBox("what is the item " & Info & "?")
ws.Cells(Line, Stat).Value = Info
Loop While Not IsEmpty(ws.Cells(1, Stat).Value)