Solved

Excel Run-time error '1004'

Posted on 2011-03-02
13
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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