Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Nov 21, 2011, 09:50 AM
    VBA Question
    Each day I download excel files that I name with a partial date format: e.g. 11.20_data or 11.21_data.

    Currently my VBA code allows me to go and find each file and manipulate the content in it. However, I have to do that one by one. What I would prefer to do is tell VBA (perhaps with the use of an Inputbox) to perform the data manipulations based on a range of dates, rather than one at a time.

    For example, say I have 3 days worth of data that I have to manipulate: 11.20 - 11.22 (20, 21, & 22). Can I create an input box that will allow me to specify that date range (or any range for that matter) and go to the first filename (11.20_data) manipulate it and then go to the next filename (11.21_data)? Based upon the number of days in the range, I want VBA to loop through the number of days and find each file and stop once it's done.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Nov 23, 2011, 01:32 PM
    Set two variables to hold your dates. The use the TEXT() function in vba to feed the "modifed" date into the workbook.open command.

    Code:
    Option Explicit
    
    Sub DatedFiles()
    Dim firstDate As Date, secondDate As Date, Dt As Date
    Dim wbData As Workbook, fName As String
    
    firstDate = Application.InputBox("Enter the first date", "First Date", "11/20/2011", Type:=2)
    If firstDate = 0 Then Exit Sub
    secondDate = Application.InputBox("Enter the second date", "Second Date", "11/22/2011", Type:=2)
    If secondDate = 0 Then Exit Sub
    
    For Dt = Application.Min(firstDate, secondDate) To Application.Max(firstDate, secondDate)
        fName = "C:\MyDataFiles\" & Format(Dt, "MM.DD") & "_data"
        If Len(Dir(fName)) > 0 Then
            'open file and process
            Set wbData = Workbooks.Open(fName)
            'do stuff here
            
            
            wbData.Close False
        Else
            MsgBox "The date file: " & fName & " could not be found. Please make a note"
        End If
    Next Dt
    
    End Sub

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

VBA If Statement [ 4 Answers ]

Ok. Here's my problem. I have a spreadsheet that I use which filters on 6 different filter criteria and copies the result set of each filter criteria to a separate worksheet. However, on occasion there are no results found for a given criteria. What I want to do is when no results are found...

Can VBA do this for me? [ 7 Answers ]

Refer attachment; For sheet "SPPA" cell "D18" value to be recorded on sheet "HO plan" cell "T7", and this is where it gets nasty, as the date rolls over on the "SPPA" sheet cell "F1", so must the macro record the new value in the next cell for sheet "HO plan", ie: If "SPPA" sheet cell "F1"...

Can this be done using VBA? [ 5 Answers ]

Ok - I have a spreadsheet that is exported from an application I use at work. It formats the data in a manner that makes it really difficult to manipulate but I think I've worked out a solution that gets me what I need; only it is very tedious. The data starts in row 16 of the sheet. There is...

VBA Issue [ 2 Answers ]

Ok, so here's what I am trying to do. I am trying to use a variable sheet name as a Named Range in my procedure but it is not working. Here's the part of my code that is failing: ActiveWorkbook.Names.Add Name:=NameRange1, RefersToR1C1:= _ ...

VBA Question regarding Headers [ 1 Answers ]

Ok, so here's my problem - I'm trying to customize a Header to be displayed in different workbooks. I want the Header to contain the name of the Worksheet as well as the current year. The header will read this way: Month of August 2010 For the purposes of my code, I have the following...


View more questions Search