Solved

vba(excel) run time error 1004

Posted on 2004-08-10
3
1,437 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 20 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

19 Experts available now in Live!

Get 1:1 Help Now