Am I missing something in my range definition???

TDC21

Board Regular
Joined
Mar 31, 2017
Messages
97
Hey everyone, sorry, my VBA is pretty limited. I am trying to create a new update form by reverse engineering a working form. I am getting error '1004' "Select method of Range class failed". The error is from "rng.Select" but I dont see anything wrong with the code where the range is defined, IDK.

As a side note, I also have time stamp to record the entry date but if possible I would like to switch that out for a date picker but I can seem to find a good way to do that. Anything obvious I might be missing or is this not an option anymore.


Code

Private Sub CommandButton1_Click()


Dim oNewRow As ListRow
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("DATA_SHEET").Range("TempTbl")
rng.Select
Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
With ws
oNewRow.Range.Cells(1, 1).Value = Me.TempID
oNewRow.Range.Cells(1, 6).Value = Me.StatusC
oNewRow.Range.Cells(1, 2).Value = Now
End With


Me.TempID = ""
Me.StatusC = ""


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You cannot select a range on a sheet that is not the active sheet. Is DATA_SHEET the active sheet when your code runs?
 
Upvote 0
Pretty sure you'll have to define the worksheet and range separately.

Code:
Set ws = ThisWorkbook.Worksheets("DATA_SHEET")
Set rng = Range("TempTbl")

and really, if you're only using it once, like you're showing...you don't need all that. Just use:

Code:
ThisWorkbook.Worksheets("DATA_SHEET").Range("TempTbl").Select

And really, you don't need to select it anyway...just do what you want to do to it.

Code:
ThisWorkbook.Worksheets("DATA_SHEET").Range("TempTbl").Copy ' (or whatever)


EDIT: Guess I had it wrong about setting them separately. I didn't even think about it being the active sheet or not
 
Last edited:
Upvote 0
Thanks Rick, that is exactly what the issue was. The code was being called from a button on a separate sheet. I wish I was better and did not have to ask but is there a way to modify the code to allow this? It would be nice to keep users away from the source data being generated.
 
Upvote 0
I wish I was better and did not have to ask but is there a way to modify the code to allow this? It would be nice to keep users away from the source data being generated.
The post by jproffer had it wrong about setting the references, but he was right on target with his last comment... "And really, you don't need to select it anyway...just do what you want to do to it {the range}". A range is a defined object in VBA, so you do not have to select it in order to work with it... there are a couple of exceptions, but basically whatever you can do to the Selection object you can do directly to the object that you selected originally. In other words, whenever you see yourself doing this...

Sheets("Tabname").Range("SomeAddress").Select
Selection.Something....

you should do this instead...

Sheets("Tabname").Range("SomeAddress").Something...

You can omit the sheet reference if every the range is always going to be on the active sheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top