Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select method of Range class failed via VBA

Tags:

This is the code that I'm currently working with, and I'm getting this problem. I'm novice at Excel and I can't figure out what's wrong.

Private Sub cmdRecord_Click() Sheets("BxWsn Simulation").Range("Result").Select //This is the line with the problem, as excel told me.     Selection.Copy     Sheets("Reslt Record").Select     Sheets("Reslt Record").Range("A5000").End(xlUp).Offset(1).Select     Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _         xlNone, SkipBlanks:=False, Transpose:=False     Sheets("CuCon Simulator").Select     Application.CutCopyMode = False     Range("Improvement").Select End Sub 

The error is Select method of Range class failed via VBA, Error 1004. Any ideas?

ETA:

So I just changed the code to

Sheets("BxWsn Simulation").Select Range("Result").Select 

I believe this is what you mean by making it active?

However I'm still getting Method 'Range' of object '_Worksheet' failed, error 1004.

like image 828
guesthouse123 Avatar asked Oct 11 '10 00:10

guesthouse123


People also ask

How do I select a range in Excel VBA?

Selecting a Single Cell Using VBARange(“A1”) tells VBA the address of the cell that we want to refer to. Select is a method of the Range object and selects the cells/range specified in the Range object. The cell references need to be enclosed in double quotes.

How do I fix VBA error?

Go to the VBA toolbar and click on Tools and then click on Options. In the Options dialog box, click on the General tab and make sure that within the 'Error Trapping' group, 'Break on Unhandled Errors' is checked.


1 Answers

I believe you are having the same problem here.
The sheet must be active before you can select a range on it.

Also, don't omit the sheet name qualifier:

Sheets("BxWsn Simulation").Select Sheets("BxWsn Simulation").Range("Result").Select 

Or,

With Sheets("BxWsn Simulation")   .Select   .Range("Result").Select End WIth 

which is the same.

like image 69
GSerg Avatar answered Sep 20 '22 09:09

GSerg