Object variable or with block variable not set

LLT

Board Regular
Joined
Nov 4, 2009
Messages
51
Dear Expert Out there,

I have created the macro below to copy the value to another sheet if the description is closing balance.

However I run into error "object variable or with block variable not set". What does it mean. Can help me on this.


[Dim lr As Long, r As Long
Dim ws As Worksheet
Dim wk As Worksheet
Dim wsName As String
Dim wkDest As String

wsName = Sheets("Instruction").Range("H4")
wkDest = Sheets("Instruction").Range("H6")

On Error Resume Next
Set ws = Sheets(wsName)
Set wk = Sheets(wkDest)

On Error GoTo 0
If ws Is Nothing Then
MsgBox wsName & " is not a valid sheet name for this workbook"
Else
With ws
lr = .Cells(.Rows.Count, "B").End(xlUp).Row
For r = lr To 1 Step -1

' Classify - REPATRIATION
If ws.Range("B" & r) Like "*CLOSING BALANCE*" Then
ws.Range("E" & r).Copy Destination:=wk.Range("E" & 4)
End If
Next r
End With
End If]
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Perhaps

Rich (BB code):
Dim lr As Long, r As Long
Dim ws As Worksheet
Dim wk As Worksheet
Dim wsName As String
Dim wkDest As String

wsName = Sheets("Instruction").Range("H4")
wkDest = Sheets("Instruction").Range("H6")

On Error Resume Next
Set ws = Sheets(wsName)
Set wk = Sheets(wkDest)

On Error GoTo 0
If ws Is Nothing Then
    MsgBox wsName & " is not a valid sheet name for this workbook"
ElseIf wk Is Nothing Then
    MsgBox wkDest & " is not a valid sheet name for this workbook"
Else
    With ws
        lr = .Cells(.Rows.Count, "B").End(xlUp).Row
        For r = lr To 1 Step -1
        
            ' Classify - REPATRIATION
            If ws.Range("B" & r) Like "*CLOSING BALANCE*" Then
                ws.Range("E" & r).Copy Destination:=wk.Range("E" & 4)
            End If
        Next r
    End With
End If
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top