+ Reply to Thread
Results 1 to 13 of 13

Macro to find user input date range and delete everything outside range

  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Macro to find user input date range and delete everything outside range

    Hi Guys,

    Wondering if someone could help me in developing a simple user interfacing macro. I have a column with dates of a specified range shown below:

    dateRange.PNG

    The date range can be an arbitrary range so could go from any start date to end date (most of the time its a 6/8 weeks). Lets say for arguments sake the date range goes from 31/12/2014 18:00 to 09/02/2015 18:00 (note date is UK format of dd/mm/yyyy). I would like the user to be asked for a start and end date range that they went - say 01/01/2015 to 31/01/2015. Once they have chosen the range the macro should delete everything BEFORE their selected date range (and shift cells up) and delete everything AFTER their selected date range. The date range is in increments of 10 minutes.

    I'm pretty new to the whole User input through InputBox concepts so any advice would be much appreciated! Especially what you do with these input values if you want to use them outside the Sub routine.

    Also, it would be EPIC if some fail/error handling was in there - so the macro would revert back to asking them to input the date range again IF
    - if they put a date range that is outside the original date range
    - if they put in a wrong date format

    I'm not asking for a full blown macro, just somewhere to start would be great! Let me know if you need the workbook uploaded.

    Cheers.

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    Here's something to get you started.

    Please Login or Register  to view this content.
    Especially what you do with these input values if you want to use them outside the Sub routine.
    You can use Functions to pass values outside the sub routine. A very simple example follows. If you want the input values to be "remembered" the next time the sub is run, you'll need to save them somewhere in the workbook.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    Hi Walruseggman (love the name),

    I tried to do a bit my self but I kept getting a Run Time:91 error

    Please Login or Register  to view this content.
    I've dimensioned and set the lastRow from a previous subroutine and it works fine. The error happens at this line:

    Please Login or Register  to view this content.
    I've looked around and some similar forum problems suggest to add "Set" before the sRow but this gives me a "Compile Error: Object Required" at the same line.

    If I don't use the "Set" before sRow/eRow, I think the issue is the fact that the Number format of the userInput in the Find function. I'm assuming the userInput is in Text format, and Find is looking for the text format in a range that is a Custom Date & Time format.

    I think deletion of rows before and after the date range will be easy enough...if I can just get past this error...
    Last edited by sudric; 02-12-2015 at 12:01 PM.

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    Set is only used when setting a variable equal to and object. You wouldn't use Set before sRow because your use of .Row makes sRow just an integer, not an object. You WOULD use Set if you were setting sRow equal to the Range, which is an object.

    Anyway, the error happens because you haven't defined what lastRow is in this sub. If you're saying that you did define it in another sub, then pass it into this sub. As it is now, you're passing in sDate and eDate; there's no reason to do that, because you immediately set their values with the input boxes.

  5. #5
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    I dimensioned lastRow as a GLOBAL variable at the start so it was available to this sub routine. As can be seen from the screenshot below, the variable lastRow does indeed have a value:

    lastRow.PNG

  6. #6
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    Hmm, I can't replicate the error. Would need to see the workbook, or an example workbook where the error also happens.

  7. #7
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    Sorry for the delay, here is the workbook.

    Note, I removed some code earlier that is basically just importing a CSV file and doing some formatting on it (colour cells backgrounds, multiple some constants etc.) and have thus redefined the lastRow variable in this code.
    Attached Files Attached Files
    Last edited by sudric; 02-13-2015 at 07:34 AM.

  8. #8
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    I still can't replicate the error. The line you say you get an error runs fine for me. See screenshot below, both sRow and eRow have a value.

    NoError.png

  9. #9
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    Now im confused...im still getting that error at sRow.

    I did notice that your end date format is "2/9/2015 5", while in mine it is "09/02/2015"...they are both 10 characters but yours removes the 0 from the day and month and switched the date format to US style. Could this have some kind of impact? All my dates (both data values in sheets & user inputs) are in UK date format: DD/MM/YYYY

  10. #10
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    Could be. Try forcing formatting on startDate and endDate like this:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    No good...wow this is annoying.

    I forced the format for startDate/endDate & sDate/eDate. The first two dates are just to show the user what the original start and end dates are. The second two dates are the user input dates.

    Getting the same error.

  12. #12
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Macro to find user input date range and delete everything outside range

    Fixed it!! I used the CDate function in VBA to convert the userinput variable to a date. Thus the FIND function was able to work properly as it was looking for a date within a range of dates. So, format was the issue.

    Thanks a lot for your help and advice!!

  13. #13
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Macro to find user input date range and delete everything outside range

    Great! Dates can be finicky, I know.

    Please mark this thread as [SOLVED], and any reputation (the * in the lower left of my posts) would be appreciated.

+ 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. Replies: 0
    Last Post: 12-10-2014, 11:30 AM
  2. User Input to Define Range in Autofill Macro
    By Chaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2014, 02:12 PM
  3. [SOLVED] Project Timeline with Dynamic Date Range Based on User Input
    By Rollinstone12 in forum Excel General
    Replies: 2
    Last Post: 10-28-2012, 10:07 AM
  4. Macro to define name range based on user input
    By truongn2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-03-2007, 08:53 PM
  5. [SOLVED] CF -- User input date range
    By jujube in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-27-2005, 04:50 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