splitting string

rossbritton26

New Member
Joined
Jul 28, 2011
Messages
33
Hi,

i have a cell reference (cellref) which is a string from an input box,
what i want to do is to split the cell reference into the column reference and the row reference. The following code works until the row ref goes above 9 and the column ref goes beyond Z. Any ideas??

Code:
    Col = Format(Left$(cellref, 1))

    Row = Format(Right$(cellref, 1))
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I would probably do:-
Code:
col = Left(Range(cellref).Address(1, 0), InStr(Range(cellref).Address(1, 0), "$") - 1)
row = Range(cellref).row
assuming col is String and row is Long or Integer.

(In fact I would never give my VBA variables the same name as an Excel object, like Row, to avoid the possibility of confusion. (VBA copes, humans don't always!) I would have called it iRow or some other variation.)
 
Upvote 0
Hi Ross

Is the address absolute or relative?



P. S. I also think that it's bad practice to use the name of a property as the name of your variable.

Since the Row number is integer you could for ex. use:

Code:
Dim lRow as Long
 
lRow = ...
 
Upvote 0
Others have told you about using Row as a variable name. Here is another method to get what you asked for...

TheRow = Range(cellref).Row

TheColumn = Split(Range(cellref).Address, "$")(1)
 
Upvote 0
Thanks for the replies,

Ruddles i have used your idea as it worked perfectly, thank you!!

I have also changed my variable names!! Thank you both for the advice :)
 
Upvote 0
Be sure to test it against a wide variety of different inputs all the way from A1 to the row and column limits of your worksheet, with and without the $ before the row and/or column reference.

(Testing against a wide variety of input data is always wise when receiving code from another person. Never assume because a piece of code works in limited circumstances, that it will work in all scenarios.)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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