PC Review


Reply
Thread Tools Rate Thread

How do I change the row/column format in a macro to beyond letters?

 
 
LunaMoon
Guest
Posts: n/a
 
      4th Aug 2008
Hi all,

I recorded a very long VBA macro in Excel and the index of ranges is
in letter format,

for example, "M14:M19", etc.

Now I am going to run this macro programmatically and automatically in
a for loop and expand it from the left to the right so I want to
change the "M" in the above example automatically.

But after 26 letters, there will be AA, AB, etc. which is really hard
to program.

Is there a way to adapt the recorded macro (by changing as little as
possible) to more than 26 letters.

I really want to change as little as possible because I spent lots of
time recording this macro and it is really long and it has reference
to addresses such as above everywhere... it's going to be a total mess
if I try to change too much of its addressing...

Thanks!
 
Reply With Quote
 
 
 
 
Proof Of Concept
Guest
Posts: n/a
 
      4th Aug 2008
This function will convert an integer to a column letter, so if you are
looping through columns, say 1...100 you can do the conversion that way:

' e.g. GetColLetter(100)
Function GetColLetter(ByVal iCol As Integer) As String
Dim strAddr As String
Dim iPos As Integer
strAddr = Mid(ThisWorkbook.Worksheets(1).Cells(1, iCol).Address, 2)
iPos = InStr(strAddr, "$")
GetColLetter = Left$(strAddr, iPos - 1)
End Function

Or modify the range directly

Range("M14:M19")
becomes
Range(Cells(14,"M"),Cells(19,"M"))
which is the equivalent of
Range(Cells(14,13),Cells(19,13))

e.g. to loop through 100 columns

Dim y as Integer, rng As Range
For y = 1 to 100
Set rng = Range(Cells(14,x),Cells(19,x))
Next


--
Tim Zych
www.higherdata.com
Compare data in Excel and find differences with Workbook Compare
A free, powerful, flexible Excel utility
Now with Table Compare for quick table comparisons




"LunaMoon" <(E-Mail Removed)> wrote in message
news:391f0fd9-1254-4f5a-aee2-(E-Mail Removed)...
> Hi all,
>
> I recorded a very long VBA macro in Excel and the index of ranges is
> in letter format,
>
> for example, "M14:M19", etc.
>
> Now I am going to run this macro programmatically and automatically in
> a for loop and expand it from the left to the right so I want to
> change the "M" in the above example automatically.
>
> But after 26 letters, there will be AA, AB, etc. which is really hard
> to program.
>
> Is there a way to adapt the recorded macro (by changing as little as
> possible) to more than 26 letters.
>
> I really want to change as little as possible because I spent lots of
> time recording this macro and it is really long and it has reference
> to addresses such as above everywhere... it's going to be a total mess
> if I try to change too much of its addressing...
>
> Thanks!



 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      4th Aug 2008
Let VB do all the hard work...

Addr = "G5:CD10"
FirstColumn = Range(Left(Addr, InStr(Addr, ":") - 1)).Column
SecondColumn = Range(Mid(Addr, InStr(Addr, ":") + 1)).Column

Rick


"LunaMoon" <(E-Mail Removed)> wrote in message
news:391f0fd9-1254-4f5a-aee2-(E-Mail Removed)...
> Hi all,
>
> I recorded a very long VBA macro in Excel and the index of ranges is
> in letter format,
>
> for example, "M14:M19", etc.
>
> Now I am going to run this macro programmatically and automatically in
> a for loop and expand it from the left to the right so I want to
> change the "M" in the above example automatically.
>
> But after 26 letters, there will be AA, AB, etc. which is really hard
> to program.
>
> Is there a way to adapt the recorded macro (by changing as little as
> possible) to more than 26 letters.
>
> I really want to change as little as possible because I spent lots of
> time recording this macro and it is really long and it has reference
> to addresses such as above everywhere... it's going to be a total mess
> if I try to change too much of its addressing...
>
> Thanks!


 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      4th Aug 2008
On Sun, 3 Aug 2008 17:17:29 -0700 (PDT), LunaMoon <(E-Mail Removed)>
wrote:

>Hi all,
>
>I recorded a very long VBA macro in Excel and the index of ranges is
>in letter format,
>
>for example, "M14:M19", etc.
>
>Now I am going to run this macro programmatically and automatically in
>a for loop and expand it from the left to the right so I want to
>change the "M" in the above example automatically.
>
>But after 26 letters, there will be AA, AB, etc. which is really hard
>to program.
>
>Is there a way to adapt the recorded macro (by changing as little as
>possible) to more than 26 letters.
>
>I really want to change as little as possible because I spent lots of
>time recording this macro and it is really long and it has reference
>to addresses such as above everywhere... it's going to be a total mess
>if I try to change too much of its addressing...
>
>Thanks!


Are you looping through one column at a time, or are you really "expanding" the
range to the right, so that first you go through one column (M14:M19), then two
columns (M14:N19), etc?

To loop through one column at a time, use the Offset property:

==========================
Option Explicit
Sub foo()
Dim rAddr As Range
Dim i As Long
Set rAddr = Range("M14:M19")

For i = 1 To 30 'loop through 30 columns
Set rAddr = rAddr.Offset(0, 1)
'do whatever
'for example
Debug.Print rAddr.Address
Next i
End Sub
==========================

To "expand" the range so it is multiple columns, use the Resize property:

==========================
Option Explicit
Sub foo()
Dim rAddr As Range
Dim i As Long
Set rAddr = Range("M14:M19")

For i = 1 To 30 'expand from 1 to 30 columns
Set rAddr = rAddr.Resize(columnsize:=i)
'do whatever
'for example
Debug.Print rAddr.Address
Next i
End Sub
=======================
--ron
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change the row/column format in a macro to beyond letters? LunaMoon Microsoft Excel Discussion 3 4th Aug 2008 03:48 AM
How do I change the row/column format in a macro to beyond letters? LunaMoon Microsoft Excel Misc 3 4th Aug 2008 03:48 AM
change column numbering to column letters in my spreadsheet view ferdla Microsoft Excel Misc 1 29th Apr 2008 03:34 PM
How do change a column of data in capitol letters to small letters =?Utf-8?B?QmFyYiBQLg==?= Microsoft Excel Misc 6 15th Nov 2006 06:17 PM
How can I change column numbers back to column letters? =?Utf-8?B?U3BhY2UgRWxm?= Microsoft Excel Worksheet Functions 3 2nd Mar 2006 09:35 PM


Features
 

Advertising
 

Newsgroups
 


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