0

i try to create button to add data with looping on last row, but i got error with code "object variable or with block variable not set"

here is my code

Sub Add_Click2()

Application.ScreenUpdating = False

Dim emptyRow1 As Long
Dim Celltujuan2 As Range

Set Menu = Sheets("Interface")
Set Db2 = Sheets("Database CMMS")

'DB Power
Db2.Activate
RCNumberCMMS = Menu.Range("D20").Value
Set Celltujuan2 = Db2.Range("A:A").Find(What:=RCNumberCMMS)

If RCNumberCMMS = "" Then
MsgBox ("Silakan isi RC Number terlebih Dahulu!")
Exit Sub
End If


Celltujuan2 = Db2.Range("A99999").End(xlUp).Row + 1

'Info
For i = 25 To 35
Cells(Celltujuan2, Menu.Range("E" & i).Value) = Menu.Range("D" & i).Value
Next i

'DOP & Approval
For i = 25 To 34
Cells(Celltujuan2, Menu.Range("I" & i).Value) = Menu.Range("H" & i).Value
Next i


'Install & Material Eks
For i = 25 To 33
Cells(Celltujuan2, Menu.Range("M" & i).Value) = Menu.Range("L" & i).Value
Next i

'Write Off
For i = 25 To 26
Cells(Celltujuan2, Menu.Range("Q" & i).Value) = Menu.Range("P" & i).Value
Next i

MsgBox ("Data Telah Terupdate")


Menu.Activate
Menu.Range("D20").Select

End Sub

i do the same code for other sheet and running well, i create this code in the same workbook but in diffren module, is that the problem ?

10
  • 2
    Celltujuan2 = Db2.Range("A99999").End(xlUp).Row + 1 needs to be set Celltujuan2 = Db2.Range("A99999").End(xlUp).Row + 1 Commented Jun 17, 2021 at 6:12
  • 1
    Also Db2.Range("A99999").End(xlUp).Row + 1 returns a number, which isn't compatible with a range variable. Commented Jun 17, 2021 at 6:22
  • Hy Christofer, any suggest for row number that isn't compatible with a range variable ? Commented Jun 17, 2021 at 6:27
  • Your code does not make sense. First you Set Celltujuan2 = Db2.Range("A:A").Find(What:=RCNumberCMMS) Then you try to overwrite the same variable Celltujuan2 = Db2.Range("A99999").End(xlUp).Row + 1 It is very unclear what you are trying to do here. Commented Jun 17, 2021 at 6:29
  • 1
    @ChristoferWeber no actually the If RCNumberCMMS = "" Then has nothing to do with If Celltujuan2 Is Nothing Then that doesn't need to be replaced, that is a completly independent test. Just add the test for If Celltujuan2 Is Nothing Then. Commented Jun 17, 2021 at 6:57

1 Answer 1

1

After using Find you need to check if something was found.

Set Celltujuan2 = Db2.Range("A:A").Find(What:=RCNumberCMMS)
If Celltujuan2 Is Nothing Then
    MsgBox """" & RCNumberCMMS & """ was not found."
    Exit Sub
End If

If you read the documentation of the Range.Find method it says:

The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you do not specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

So you need to specify at least those parameters or something random is used (there is no default).

Set Celltujuan2 = Db2.Range("A:A").Find(What:=RCNumberCMMS, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchByte:=False)
If Celltujuan2 Is Nothing Then
    MsgBox """" & RCNumberCMMS & """ was not found."
    Exit Sub
End If
Sign up to request clarification or add additional context in comments.

Comments

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.