View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Baine Baine is offline
external usenet poster
 
Posts: 9
Default PUBLIC VARIABLE for SELECTEDFILE

Thanks for the help Tom. I tried your code and was unable to do any better
on the main question I had. How do I refer to the file that was opened in
the first sub when I use the second sub in the same module?

"Tom Ogilvy" wrote in message
...
Option Explicit
Public selectedFile As String
' Public selectedFile As Workbook
' Brings up dialog to select invoice file to
' copy data to Master file.

Sub openInvoiceFile()
Dim selectedFileFullName As String
Dim wbToOpen As Integer, wsCount As Integer
Dim objWsQBRfreeB As String
For wbToOpen = 1 To 1
'display dialog asking user to select a file
selectedFileFullName = Application.GetOpenFilename("Files (*.xls),*.xls",
, "Select
Invoice", , False)
'check if cancel selected then open
If selectedFile = "False" Then
MsgBox "You choose to interrupt loading files."
Exit For
Else
Workbooks.OpenText Filename:=selectedFileFullName
SelectedFile = ActiveWorkbook.Name
End If
' Workbooks(2).Activate
ThisWorkbook.Activate
Next
End Sub

--
Regards,
Tom Ogilvy


"Baine" wrote in message
...
I have one sub that opens the file selection dialog. Then I want to copy
some cells from the Excel file that was just opened to the MasterData.XLS
file where the Module resides. I don't know what the file name might be
but I need to refer back to that file on several occations to copy all of
the cells. The data needed in not allways in the same row or column. I
know how to do the coding to locate the data and copy it. How do I refer
to the file that was opened in the first sub?

Option Explicit
Public selectedFile As String
' Public selectedFile As Workbook
' Brings up dialog to select invoice file to
' copy data to Master file.

Sub openInvoiceFile()
Dim selectedFile As String
Dim wbToOpen As Integer, wsCount As Integer
Dim objWsQBRfreeB As String
For wbToOpen = 1 To 1
'display dialog asking user to select a file
selectedFile = Application.GetOpenFilename("Files (*.xls),*.xls", ,
"Select Invoice", , False)
'check if cancel selected then open
If selectedFile = "False" Then
MsgBox "You choose to interrupt loading files."
Exit For
Else
Workbooks.OpenText Filename:=selectedFile
End If
' Workbooks(2).Activate
Next
End Sub
--------------------------------------------------------------------------------
Sub CopyDATAtoMaster()
'
' CopyDATAtoMaster Macro
' Macro recorded 10/6/2006 by Baine-64
'
Range("B2").Select
Selection.Copy
Windows("S2 Master1.xls").Activate
Range("C5").Select
ActiveSheet.Paste
' Windows(selectedFile).Activate
' Set selectedFile = ActiveWorkbook
' selectedFile.Activate
End Sub