在excel vba中出現錯誤:對象變量或塊變量未設置(錯誤91)。對象變量或在excel中未設置塊變量(錯誤91)
Sub RegisterNewUser()
'Disable screen refresh during code excution
Application.ScreenUpdating = False
'Variables
Dim users As Worksheet 'Variable for users table sheet
Dim NewUser As Worksheet 'Variable for new user entry sheet
Dim RecordIsValid As String
Dim NextRecordIndex As Long 'Variable for the next available row in the users table
Set users = ThisWorkbook.Worksheets("USERS")
Set NewUser = ThisWorkbook.Worksheets("NEW USER")
NextRecordIndex = users.ListObjects("USERS").ListRows.Count
If NextRecordIndex = 0 Then
users.ListObjects("USERS").DataBodyRange(NextRecordIndex, 1).Value = NewUser.Range("D" & 6).Value
Else
NextRecordIndex = NextRecordIndex + 1
users.ListObjects("USERS").DataBodyRange(NextRecordIndex, 1).Value = NewUser.Range("D" & 6).Value
End If
錯誤正是這裏:
after the if statement:
users.ListObjects( 「用戶」)DataBodyRange(NextRecordIndex,1)。價值= NewUser.Range( 「d」 & 6)。價值
。else條件中的代碼工作正常。它只在USERS表爲空且行數爲零時纔會出現此錯誤。
任何人都可以請指導是什麼問題?
謝謝..
因此,在USERS工作表上,您有一張名爲USERS的表? – Jeeped
檢查ThisWorkbook.Worksheets(「新用戶」)的計算結果爲Nothing –
是的,在USERS工作表下有一個USERS表。 –