Populate Range from string

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I'm passing a Range to my Form via a Property
Code:
Dim Rng as Range
Public Property Let rRange(z as Range)
Set Rng= z
End Property
Then I attempt to populate that range (which is a single cell)
Code:
Private Sub cmdApply_Click()
Worksheets("Sheet1").Range(rng).Value = Me.txtTest
End Sub
I get error 1004 Application-defined or object-defined error
At this point rng.Address is $A$2 (which is correct) and txtTest is a valid string.

What am I doing wrong ? Thanks.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Are you working with classes? Properties are a class thing and I don't see where you are using a class. I think you probably just want to either declare rng as a global variable, or pass the rng variable to your cmd_ApplyClick function. I don't think there is any way given what you've provided, that your cmd_Apply_Click function can read the rng variable.
 
Upvote 0
Isn't a Userform a class?
I'm stepping though Sub cmdApply and I can debug.print rng.Address correctly.
Stepping also shows the Property being assigned.. You may well be right but I'm puzzled.
Yes, I could try other methods, but it'd be useful to know why this isn't working.
 
Upvote 0
Yeah, I'm not sure what you have going on. Classes are a different thing. Userform isn't a class. I'm surprised that you can read that variable, but whatever.

Maybe try this.

Code:
Private Sub cmdApply_Click()
Worksheets("Sheet1").Range(rng.address).Value = Me.txtTest
End Sub
 
Upvote 0
Yep, that works! Nicely done... thanks.
There's much I don't know but I have found Property Gets and Lets work in Userforms and are very
straightforward. I load the Form first, assign the Property, then Show it.
 
Upvote 0
Cool. Might use that in the future. Glad that we got the solution to your problem. The 'Range' method expects a string, not a range object. Pretty trivial kind of mistake that I've made 1K times.
 
Upvote 0
Yeah, I'm not sure what you have going on. Classes are a different thing. Userform isn't a class. I'm surprised that you can read that variable, but whatever.

Actually, a Userform is a Class with a user interface ... All that which applies to Classes also applies to userforms. Things like instanciation with the New keyword and defining Properties, Methods and Events.
 
Upvote 0
rng is a Range object, so all you should really need is:

Code:
Private Sub cmdApply_Click()
rng.Value = Me.txtTest
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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