Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 355
Default Open file macro

Greetings
I need to modify this code slightly. The change I need to make is as
follows... The file that that will be opened has only 1 sheet (as opposed to
several) that is named datayyyymmdd. I need to modify the reference from
Forecast to looking at the first 4 characters of the sheetname or to sheet1.
I tried changing "forecast" to sheet1 and I got a Type Mismatch.

Private Sub CommandButton1_Click()
Dim myCell As Range
Dim myBook As Workbook
Dim i As Long
Dim r As Range, r1 As Range

With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With

With Application.FileSearch
.NewSearch
'Copy or move this workbook to the folder with
'the files that you want to summarize
.LookIn = ThisWorkbook.Path
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
If .FoundFiles(i) < ThisWorkbook.FullName Then
If InStr(1, .FoundFiles(i), "A.xls", vbTextCompare) Then
Set myBook = Workbooks.Open(.FoundFiles(i))
myBook.Worksheets("Forecast").Select
Set r = myBook.Worksheets("Forecast").Range("BP18:BU18")
Set r1 = ThisWorkbook.Worksheets(1). _
Range("B65536").End(xlUp)
If r1.Row = 1 Then Set r1 = r1.Offset(1, 0)
If Not IsEmpty(r1) Then Set r1 = r1.Offset(1, 0)
r.Copy Destination:=r1
myBook.Close SaveChanges:=False
End If ' Instr
End If ' not thisworkbook
Next i
End If
End With

With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With

ThisWorkbook.SaveAs Application.GetSaveAsFilename

End Sub

Thanks!!!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Open file macro

Hi Sandy

You can use something like this

Dim sh As Worksheet
For Each sh In mybook.Worksheets
If Left(sh, 4) = "data" Then
' code
Else
'do nothing
End If
Next


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Sandy" wrote in message ...
Greetings
I need to modify this code slightly. The change I need to make is as
follows... The file that that will be opened has only 1 sheet (as opposed to
several) that is named datayyyymmdd. I need to modify the reference from
Forecast to looking at the first 4 characters of the sheetname or to sheet1.
I tried changing "forecast" to sheet1 and I got a Type Mismatch.

Private Sub CommandButton1_Click()
Dim myCell As Range
Dim myBook As Workbook
Dim i As Long
Dim r As Range, r1 As Range

With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With

With Application.FileSearch
.NewSearch
'Copy or move this workbook to the folder with
'the files that you want to summarize
.LookIn = ThisWorkbook.Path
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
If .FoundFiles(i) < ThisWorkbook.FullName Then
If InStr(1, .FoundFiles(i), "A.xls", vbTextCompare) Then
Set myBook = Workbooks.Open(.FoundFiles(i))
myBook.Worksheets("Forecast").Select
Set r = myBook.Worksheets("Forecast").Range("BP18:BU18")
Set r1 = ThisWorkbook.Worksheets(1). _
Range("B65536").End(xlUp)
If r1.Row = 1 Then Set r1 = r1.Offset(1, 0)
If Not IsEmpty(r1) Then Set r1 = r1.Offset(1, 0)
r.Copy Destination:=r1
myBook.Close SaveChanges:=False
End If ' Instr
End If ' not thisworkbook
Next i
End If
End With

With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With

ThisWorkbook.SaveAs Application.GetSaveAsFilename

End Sub

Thanks!!!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Open file macro

Hi Sandy

Sorry, I see that I forgot .Name

If Left(sh.Name, 4) = "data" Then



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ron de Bruin" wrote in message ...
Hi Sandy

You can use something like this

Dim sh As Worksheet
For Each sh In mybook.Worksheets
If Left(sh, 4) = "data" Then
' code
Else
'do nothing
End If
Next


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Sandy" wrote in message ...
Greetings
I need to modify this code slightly. The change I need to make is as
follows... The file that that will be opened has only 1 sheet (as opposed to
several) that is named datayyyymmdd. I need to modify the reference from
Forecast to looking at the first 4 characters of the sheetname or to sheet1.
I tried changing "forecast" to sheet1 and I got a Type Mismatch.

Private Sub CommandButton1_Click()
Dim myCell As Range
Dim myBook As Workbook
Dim i As Long
Dim r As Range, r1 As Range

With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With

With Application.FileSearch
.NewSearch
'Copy or move this workbook to the folder with
'the files that you want to summarize
.LookIn = ThisWorkbook.Path
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
If .FoundFiles(i) < ThisWorkbook.FullName Then
If InStr(1, .FoundFiles(i), "A.xls", vbTextCompare) Then
Set myBook = Workbooks.Open(.FoundFiles(i))
myBook.Worksheets("Forecast").Select
Set r = myBook.Worksheets("Forecast").Range("BP18:BU18")
Set r1 = ThisWorkbook.Worksheets(1). _
Range("B65536").End(xlUp)
If r1.Row = 1 Then Set r1 = r1.Offset(1, 0)
If Not IsEmpty(r1) Then Set r1 = r1.Offset(1, 0)
r.Copy Destination:=r1
myBook.Close SaveChanges:=False
End If ' Instr
End If ' not thisworkbook
Next i
End If
End With

With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With

ThisWorkbook.SaveAs Application.GetSaveAsFilename

End Sub

Thanks!!!





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Excel file from VB and open MACRO no1jimmyman Excel Discussion (Misc queries) 0 February 14th 11 10:10 PM
2007 Macro to Open File, Delete Contents, Save New File Flintstone[_2_] Excel Discussion (Misc queries) 2 February 1st 10 11:25 PM
Open a file do a macro ( made) and open next succesive file SVTman74 Excel Programming 5 April 21st 06 10:14 PM
Macro to call a file that has a auto open macro in the file itself [email protected] Excel Programming 1 August 5th 05 06:39 AM
Automate open file, update links, run macro, close and save file Geoff[_7_] Excel Programming 2 August 26th 03 10:13 PM


All times are GMT +1. The time now is 05:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"