Hello,
Below is a macro which largely came from a sample CD provided with a book I bought. Programming this code would have been beyond me.
The macro is in a file which I save into the same folder that my estimate files are in. There are usually 5 to 30 estimate files.
When I run this macro, it ripples thru all the files and sets a gross margin value. (Reads in the file list according to a couple criteria, opens the file, sets the value, saves and closes the file.)
For some reason, it will work fine when I first create the "setting" file (I'll call it) and I can continue to use it over and over, but then, for some reason, it will stop working. I mean, it runs, but doesn't do anything. No files open etc. No errors result.
When I rem'd out the "On error resume next" I got a message that it could not find the file. But the filename presented was 100% correct. In stepping thru the macro, I could see that it did execute the While strFilename... loop the proper number of times (if that helps).
Is something not getting flushed / initialized / re-initialized... ? I'm lost.
Thanks in advance. (Using Excel 2003 on Windows XP.)
Below is a macro which largely came from a sample CD provided with a book I bought. Programming this code would have been beyond me.
The macro is in a file which I save into the same folder that my estimate files are in. There are usually 5 to 30 estimate files.
When I run this macro, it ripples thru all the files and sets a gross margin value. (Reads in the file list according to a couple criteria, opens the file, sets the value, saves and closes the file.)
For some reason, it will work fine when I first create the "setting" file (I'll call it) and I can continue to use it over and over, but then, for some reason, it will stop working. I mean, it runs, but doesn't do anything. No files open etc. No errors result.
When I rem'd out the "On error resume next" I got a message that it could not find the file. But the filename presented was 100% correct. In stepping thru the macro, I could see that it did execute the While strFilename... loop the proper number of times (if that helps).
Is something not getting flushed / initialized / re-initialized... ? I'm lost.
Code:
Sub Set_Gross_Margin()
Dim dblPct As Double
Dim strPath As String, strPrj As String, strFilename As String
Dim wbkCurr As Workbook
Dim Repsonse As VbMsgBoxResult
Response = MsgBox("This Macro will apply the GROSS MARGIN value to" & vbCrLf & _
"all files within the folder that match the criteria." & vbCrLf & _
"It is recommended that ALL files are SAVED and CLOSED first!" & vbCrLf & _
" " & vbCrLf & _
"Do you want to run the Macro?", _
vbYesNo + vbInformation + vbDefaultButton2, "Apply GROSS MARGIN value")
If Response = vbNo Then Exit Sub
Application.Calculate
dblPct = Range("GM_TGT_INPUT") 'Pick up the value for Gross Margin
strPrj = "*" & Range("PRJ_NO") 'Pick up the Job or Quote number, just to help limit the files this macro will act on.
strPath = ThisWorkbook.Path & "\" 'Pick up the Path and add the backslash.
strFilename = Dir(strPath & strPrj & "*.xls*") 'Restricted to only Excel workbook files.
'If no matches found, then exit the macro.
If strFilename = "" Then
MsgBox "No files found matching: " _
& strPath & strPrj & "*.xls*"
Exit Sub
End If
'Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
While strFilename <> ""
Set wbkCurr = Workbooks.Open(strFilename)
If Not wbkCurr Is Nothing Then
Range("PARA!GM_TGT").Value = dblPct
wbkCurr.Close SaveChanges:=True
End If
strFilename = Dir()
Wend
Set wbkCurr = Nothing
Application.EnableEvents = True
'Application.ScreenUpdating = True
End Sub
Thanks in advance. (Using Excel 2003 on Windows XP.)