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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Are you copying the sheet or cells on the sheet?
 
Upvote 0
Sorry I don't follow.

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

Forum statistics

Threads
1,214,527
Messages
6,120,058
Members
448,940
Latest member
mdusw

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