Hello -
I am using Excel 2002 SP3 and I in the process of building a macro that will eventually do the following:
(1) Use a button control to allow a user to select a file - Procedur OpenSingleFile - I got that from the FAQ's so thanks
(2) Once opened there will be a procedure that will identify the appropriate macro to run based on particular cell values from the file that was just opened.
(3) Run the appropriate procedure passing in the filename from the opened file - in this case there are several different macros because there are varying file formats. The procedure copies specified data from the opened workbook in step 1 and pastes a sheet in the speadsheet that has the button.
(4) Once all the copy/paste process is done between the opened workbook and the main workbook create a csv file. I have not gotten to this part yet.
My issue, because I am still trying to get a grasp on all of this, is knowing how to reference the correct workbooks, correct worksheets and/or range within the worksheets.
Before creating step 1 mentioned above, I used Record Macros to create all the macros used in step 3. When doing that the code ended up with something like:
Now that I am building onto the project I need to be able to replace the 001 10-06 Spreadsheet emailed.xls and remittance.xls so that it goes to the correct workbook and/or worksheets. Below is the code I have so far.
Here is part of the revised Step 3 and where I am now having a problem
Thanks, in advance, for the guidance
P
I am using Excel 2002 SP3 and I in the process of building a macro that will eventually do the following:
(1) Use a button control to allow a user to select a file - Procedur OpenSingleFile - I got that from the FAQ's so thanks
(2) Once opened there will be a procedure that will identify the appropriate macro to run based on particular cell values from the file that was just opened.
(3) Run the appropriate procedure passing in the filename from the opened file - in this case there are several different macros because there are varying file formats. The procedure copies specified data from the opened workbook in step 1 and pastes a sheet in the speadsheet that has the button.
(4) Once all the copy/paste process is done between the opened workbook and the main workbook create a csv file. I have not gotten to this part yet.
My issue, because I am still trying to get a grasp on all of this, is knowing how to reference the correct workbooks, correct worksheets and/or range within the worksheets.
Before creating step 1 mentioned above, I used Record Macros to create all the macros used in step 3. When doing that the code ended up with something like:
Code:
ChDir "I:\Real Estate\Single Family\Nov 2006"
Workbooks.Open Filename:= _
"I:\Real Estate\Single Family\Nov 2006\001 10-06 Spreadsheet emailed.xls"
Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("remittance.xls").Activate
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("001 10-06 Spreadsheet emailed.xls").Activate
Application.CutCopyMode = False
Windows("remittance.xls").Activate
Range("E2").Select
Now that I am building onto the project I need to be able to replace the 001 10-06 Spreadsheet emailed.xls and remittance.xls so that it goes to the correct workbook and/or worksheets. Below is the code I have so far.
Code:
'Step 1
Sub OpenSingleFile()
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Filename As Variant
Dim RemitFileName As Workbook
Set wbMain = ThisWorkbook
' File filters
Filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
' Default Filter to *.*
FilterIndex = 3
' Set Dialog Caption
Title = "Select a Remittance File to Open"
' Select Start Drive & Path
ChDrive ("C")
ChDir ("C:\sfp\Nov 2006")
With Application
' Set File Name to selected File
Filename = .GetOpenFilename(Filter, FilterIndex, Title)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit on Cancel
If Filename = False Then
MsgBox "No file was selected."
Exit Sub
Else
Set RemitFileName = Workbooks.Open(Filename)
LoadName = RemitFileName.Name
End If
' Open File
'Workbooks.Open Filename
Call SelectMacro
End Sub
'Step 2
Sub SelectMacro()
'purpose is to search through the open workbook and run the appropriate macro based
'on the Loan Field and Ending Schedule Value Field
If Range("A6").Value = "InvLnNo" And Range("S6").Value = "END SCH BAL" Then
Call macAS
End If
End Sub
Here is part of the revised Step 3 and where I am now having a problem
Code:
Sub macAS()
'
Dim NetCellRng As Range
Dim GrossCellRng As Range
Application.ScreenUpdating = False
Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("remittance.xls").Activate '<--having this is copying into B2 of the page with the button rather than the third tab
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("LoadName").Activate '<- I think this will be a problem too
Thanks, in advance, for the guidance
P