Object variable or With block variable not set

Koen

New Member
Joined
Mar 10, 2011
Messages
18
Hi, I have the following code, but I keep getting 'error 91: Object variable or With block variable not set'.

Does somebody have a solution to this?

Thank you very much!


The code is moving the value of A1 and B1 to A2 and B2, the value of A2 and B2 to A3 and B3, ... every time A1 changes. The value of A1 isn't entered manualy but comes over an OPC server.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect "koen"
    
    Dim c As Range
    c = Range("A" & Rows.Count).End(xlUp).Row
    Range("A" & c + 1).Value = Range("A1").Value
    Range("B" & c + 1).Value = Range("B1").Value

Me.Protect "koen"
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
TRy

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Long

Me.Unprotect "koen"
    
    c = Range("A" & Rows.Count).End(xlUp).Row
    Range("A" & c + 1).Value = Range("A1").Value
    Range("B" & c + 1).Value = Range("B1").Value

Me.Protect "koen"
End Sub
 
Upvote 0
Great!! Thank you very much :).

But I had expected that my code would move the new value of A1 to A2, but instead it puts the value at the botom of the A collum, Do you have any solution to this one?

so it makes: 4123 (4 is in A1, 1 is in A2, 2 is in A3, 3 is in A4)
and not: 4321

I'm sorry, but like you probably already have noticed, i'm a newbie in vba :).

Thank you!!
 
Upvote 0
So should it be?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Me.Unprotect "koen"
    
    Rows(2).Insert
    Range("A2").Value = Range("A1").Value
    Range("B2").Value = Range("B1").Value

Me.Protect "koen"
End Sub
 
Upvote 0
Thanks, I need to be at work to test it. Tomorow morning I will test it, but I think it will do :).

Thank you very much!
 
Upvote 0
Actually we need to turn off events or it will go into an infinite loop

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Me.Unprotect "koen"
    
    Rows(2).Insert
    Range("A2").Value = Range("A1").Value
    Range("B2").Value = Range("B1").Value

Me.Protect "koen"
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,847
Members
452,361
Latest member
d3ad3y3

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