Solved

Excel Run-time error '1004'

Posted on 2011-03-02
13
344 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel - Page layout - Margins 7 32
Excel formula 6 21
Macro 3 19
Changing absolute cell references 3 17
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now