ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can I use a variable in a Range statement (https://www.excelbanter.com/excel-programming/363373-can-i-use-variable-range-statement.html)

Alfarata

Can I use a variable in a Range statement
 

I have tried to use a variable in this Range satement but I get a Select
Method of Range Class Failed. I need to reformat this as I move data.
X is a changing row variable.
Any help would be greatly appreciated
Thanks
Bill
Dim Temp as String
Temp = "O" & X

If TopInches < 10 Then
Range(Temp).Select
Selection.NumberFormat = "## ""' "" 0 ##/##"
Else
Range(Temp).Select
Selection.NumberFormat = "## ""' "" #0 ##/##"
End If


--
Alfarata
------------------------------------------------------------------------
Alfarata's Profile: http://www.excelforum.com/member.php...o&userid=34360
View this thread: http://www.excelforum.com/showthread...hreadid=548833


malik641[_31_]

Can I use a variable in a Range statement
 

You mean something like this?


Code
-------------------
Sub test1()
Dim Temp As String
Dim X As Integer
Dim TopInches As Integer
X = 1
Temp = "O" & X

For TopInches = 1 To 10
If TopInches < 10 Then
Range(Temp).NumberFormat = "## ""' "" 0 ##/##"
Else
Range(Temp).NumberFormat = "## ""' "" #0 ##/##"
End If

X = X + 1
Temp = "O" & X
Next
End Su
-------------------

--
malik64

-----------------------------------------------------------------------
malik641's Profile: http://www.excelforum.com/member.php...fo&userid=2419
View this thread: http://www.excelforum.com/showthread.php?threadid=54883


Alfarata[_2_]

Can I use a variable in a Range statement
 

Thanks that worked

any thoughts on the formating if TopInches is 10 then I was going to
format using this:Range(Temp).NumberFormat = "## ""' "" #0 ##/##"

But I get the wrong result 161' 03/4
It shold be 16' 10 3/4

Thanks bill


--
Alfarata
------------------------------------------------------------------------
Alfarata's Profile: http://www.excelforum.com/member.php...o&userid=34360
View this thread: http://www.excelforum.com/showthread...hreadid=548833



All times are GMT +1. The time now is 10:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com