+ Reply to Thread
Results 1 to 7 of 7

Vba code to sort variable ranges in one work sheet

  1. #1
    Registered User
    Join Date
    09-14-2004
    Posts
    21

    Vba code to sort variable ranges in one work sheet

    Hi , i am strugling to make thios to work, tried again and again allways failing
    I have a worksheet that have 4 diferent ranges that i need to sort everitime i insert a new row in a diferent range

    if i insert row in range " line1" i need to sort it by delivery date and quantity, and so on , i have 4 diferent ranges in the sheet
    line1
    line2
    line3
    line4

    atached example worksheet,
    All help is apretiated !
    Attached Files Attached Files
    Last edited by clixo; 07-15-2020 at 03:26 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,946

    Re: Vba code to sort variable ranges in one work sheet

    The code below will allow you to insert a new blank row, and then begin to fill it in. When you fill in the date, it will sort that block of data ascending by date and desending by amount. To get the code to work, remove all your other code, insert a new fully blank row 4, and then:

    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    09-14-2004
    Posts
    21

    Re: Vba code to sort variable ranges in one work sheet

    @Bernie Deitrick first of all thanks !
    The macro works but only im the test sheet i uploaded.
    In the main file it is not working, all of it is in portuguese but i will upload it anyway and if you could point me in the right direction i will be thankfull !
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,946

    Re: Vba code to sort variable ranges in one work sheet

    OK - each of your monthly sheets needs to have a new blank row inserted above your existing row 4, to allow the CurrentRegion command to operate correctly. I have assumed that you will have the twelve sheets for the months in the same location - as the 2nd through 13th sheets. Note that you really could just use one sheet for the entire task that you seem to be designing for, and apply filters to show the current month's data, or just the data for one line, all keyed to a few control cells at the top of the sheet, and an extra column of formulas to flag the correct data to show for that view.

    Anyway, once you have inserted the new row 4 on each sheet, you can use workbook level code so you only need one version of code:

    '1) Copy this code.
    '2) Open the VBE (press Alt-F11)
    '3) Select your workbook in the project explorer
    '4) Double-click the ThisWorkbook object
    '5) Paste the code into the window that appears.
    '6) Save the file as a macro-enabled .xlsm file.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-14-2004
    Posts
    21

    Re: Vba code to sort variable ranges in one work sheet

    Hi, Code is running ! My bad !


    _______________________________________________________________________________
    Hi.
    I can not make it run as suposed ! i have done as in the last post but the code simply do not run.
    __________________________________________________________________________________
    Last edited by clixo; 07-14-2020 at 07:08 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Vba code to sort variable ranges in one work sheet

    See if this is how you wanted.
    To ThisWorkbook(EsteLivro?) code module
    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-14-2004
    Posts
    21

    Re: Vba code to sort variable ranges in one work sheet

    @ Bernie Deitrick , Your code is working !!!! sory i was not setting it properly !!!!! thanks !!!!
    @jindon Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Testing single code, work fine. Put multiple codes in one sheet, one code doesn't work.
    By MayDay1988 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2017, 06:14 PM
  2. [SOLVED] Why DOESN'T this Sort Code work?
    By CaptMac in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2016, 02:42 PM
  3. Sort function with variable ranges
    By Vaniok in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-03-2009, 11:48 PM
  4. Compare ranges with VBA (code doesn't work)
    By rinser in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-25-2009, 12:30 PM
  5. Setting variable data-sort ranges
    By forcybo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-11-2008, 09:18 AM
  6. Automatic Sort for variable Ranges
    By bman342 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-27-2006, 03:55 PM
  7. [SOLVED] Counting variable ranges and auto-summing variable ranges
    By Father Guido in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2006, 11:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1