(Noob) Range Question + UserForm

Alphacsulb

Active Member
Joined
Mar 20, 2008
Messages
414
TextBox1 in my userform is a row I input a number into.

I'm having a problem identifying it as a range though. Here is my code:

Code:
Private Sub CommandButton1_Click()
  MyRowNumber = TextBox1.Value
  
[COLOR=Red]'Here is where the problem starts, it says compile error, syntax error[/COLOR]
Range("DU" & MyRowNumber:"IC" & MyRowNumber).Select
    Range("IC" & MyRowNumber).Activate
    Selection.ClearContents
  
  
 'This reference here works
 TextBox2.Text = Range("IB" & MyRowNumber).Value
End Sub
Second question is, what is it called when you reference the "MrRowNumber" like I did here?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You're missing some bits:

Range("DU" & MyRowNumber & ":IC" & MyRowNumber).Select

And note that you don't need to select anything:

Range("DU" & MyRowNumber & ":IC" & MyRowNumber).ClearContents

HTH,
 
Upvote 0
You're seeing that error because you need to include the colon within the quotes:
Code:
Range("DU" & MyRowNumber &":IC" & MyRowNumber).ClearContents
Although you could also use:
Code:
Range(Cells(MyRowNumber,125),Cells(MyRowNumber,237)).ClearContents
 
Upvote 0
Smitty, that worked thanks.

Here is a follow up, can I reference the MyRowNumber in different modules if it's already in this Private Sub, or do I need to state it in every macro/module?
 
Upvote 0
Here is a follow up, can I reference the MyRowNumber in different modules if it's already in this Private Sub, or do I need to state it in every macro/module?

Sure, just declare it as public outside of any procedures in a general module:

Public MyRowNumber as Long

Then you can use it anywhere in the workbook with out re-declaring it. You can also pass the value between subs. Check out "Lifetime and Scope of Variables" in the VB helpfile.
 
Upvote 0
It goes before any procedures, but after Option Explicit:

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Public</SPAN> MyRowNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> foo()<br><SPAN style="color:#007F00">'    yada</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,254
Members
452,900
Latest member
LisaGo

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