If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Select Range questions on a macro

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-06-05, 08:34
palto1 palto1 is offline
Registered User
 
Join Date: Mar 2004
Posts: 68
Select Range questions on a macro

Hello,
I have a sheet that contains a "sort and concatenate" macro. This macro selects three columns and then sorts and concatenates these columns into a new columns. I am trying to figure out how to have the WHOLE columns selected. What do I put as the range for the columns?

Here is the code:

Sub SortCaseCaptionandConcatenate()
'
' SortCaseCaptionandConcatenate Macro
' Macro recorded 11/18/2005 by palto1
'
Cells.Select
Application.CutCopyMode = False
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A2").Select
ActiveCell.FormulaR1C1 = "=RC[1]&"" | ""&RC[2]&"" | ""&RC[3]"
Range("A2").Select
Selection.AutoFill Destination:=Range("??:??")
Range("??:??").Select
Range("A2").Select
End Sub

The "??:??" indicates the area that I need help with. I would like to not have to specify a set number of rows. I would like the macro to look for all rows where there is data. This will be different each time the macro is run.

Thank you in advance for your help.

Grace
Reply With Quote
  #2 (permalink)  
Old 12-06-05, 09:36
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
You can define the range initially or you can use Resize. For instance the following would expand the selection by two columns.

Code:
Cells(2,1).Resize(0,2)
BTW, no need (normally) to select a range in order to do something with it.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #3 (permalink)  
Old 12-12-05, 07:59
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
Ok a bit late i realise but ive rewritten your code for you
i think this is the kind of thing your after,

Code:
Sub SortCaseCaptionandConcatenate()
'
' SortCaseCaptionandConcatenate Macro
' Macro recorded 11/18/2005 by palto1
'

Dim rRange As Range
    
    Set rRange = Cells.Find(what:="*", searchdirection:=xlPrevious)
    If Not rRange Is Nothing Then
        Set rRange = Range(Cells(2, 1), Cells(rRange.Row, 1))
        Cells.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        rRange.FormulaR1C1 = "=RC[1]&"" | ""&RC[2]&"" | ""&RC[3]"
    End If

    Range("A2").Select
End Sub
if you have any comments pleasse just let me know ill be about for the next couple of days,

All the best
Dave
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On