PC Review


Reply
Thread Tools Rate Thread

Application.undo runtime error '1004'

 
 
Adam
Guest
Posts: n/a
 
      15th Oct 2009
Hi,

I am using a vba code to track changes made to a spreadsheet on another
sheet.

Code:
=====================================================
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Changelog" Then Exit Sub
Application.EnableEvents = False

UserName = Environ("USERNAME")

Sheets("Changelog").Unprotect ("test")
NewVal = Target.Value
Application.Undo
oldVal = Target.Value

lr = Sheets("Changelog").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Changelog").Range("A" & lr) = Now
Sheets("Changelog").Range("B" & lr) = ActiveSheet.Name
Sheets("Changelog").Range("C" & lr) = Target.Address
Sheets("Changelog").Range("D" & lr) = oldVal
Sheets("Changelog").Range("E" & lr) = NewVal
Sheets("Changelog").Range("F" & lr) = UserName
Target = NewVal
Application.EnableEvents = True

Sheets("Changelog").Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True, Password:="test"


End Sub

===================================================


My problem is I can't figure out how to code to where the user is able to
add/delete rows. Currently when someone tries to add or delete a row they
will receive a Run Time error '1004'.

I tried an If Activesheet.EntireRow.Insert then statement but that failed
miserably.
If I can track additions and deletions of rows, great, but if all changes
are tracked except for the adding/deleting of rows, that would suffice.

Thanks in advance.


 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      15th Oct 2009
First, I don't see a .unprotect line before you try to change anything.

If you want to test to see if you're working with an entire row or column...

if target.address = target.entirerow.address then
'entire row


if target.address = target.entirecolumn.address then
'entire column

========
And if you're processing multiple cells at a time, then you're going to have to
loop through the changed cells. And probably keep track of each of the values
in each of the areas that changed.

It may be simpler to just ignore(!) those changes.

if target.cells.count > 1 then exit sub

====
And instead of using the Activesheet, use the object that VBA shares with you:
Sh

If lcase(Sh.Name) = lcase("Changelog") Then Exit Sub
and
Sheets("Changelog").Range("B" & lr) = Sh.Name


Adam wrote:
>
> Hi,
>
> I am using a vba code to track changes made to a spreadsheet on another
> sheet.
>
> Code:
> =====================================================
> Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
> If ActiveSheet.Name = "Changelog" Then Exit Sub
> Application.EnableEvents = False
>
> UserName = Environ("USERNAME")
>
> Sheets("Changelog").Unprotect ("test")
> NewVal = Target.Value
> Application.Undo
> oldVal = Target.Value
>
> lr = Sheets("Changelog").Range("A" & Rows.Count).End(xlUp).Row + 1
> Sheets("Changelog").Range("A" & lr) = Now
> Sheets("Changelog").Range("B" & lr) = ActiveSheet.Name
> Sheets("Changelog").Range("C" & lr) = Target.Address
> Sheets("Changelog").Range("D" & lr) = oldVal
> Sheets("Changelog").Range("E" & lr) = NewVal
> Sheets("Changelog").Range("F" & lr) = UserName
> Target = NewVal
> Application.EnableEvents = True
>
> Sheets("Changelog").Protect DrawingObjects:=True, Contents:=True,
> Scenarios:=True, Password:="test"
>
> End Sub
>
> ===================================================
>
> My problem is I can't figure out how to code to where the user is able to
> add/delete rows. Currently when someone tries to add or delete a row they
> will receive a Run Time error '1004'.
>
> I tried an If Activesheet.EntireRow.Insert then statement but that failed
> miserably.
> If I can track additions and deletions of rows, great, but if all changes
> are tracked except for the adding/deleting of rows, that would suffice.
>
> Thanks in advance.


--

Dave Peterson
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find: runtime error 1004 Application-defined or object-defined error inspired Microsoft Excel Discussion 3 30th Nov 2010 12:34 PM
runtime error '1004' application or object defined error =?Utf-8?B?SmFuaXM=?= Microsoft Excel Programming 4 18th Nov 2009 03:01 PM
Runtime error 1004 on an application.count statement PhilM Microsoft Excel Programming 1 27th Feb 2008 03:36 PM
runtime error '1004' application or object defined error. Please help deej Microsoft Excel Programming 0 1st Aug 2007 09:26 AM
RE: Runtime error 1004- application defined or object defined erro =?Utf-8?B?SmltIFRob21saW5zb24=?= Microsoft Excel Programming 0 6th Feb 2006 09:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.