Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vba(excel) run time error 1004

Posted on 2004-08-10
3
Medium Priority
?
1,470 Views
Last Modified: 2006-11-17
hi there
i have 2 programs
  i use the first one to test some code
   and the second is the entire code

now my code works good at the first program the code is here (it only delete the rows which has 0 at column 2)

dec = 0
For I = 24 To 233
    V = Sheet12.Cells(I, 2)
    If V = "0" Then
      Sheet12.Rows(I - dec).Select   'heres the problem
      Selection.Delete shift:=lup
      dec = dec + 1
    End If
Next I


but when i paste the same code  (i have the same object called sheet12 of course)
it says:
     Run time error 1004
   Select method of range class failed


:( please




0
Comment
Question by:BaTy_GiRl
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 80 total points
ID: 11765978
Hi BaTy_GiRl,

While there are better ways to do what you're doing, I'll just try and answer your question first off.  

It would give you that error if your Sheet12 object isn't selected first.  Could that be the problem?  

Now, a couple suggestions:
You may want to change that reference to Sheets("sheetname") instead of Sheet12
When deleting rows in a loop, it's easier to do  For i = 233 to 24 Step -1

Even if you stick to the Sheet12 reference, you can do the whole thing using

 Dim i As Long
 For i = 233 To 24 Step -1
  If Sheet12.Cells(i, 2) = "0" Then Sheet12.Rows(i).Delete shift:=xlUp
 Next i

If the last row in column B (233) portion of your code changes, you could change the For line to:
 For i = Sheet12.Range("B65536").End(xlUp).Row To 24 Step -1

Using the code this way, nothing is selected.  This will not only prevent 1004 errors like you have, but also makes it much quicker.

Matt
0
 

Author Comment

by:BaTy_GiRl
ID: 11767428


Thank you very much  Matt you have solved my life =)

of course do you know why vba  give that kind of errors, i was very confused because
in Visual basic those errors reference libraries when objects doesn`t exist


0
 
LVL 35

Expert Comment

by:mvidas
ID: 11767455
I think it gave that error just because the sheet wasn't selected.  Say you had Sheet1 selected, and you gave it the command Sheet12.Range("A1").Select, it wouldn't know what to do because you didn't first say Sheet12.Select. It wasn't a problem with the object, just the selection portion of the code
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

972 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