Error 1004 using Worksheet_Change if multiple cells are changed using paste

dlinker

New Member
Joined
Jul 29, 2012
Messages
1
I'm using Excel 2007
I have a Spreadsheet with two sheets, one for data entry and one with a set of data for lookup
I have written a Worksheet_Change macro that when an ID is entered into column 3 of the data entry sheet, it looks up certain columns in the data and if they are blank puts a cross symbol in a corresponding column on the data entry sheet.
This was all working fine when I checked that the Target Range was only a single cell.
I then got more complex and decided to also allow multiple values to be pasted - typically by cutting and pasting a list of IDs from another application.
The code all looks fine - but when attempting to insert more than one value in the corresponding column, I get the error "error '1004' Application-defined or object-defined error". Inserting a single value still works.
Note - I also have a Worksheet_SelectionChange macro that will check and uncheck in certain columns when those cells are clicked on. I've included this for completeness.
Both of these are in the Worksheet module for the data entry sheet.


The worksheets (modified to protect the innocent :- )
Data entry - only column C (Site Number) is changed:
ABSite NumberImpacted Site NamesStatePostcodeOld Site NumberTech1Tech2Tech3Tech4
123200002SydneyNSW20002000rrr
145202036NowraNSW20282028r
145303026MelbourneVIC30833083rr
143303028BendigoVIC30023002rr
132303123BallaratVIC30643064r
112303124GeelongVIC31413141r

<tbody>
</tbody>

"Site Address" sheet used for VLOOKUP:
SiteIDAlt SiteIDSiteTech1Tech2Tech3Tech4
2000022SydneyA0002
2000033NewcastleA0003
2000044WoollongongA0004F0004Q2004
202036NowraA2036Q2036
303026MelbourneA3026Z9026
303028BendigoA3028F3028
303123BallaratA3123Q3123Z3123
303124GeelongA3124F3424Z3128

<tbody>
</tbody>

Private Sub Worksheet_Change(ByVal Target As Range)
'Check changes cell is in the Site Numbers range
Dim cCurrent As Range
If Intersect(Target, Range("SiteNumbers")) Is Nothing Then Exit Sub
'Limit Target count to 1
'If Target.Count > 1 Then Exit Sub
For Each cCurrent In Target
If cCurrent.Value = "" Then
For K = 1 To 4
Cells(cCurrent.Row, K + 7).Value = ""
Next K
Else
' Check that site is valid - and if not show error message
If Application.IsNA(Cells(cCurrent.Row, cCurrent.Column + 1).Value) Then
MsgBox cCurrent.Value & " is not a valid Site Number", vbOKOnly, "Invalid Site"
cCurrent.Value = ""
cCurrent.Select
Else
' Check if there is a blank row above the current row - if so move the site up a row
If Cells(cCurrent.Row - 1, cCurrent.Column).Value = "" Then
Cells(cCurrent.Row - 1, cCurrent.Column).Value = cCurrent.Value
cCurrent.Value = ""
Else
On Error Resume Next
For J = 1 To 4
If Application.VLookup(cCurrent.Value, Worksheets("Site Address").Range("SiteList"), J + 3, False) = "" Then
MsgBox "Vlookup returned blank, cCurrent.Row=" & cCurrent.Row & "; J=" & J
Application.EnableEvents = False
Cells(cCurrent.Row, J + 7).Value = "r" ' <----- This line is generating to 1004 error
MsgBox Err & ": " & Error(Err)
Application.EnableEvents = True
Else
MsgBox "Vlookup returned a value, cCurrent.Row=" & cCurrent.Row & "; J=" & J
Application.EnableEvents = False
Cells(cCurrent.Row, J + 7).Value = "" ' <----- This line is generating to 1004 error
MsgBox Err & ": " & Error(Err)
Application.EnableEvents = True
End If
MsgBox "After if"
Cells(cCurrent.Row, J + 7).Font.Name = "marlett"
Cells(cCurrent.Row, J + 7).Font.Size = "10"
Cells(cCurrent.Row, J + 7).Font.Bold = True
MsgBox "before next J in loop"
Next J
On Error GoTo 0
MsgBox "just after J loop"
End If
End If
End If
Next
Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Only toggle checkmark if Target is in a specific range
If Intersect(Target, Range("Technologies")) Is Nothing Then Exit Sub
' Check that there is a Site Number in Column C
If Cells(Target.Row, 3).Value = "" Then Exit Sub
'Move selection to start of Row so we can toggle on and off
'prevent Select event triggering again when we extend the selection below
Application.EnableEvents = False
Cells(Target.Row, 1).Select
Application.EnableEvents = True
'set Target font to marlett 10 Bold
Target.Font.Name = "marlett"
Target.Font.Size = "10"
Target.Font.Bold = True
'Check value of target and if it's not a
If Target.Value = "" Then
Target.Value = "a" 'Sets target Value = "a" which is a tick
Cancel = True
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Cancel = True
Exit Sub
End If
End Sub


Many thanks in advance for any help with this.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

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