Type Mismatch Error and Wrong Order of Output

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
I am having two issues with the below working code:

  1. It places the last action at the bottom instead of the top.
  2. When a user completes an auto-fill selection it kicks out a bug (as if it is getting overloaded with things to report on) [The actual error is a Run-Time Error '13': Type mismatch] which bugs on this line ==> If Target.Value <> PreviousValue Then.
Regarding 2, I would be okay with suspending the code while an auto-fills are complete (since they don't happen that often).

As a side not this is running on a single sheet in the workbook rather than a module.


Code:
Dim PreviousValue

'Private Sub Worksheet_Change(ByVal Target As Range)


 ' If Target.Value <> PreviousValue Then
  '   Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
   '    Application.UserName & " changed cell " & Target.Address _
    ' & " from " & PreviousValue & " to " & Target.Value & " at " & Now() & " for file: " & ActiveWorkbook.FullName
' End If
'End Sub

'Private Sub Worksheet_SelectionChange(ByVal Target As Range)


 ' PreviousValue = Target.Value
'End Sub
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
Perhaps.
Code:
Option Explicit

Dim PreviousValue

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    If Target.Value <> PreviousValue Then
        Application.EnableEvents = False
        Sheets("log").Range("A2").Insert xlShiftDown
        Sheets("log").Range("A2").Value = _
        Application.UserName & " changed cell " & Target.Address _
                                          & " from " & PreviousValue & " to " & Target.Value & " at " & Now() & " for file: " & ActiveWorkbook.FullName
        Application.EnableEvents = True
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    PreviousValue = Target.Value
End Sub
 

BrianMH

Well-known Member
Joined
Mar 26, 2008
Messages
1,490
I can't reproduce the error you are getting but if you want it to write to the top row of the log you need to do this instead.

Code:
Dim PreviousValue

Private Sub Worksheet_Change(ByVal Target As Range)




    If Target.Value <> PreviousValue Then
        Sheets("log").Rows(1).Insert
        Sheets("log").Cells(1, 1).Value = _
        Application.UserName & " changed cell " & Target.Address _
        & " from " & PreviousValue & " to " & Target.Value & " at " & Now() & " for file: " & ActiveWorkbook.FullName
    End If
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    PreviousValue = Target.Value
End Sub

Edit: yeah forgot about it erroring with more than 1 cell. That's why Norie added the count>1
 

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
Thanks guys for the quick response....

Norie :: When I copy the sheet to another part of the workbook I get the following error:

Run-Time error '6': Overflow

It's buggin on the code below in red...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
PreviousValue = Target.Value
End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Are you copying the sheet or cells on the sheet?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Sorry I don't follow.

Are you right clicking the sheet tab, selecting Move or Copy... and then selecting to copy the sheet?
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,782
Office Version
  1. 2010
Platform
  1. Windows
You're welcome
 

Watch MrExcel Video

Forum statistics

Threads
1,122,780
Messages
5,598,038
Members
414,205
Latest member
Tushark

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
Top