Excel Run-time error '1004'

Hi Experts,

I wonder if you can help me. Whenever I create a copy of a worksheet in Excel I get the following error message.

'Unable to to get the SpecialCells property of the Range Class'

When I click on debug I'm taken to:

xLastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

Can someone please help me fix this?

Cheers

If you need the full code its:

Option Explicit
Private Declare Function sndPlaySound32 _
    Lib "winmm.dll" _
    Alias "sndPlaySoundA" ( _
        ByVal lpszSoundName As String, _
        ByVal uFlags As Long) As Long


Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

If xLastRow < 2 Then Exit Sub


For Each xCell In Range("O2:O" & xLastRow)
    xValue = xCell.Value
    If Not IsError(xValue) Then
        If xValue <> "" And xValue = 0.0002 Then
            Cells(1, 15).Interior.Color = 255
            Point02 = True
            Exit For
        End If
    End If
Next



If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
    xValue = xCell.Value
    xValue2 = xCell.Offset(0, -13).Value
    If Not IsError(xValue) And Not IsError(xValue2) Then
        If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then
            PlayTheSound "tada.wav"

            Cells(1, 17).Interior.Color = 255
            Exit Sub
        End If
    End If
Next

Cells(1, 17).Interior.Color = 65535
If Not Point02 Then Cells(1, 12).Interior.Color = 65535

For Each xCell In Range("l2:l" & xLastRow)
    xValue = xCell.Value
    xValue2 = xCell.Offset(0, -8).Value
    If Not IsError(xValue) And Not IsError(xValue2) Then
        If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then
            PlayTheSound "Windows XP Print complete.wav"

            Cells(1, 12).Interior.Color = 255
            Exit Sub
        End If
    End If
Next

Cells(1, 12).Interior.Color = 65535


End Sub

Sub PlayTheSound(ByVal WhatSound As String)
    If Dir(WhatSound, vbNormal) = "" Then
        ' WhatSound is not a file. Get the file named by
        ' WhatSound from the Windows\Media directory.
        WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
        If InStr(1, WhatSound, ".") = 0 Then
            ' if WhatSound does not have a .wav extension,
            ' add one.
            WhatSound = WhatSound & ".wav"
        End If
        If Dir(WhatSound, vbNormal) = vbNullString Then
            ' Can't find the file. Do a simple Beep.
            Beep
            Exit Sub
        End If
    Else
        ' WhatSound is a file. Use it.
    End If
    ' Finally, play the sound.
    sndPlaySound32 WhatSound, 0&
End Sub





cpatte7372Asked:
Who is Participating?
 
Helen FeddemaCommented:
If the range is just A1, there is only one row -- unless I am missing something?  What is the purpose of the row that throws the error?
0
 
Rory ArchibaldCommented:
Change:
xLastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

Open in new window

to:
xLastRow = Me.Range("A1").SpecialCells(xlLastCell).Row

Open in new window

0
 
Helen FeddemaCommented:
If you are looking for the last used row in the sheet, here is some alternate code:

lngLastRow = shtImport.UsedRange.Rows.Count
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
StephenJRCommented:
Change it to

xLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlup).Row
0
 
cpatte7372Author Commented:
Thanks for responding.

I should have mentioned that the information from the worksheet is taken from live data i.e. live market data. Don't know if that will affect your responses?

I didn't write the code. Another expert wrote it for me.

Cheers
0
 
Rory ArchibaldCommented:
Doesn't affect mine - I don't believe your code should assume that the sheet with the code is active when it calculates.
0
 
cpatte7372Author Commented:
Stephen,

That seemed to work when I pasted it in the worksheet on the left hand side of the worksheet I created.

Out of interest, can you explain what the code achieves?

Cheers
0
 
redmondbCommented:
cpatte7372,

Ouch, that was a nasty bug in a multi-sheet book.

The "XLUp" approach only works as long as none of the other columns of interest has used cells beneath column A's last one. (Or else has to be done for each column of interest).

"UsedRange" could be used but it has its own dangers as it would ignore any blank rows at the top of the sheet.

FWIW, I believe that rorya's is the correct way to go.

Cheers,
Brian.
0
 
StephenJRCommented:
That code is equivalent to going to the very last cell (rows.count) and then pressing the up arrow. Excel will stop at the first filled cell it finds which will therefore be the last non-blank cell in that column.

I think there are possible problems with SpecialCells as it doesn't reset until the document is saved and I think it can be affected by formatting. If you have several columns, in my humble opinion Find is the best method.
0
 
cpatte7372Author Commented:
OK, thanks Stephen
0
 
cpatte7372Author Commented:
broomee9

I have been in Thailand and I sadly forgot to allocate points before leaving.

My apologies guys.

0
 
cpatte7372Author Commented:
Thanks all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.