2
以下是我一直在处理的代码。但是,当我从同一个工作簿中选择另一个工作表时,我意识到出了问题。选择其他工作表后变量出错VBA
变量ShiftName
在下面的代码中通过Sheets("Cash").Select
时似乎发生了变化。
我认为ShiftName
的列从工作表“ShiftRoster”的“B”更改为“Cash”的“C”,导致我的输出为ShiftName
错误。
我想检查是否有任何方法可以解决这个问题?
Sub Testing()
Sheets("Shift Roster").Select
Range("A1").Select
Cells.Find("LEAVE").Activate
r1 = ActiveCell.Row
Dim ShiftRowName As Integer
Dim ShiftColName As String: ShiftColName = "B"
Dim ShiftColLeave As String: ShiftColLeave = "E"
Dim ShiftName As String
Dim ShiftReason As String
Dim CashRowName As Integer
Dim CashColName As String: CashColName = "C"
Dim CashColLeave As String: CashColLeave = "H"
Dim CashName As String
Dim CashLeave As String
ShiftRowName = r1 + 1
Do While Cells(ShiftRowName, 1) <> ""
ShiftName = Cells(ShiftRowName, ShiftColName)
ShiftReason = Cells(ShiftRowName, ShiftColLeave)
If ShiftName = "" Or IsEmpty(ShiftName) Then
Exit Do
Else
'SOMETHING WENT WRONG FROM HERE ONWARDS
Sheets("Cash").Select
Range("C1").Select
Cells.Find("Name").Activate
r2 = ActiveCell.Row
CashRowName = r2 + 1
Do While Cells(CashRowName, 1) <> ""
CashName = Cells(CashRowName, CashColName).Value
If CashName = "" Or IsEmpty(CashName) Then
Exit Do
Else
MsgBox ShiftName
End If
CashRowName = CashRowName + 1
Loop
End If
ShiftRowName = ShiftRowName + 1
Loop
End Sub
是的,停止使用'Select'和'* .Active ...'属性。它们本质上不可靠,也很慢。 – RBarryYoung
@RBarryYoung我应该用什么来代替? – stupidgal
您应该使用直接范围并使用'SET'将变量保存到变量中。 – RBarryYoung