+ Reply to Thread
Results 1 to 9 of 9

Automatic Range Selection Macro?

  1. #1
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Automatic Range Selection Macro?

    Good morning,

    I was wondering if someone could help me with a macro that automatically selects the following ranges for each "block" of data delimited by cells in column A. I have my own macro that will copy each "block" to a new workbook and save it as its own file (once the range is selected; see below), but I want to automate the process of selecting each data "block" down column A.

    Ideally, the "Automatic Range Selection Macro" will:

    1. Select block 1,
    Block 1
    Range selection 1.PNG

    2. Run my "Move selected range to new workbook & save" macro (see code below).

    3. And then continue automatically to block 2 etc...until the last block is selected, copied and saved.
    Range Selection 2.PNG

    "Move selected range to new workbook & save" macro

    Sub CopyandSave()

    Dim Path As String
    Dim filename As String
    Dim xWs As Worksheet
    Dim rng As Range

    Set rng = Application.Selection
    Application.Workbooks.Add
    Set xWs = Application.ActiveSheet
    rng.Copy Destination:=xWs.Range("A1")

    Path = "DESTINATION FOLDER\"
    filename = Range("C2")
    ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    ActiveWorkbook.Close

    End Sub

    M

  2. #2
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Automatic Range Selection Macro?

    May be
    Please Login or Register  to view this content.
    Regards,
    MohanS


    "Perfection is not attainable, but if we chase perfection we can catch excellence." - Vince Lombardi

    You can simply say thanks by clicking "*Add Reputation" icon

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Automatic Range Selection Macro?

    Replace this

    Please Login or Register  to view this content.
    With

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Automatic Range Selection Macro?

    Quote Originally Posted by :) Sixthsense :) View Post
    Replace this

    Please Login or Register  to view this content.
    With

    Please Login or Register  to view this content.
    I placed this into my code in the OP. When I complied I got: "variable not defined" for r. Is r long or an integer? Thanks

    M

  5. #5
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Automatic Range Selection Macro?

    Quote Originally Posted by mohan.r1980 View Post
    May be
    Please Login or Register  to view this content.
    Thanks for quick reply, I couldn't get this code to work though.

    M

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Automatic Range Selection Macro?

    Quote Originally Posted by excel_novice2019 View Post
    I placed this into my code in the OP. When I complied I got: "variable not defined" for r. Is r long or an integer?
    r As Range

  7. #7
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Automatic Range Selection Macro?

    Quote Originally Posted by :) Sixthsense :) View Post
    r As Range
    I appreciate the help!

    It ran but I received run-time error '91': Object variable or With block variable not set at the underlined line below:

    Sub Super()
    Dim xWs As Worksheet
    Dim rng As Range, iCl As Long, lRw As Long
    Dim r As Range

    lRw = Range("A" & Rows.Count).End(xlUp).Row
    iCl = Cells(1, Columns.Count).End(xlToLeft).Column

    For Each r In Range("A1:A" & lRw).SpecialCells(xlCellTypeBlanks).Areas
    r.Resize(r.Rows.Count + 1, iCl).Copy
    Next r
    Application.Workbooks.Add
    Set xWs = Application.ActiveSheet
    rng.Copy Destination:=xWs.Range("A1")
    End Sub

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Automatic Range Selection Macro?

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Automatic Range Selection Macro?

    Quote Originally Posted by :) Sixthsense :) View Post
    Please Login or Register  to view this content.
    This is perfect! Thanks sixth sense!

+ 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. Automatic range selection based on adjacent formula
    By entoptics in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-01-2015, 09:17 PM
  2. Replies: 0
    Last Post: 12-10-2014, 11:30 AM
  3. [SOLVED] How to get selection range in Macro?
    By Cuibi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-28-2013, 05:23 AM
  4. Range selection in macro
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2013, 07:30 PM
  5. [SOLVED] VBA PivotTable Automatic Range-selection
    By Brullie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2013, 06:53 AM
  6. Automatic range selection
    By mroctogon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-08-2008, 06:01 PM
  7. Range selection-a macro on the net
    By m1ke in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-17-2006, 12:42 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