Results 1 to 5 of 5

Thread: Newbie Question - help with code

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    Newbie Question - help with code

    Hi All,

    I'm REALLY new to visual basic. I'm creating a macro that will sort data on a large excel spreadsheet. The data is constantly changing (the number of rows change... number of columns is fixed). The sort will always be starting from the same point (row 12).

    What code will work for this? Will a Do Loop work? How do I write the code so that it will select all rows until there is no more data?

    Thanks so much for the help!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Newbie Question - help with code

    Welcome to the Forums.

    Something like this...

    VB Code:
    1. Range("A12:G12").Sort Key1:=Range("A12"), Order1:=xlAscending, Header:=xlGuess, _
    2.     OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Newbie Question - help with code

    Hi,

    In fat, you could select all the data - "A12:IV65536" and put in enough keys to sort one column after another. Key1, as RobDog shows, is the first sort key, but you can add more. So, for example, you could sort by column A and where items in column A are the same, sort by column C and so on. Or by row, it doesn't matter.

    Not entirely sure what you mean by "Select all rows until there is no more data..." unless you mean that you are sorting by row instead of by column and you want to do all of them, in which case what I have said about selecting all the data and setting the appropriate paramter to sort by row will be sufficient.
    The Office VBA Help on the Sort method is pretty clear, and tells you all the possible options for the various parameters.

    HTH

    zaza

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    Re: Newbie Question - help with code

    Thanks for the speedy replies!

    I'm looking to select all rows with data starting with Row 12 (to be sorted later). So I wanted to select all rows until it hit a blank row. This is a large report where the number of rows changes (columns are fixed), so the code needs to take that into consideration.

    From there I would sort whatever I needed... I wasn't as concerned with the sort part as I was with the Selection part.

    Thanks again!!!

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Newbie Question - help with code

    Rob's Code will do your sort without need for selection. If you need the rows to actually be selected for another reason, this will do it:

    VB Code:
    1. Range("A12:G" & Range("A12").End(xlDown).Row).Select

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width