Solved

Excel Run-time error '1004'

Posted on 2011-03-02
13
348 Views
Last Modified: 2012-05-11
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





0
Comment
Question by:cpatte7372
  • 5
  • 2
  • 2
  • +2
13 Comments
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 167 total points
ID: 35018625
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
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 166 total points
ID: 35018630
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
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 35018640
If you are looking for the last used row in the sheet, here is some alternate code:

lngLastRow = shtImport.UsedRange.Rows.Count
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:StephenJR
ID: 35018641
Change it to

xLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlup).Row
0
 

Author Comment

by:cpatte7372
ID: 35019088
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35019118
Doesn't affect mine - I don't believe your code should assume that the sheet with the code is active when it calculates.
0
 

Author Comment

by:cpatte7372
ID: 35019175
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
 
LVL 26

Expert Comment

by:redmondb
ID: 35019485
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
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 167 total points
ID: 35019606
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
 

Author Comment

by:cpatte7372
ID: 35020247
OK, thanks Stephen
0
 

Author Comment

by:cpatte7372
ID: 35467618
broomee9

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

My apologies guys.

0
 

Author Closing Comment

by:cpatte7372
ID: 35467717
Thanks all
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question