2

Getting error: Object variable or With block variable not set (Error 91) in excel vba.

    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

Error is exactly here:

after the if statement:

users.ListObjects("USERS").DataBodyRange(NextRecordIndex, 1).Value = NewUser.Range("D" & 6).Value

The code in the else condition is working fine. It only gives this error if the USERS table is empty and the row count is zero.

Can anyone please guide what is the problem?

Thanks..

4
  • So on the USERS worksheet, you have a table called USERS? Commented May 2, 2015 at 14:00
  • Check if ThisWorkbook.Worksheets("NEW USER") evaluates to Nothing Commented May 2, 2015 at 14:00
  • Yes there is a USERS table under USERS worksheet. Commented May 2, 2015 at 14:08
  • The code in else condition is working fine: NextRecordIndex = NextRecordIndex + 1 users.ListObjects("USERS").DataBodyRange(NextRecordIndex, 1).Value = NewUser.Range("D" & 6).Value It only doesn't work if NextRecordIndex is 0 (Table is empty) Commented May 2, 2015 at 14:08

1 Answer 1

3

If the number or records is 0, you can't refer to:

users.ListObjects("USERS").DataBodyRange

Because it does not exist (DataBodyRange = Nothing)

You should add an empty row first:

users.ListObjects("USERS").ListRows.Add

You should also replace:

users.ListObjects("USERS").DataBodyRange(NextRecordIndex, 1).Value

with

users.ListObjects("USERS").DataBodyRange(NextRecordIndex+1, 1).Value

EDIT:

The if statement is probably redundant, the following code should work in every case:

NextRecordIndex = users.ListObjects("USERS").ListRows.Count

users.ListObjects("USERS").ListRows.Add
users.ListObjects("USERS").DataBodyRange(NextRecordIndex + 1, 1).Value = 
      NewUser.Range("D" & 6).Value
Sign up to request clarification or add additional context in comments.

4 Comments

Nice hint. error is gone but the records got inserted in the header row. Any way to insert in the first table row when table is empty?
@SupermanAkrt Please take a look at the second part of the answer (edit). Simply add 1 to NextRecordIndex.
Great, working now but I have a question: When I user: users.ListObjects("USERS").DataBodyRange(1, 1).Value It added in the 2nd DataBodyRange row.
@SupermanAkrt I have edited my answer once again to simplify your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.