Question regarding opening of wb

mercmannick

Well-known Member
Joined
Jan 21, 2005
Messages
730
is it possible in vba ,

if u have to open a file on a daily basis through vba,to get some data but the filename changes like this

wbook 30.5.xls
wbook 31.1.xls
wbook 31.2.xls

(week 30 day 5, week 31 day 1, ETC)

is it possible to tell vba to open newest book, or is it possible to tell vba to open book by the date created ie value.date = today()

regards

Merc


:rolleyes:
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
How are your feelings towards a dialog box that you can use to select the newest file? Is this an option?
Code:
Sub GetIt()
    Dim vFile As Variant
    vFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    If vFile <> False Then
        Workbooks.Open CStr(vFile)
    End If
End Sub
 
Upvote 0
Justinlabenne


yes tht would be ok how would i incorporate this to the existing code


Thanks

Merc
 
Upvote 0
What is your existing code?

An inputbox can probably be worked out, but also not as reliable as using getopenfilename because your typing, not selecting.
 
Upvote 0
Code:
Sub GetRedCells()
Dim strMyBook As String
Dim cell As Range
Dim TempBook As Workbook


   On Error GoTo ErrorHandler

    Application.ScreenUpdating = False
    Set TempBook = Workbooks.Add
    
    'Change line below to suit
    strMyBook = "I:\IMF stage starts wk 30.5.xls"         
    Workbooks.Open Filename:=strMyBook
    Range([A1], [IV1].End(xlToLeft)).Copy Destination:=TempBook.Sheets(1).Range("A1")
    
    ' Change Criteria1 to suit
    Range("D:D").AutoFilter Field:=4, Criteria1:="S03E"
    
    For Each cell In Range("E:E").SpecialCells(xlCellTypeVisible)
        If cell.Interior.ColorIndex = 3 Then _
            cell.EntireRow.Copy Destination:=TempBook.Sheets(1).Range("A65536").End(xlUp).Offset(1, 0)
    Next cell
    
    ActiveWorkbook.Close False
    Application.ScreenUpdating = True
Range("A:P").EntireColumn.AutoFit
Range("J1").EntireColumn.AutoFit
   On Error GoTo 0
   Exit Sub

ErrorHandler:

    MsgBox "Error: " & Err.Number & " (" & Err.Description & ")"
End Sub


"I:\IMF stage starts wk 30.5.xls"  this is the part i need it to be able to pick up newest file .

was hoping there would be a way to do by the date the file was created( if it was created today then open it ) or a inputbox where you can just put in the week no and day no

as the name of the file allways stays the same just the week and day no on end changes everyday


Thanks


Merc
 
Upvote 0
Code:
Option Explicit

Sub GetRedCells()
Dim cell As Range
Dim TempBook As Workbook


   On Error GoTo ErrorHandler

    Application.ScreenUpdating = False
    Set TempBook = Workbooks.Add
   
    'Change line below to suit
    Dim vFile As Variant
    vFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    If vFile <> False Then
        Workbooks.Open CStr(vFile)
    End If

    Range([A1], [IV1].End(xlToLeft)).Copy Destination:=TempBook.Sheets(1).Range("A1")
   
    ' Change Criteria1 to suit
    Range("D:D").AutoFilter Field:=4, Criteria1:="S03E"
   
    For Each cell In Range("E:E").SpecialCells(xlCellTypeVisible)
        If cell.Interior.ColorIndex = 3 Then _
            cell.EntireRow.Copy Destination:=TempBook.Sheets(1).Range("A65536").End(xlUp).Offset(1, 0)
    Next cell
   
    ActiveWorkbook.Close False
    Application.ScreenUpdating = True
Range("A:P").EntireColumn.AutoFit
Range("J1").EntireColumn.AutoFit
   On Error GoTo 0
   Exit Sub

ErrorHandler:

    MsgBox "Error: " & Err.Number & " (" & Err.Description & ")"
End Sub
 
Upvote 0
Justin Labenne

thanks m8 it works,

can u show me the inputbox way as well if not too much trouble


Regards

Merc
 
Upvote 0

Forum statistics

Threads
1,217,371
Messages
6,136,158
Members
449,995
Latest member
rport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top