+ Reply to Thread
Results 1 to 8 of 8

Setting a range

  1. #1
    Registered User
    Join Date
    09-03-2011
    Location
    Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Setting a range

    Hi everyone,

    Here is what I am trying to do:
    Set a range to be from the first occurence of a value in col A to the last occurence of that same value in col A (offset three columns to the right).

    I have started with code by Ron DeBruin found here:
    http://msdn.microsoft.com/en-us/libr...omAllWorkbooks

    The instructions say:
    ' Change this range to fit your own needs.
    Please Login or Register  to view this content.
    How do I change this if I need to merge from 10 workbooks and the sourceRange needs to be from the FirstOccurence in col A of a user input date to the LastOccurence in col A of that same user input date (offset 3 columns to right) ??

    Thanks for the help.
    Last edited by pnmng49; 09-23-2012 at 01:44 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Setting a range

    attach please a sample file with current and desired situation
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Setting a range

    Hi, pnmng49,

    if you scroll down the link you provided you will find the solution for a range (given in the article itself):
    f you want to copy from cell A2 until the last cell on the worksheet then replace this code with the following code. You might do this if there are headers in the first row.
    Cc837974.note(en-us,office.12).gifNote:
    If you use this procedure, copy the function RDB_Last into your code module.

    First, add this line at the top of the macro.
    VB

    Dim FirstCell As String

    Then add this code.
    VB

    With mybook.Worksheets(1)
    FirstCell = "A2"
    Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3, .Cells))
    ' Test if the row of the last cell is equal to or greater than the row of the first cell.
    If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
    Set sourceRange = Nothing
    End If
    End With
    Function RDB_Last is right above the link.

    Does that solve your problem?
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    09-03-2011
    Location
    Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Setting a range

    Thanks for the reply, but no that doesn't solve my problem.
    There are 10 workbooks. The range will start and end at different cells for each workbook. The FirstOccurance and LastOccurance need to be found based on the user input date.
    Last edited by Cutter; 09-23-2012 at 11:13 AM. Reason: Removed whole post quote

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Setting a range

    Hi, pnmng49,

    I donīt know the structure of your workbooks but you could either apply the Autofilter with the dates supplied (please mind that Dates need to be transferred by CLng to work) and then copy the visible cells into the new workbook or create a whole range like written in the article and then compare each single value from the Dates to your given data and build a new range to copy later on or copy directly.

    If you need help with that please attach a sample workbook with some data as well as a copy of the destination workbook (if not a new one will be added).

    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    09-03-2011
    Location
    Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Setting a range

    TestAnn.xlsmTestCarmen.xlsmJacob.xlsmPassdown Test.xlsm

    Thanks for the replies. I have attached three workbooks with test data and the workbook from which the merge should run. The desired result is that if a user inputs the date 5/1/12 the new worksheet should show all the rows from each workbook that have entries for 5/1/12. Can the filter be set from the workbook running the merge? Thanks

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Setting a range

    Hi, pnmng49,

    looking at your workbooks Iīd tend to copy all into a new workbook and filter there.

    You need to adjust one line in the code as the first worksheet in every workbook seems to be "Logon" (change 1 to 2):
    Please Login or Register  to view this content.
    Code to be used on the new workbook (macro recordings slightly worked on):
    Please Login or Register  to view this content.
    Worked with Excel2007 on WinServer2003 for me.

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    09-03-2011
    Location
    Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Thumbs up Re: (SOLVED)Setting a range

    Thanks HaHoBe,

    It works great!

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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