My below code was all running perfectly fine when I had it all in one function, now that I separated it into a different function for better readability and easier modifications, it's stopping after the function call. Does anyone know why its doing this?
If (Not (Find_First("ERR", Sheets("Consolidated_Data850").Range("G:G")))) Then
Debug.Print "No data errors found!"
Call SaveFile(TempFilePath & "ValidPOs.xls")
' LR = Sheets("Consolidated_Data850").Range("A" & Rows.Count).End(xlUp).row
' Debug.Print "Saving file to... " & Chr(13) & SaveFilePath & "\ValidPOs.xls"
' If Dir(SaveFilePath & "\ValidPOs.xls") <> "" Then Kill SaveFilePath & "\ValidPOs.xls"
' ActiveWorkbook.SaveAs FileName:=SaveFilePath & "\ValidPOs.xls", FileFormat:=51
' Call GenerateEmail(TempFolder & ValidFileName, _
' SaveFilePath & "\ValidPOs.xls", _
' Sheets("Consolidated_Data850").Range("A:F" & LR))
"(" & MarksFolder & ValidTemplate & ") using Range(" & Range("A:F" & LR).Address & ")"
Call GenerateEmail(MarksFolder & ValidTemplate, _
TempFilePath & "ValidPOs.xls", _
Sheets("Consolidated_Data850").Range("A:F" & LR))
Else
Debug.Print "Found errors"
End If
New function that replaced the comment section above...
Private Sub SaveFile(FileNamePath As String)
LR = Sheets("Consolidated_Data850").Range("A" & Rows.Count).End(xlUp).row
Debug.Print "Savingfile to... " & FileNamePath
If Dir(FileNamePath) <> "" Then Kill FileNamePath
On Error GoTo ErrHandler:
Debug.Print "Generating Email (" & TempFilePath & "ValidPOs.xls" & ")" & Chr(13) & _
ActiveWorkbook.SaveAs FileName:=FileNamePath, FileFormat:=51
Exit Sub
ErrHandler:
Debug.Print "Error # " & Str(Err.Number) & " was generated " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
End Sub
Everything works perfectly, but once it leaves that second function (after the save) it stops and does not continue the code
EDIT
It seems as thought the code just stops here, and it's NOT calling the function
Call SaveFile(TempFilePath & "ValidPOs.xls")
Are you sure that TempfilePath is a valid path and that it ends with a "\"?
If you step through with F8 does it highlight the End Sub before stopping or does the yellow highlight vanish from ActiveWorkbook.saveas without moving on?
Check the date and time last saved for your activeworkbook - is it actually being saved? My money is on some error here - out of disk space, invalid path or similar. But if not - and this is pretty random but replace the call - just use
SaveFile TempFilePath & "ValidPOs.xls"
(Note no brackets if you omit the call) - I have had it sometimes work with the shorthand when it's failed on the formal syntax.
Edited in reponse to code changes: You need to Exit Sub before your error handler
Private Sub SaveFile(FileNamePath As String)
LR = Sheets("Consolidated_Data850").Range("A" & Rows.Count).End(xlUp).row
Debug.Print "Savingfile to... " & FileNamePath
If Dir(FileNamePath) <> "" Then Kill FileNamePath
On Error GoTo ErrHandler:
Debug.Print "Generating Email (" & TempFilePath & "ValidPOs.xls" & ")" & Chr(13) & _
ActiveWorkbook.SaveAs FileName:=FileNamePath, FileFormat:=51
EXIT SUB ' if you miss this your error handler below is run irrespective of
' whether there's an error or not.
ErrHandler:
Debug.Print "Error # " & Str(Err.Number) & " was generated " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) &
Err.Description
End Sub
Found the error in the code above... The Range I was using to pass into the GenerateEmail() function was too broad of a range, the LR wasn't being set to a number and was using the entire column instead of a the Last Row that was NOT being calculated
Appreciate everyone for the assistance in error handling and debugging!
Related
My code works perfectly if the selected cell you tell it to delete is filled out with one of the part #'s from the drop down list.
But if you type in a new or custom # then want to delete that part # later the macro returns a
Type mismatch (Error 13)
Sub DeleteRows()
Application.ScreenUpdating = False
On Error GoTo whoa
If ActiveCell.Row <= 8 Then
MsgBox "Ooops!" & vbNewLine & _
vbNewLine & "Please select a Part Number"
ElseIf MsgBox("Are you sure you want to delete this part?" & vbNewLine & _
vbNewLine & _
ActiveCell.EntireRow.Cells(1, "A").Value & vbNewLine & _
ActiveCell.EntireRow.Cells(1, "B").Value & vbNewLine & _
"QTY: " & ActiveCell.EntireRow.Cells(1, "M").Value, _
vbYesNo) = vbYes Then
ActiveCell.Resize(3, 1).EntireRow.Delete
End If
Application.ScreenUpdating = True
Exit Sub
whoa:
MsgBox "Please select a number from the drop down list" & vbNewLine & _
"then run the delete command again.", vbInformation, Err.Description
End Sub
How can I get rid of that error and still have it delete the 3 rows (each part is 3 rows) if I enter a custom #.
Thanks
This is part of a much larger macro that has multiple instances of Application.OnTime that work just fine.
My issue with this one below is in WaitForPriceVolume() when it gets to the For Each loop and the If is true, it doesn't go back to the procedure WaitForPriceVolume(). It circles back to all the procedures that were called before, effectively just doing the Exit Sub as if the OnTime didn't exist.
When I strip out just the below code and add fixed values for the global variables being used, the Application.OnTime works. It's only when I plug it back into the bigger macro.
Sub BDP_PriceVolume()
Dim lsStartRange As String
Dim lsEndRange As String
Dim lnStartRow As Long
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Set sht = Worksheets("Variables")
' Use gvList
lsStartRange = "C" & gnStartRow
lnStartRow = gnStartRow + UBound(gvList, 2)
lsEndRange = "C" & lnStartRow
sht.Range(lsStartRange & ":" & lsEndRange).Value = _
"=BDP($A" & gnStartRow & "&Variables!$A$2,Variables!$D$2)"
lsStartRange = "D" & gnStartRow
lsEndRange = "D" & lnStartRow
If Worksheets("Variables").Cells(3, 3).Value <> "" Then
sht.Range(lsStartRange & ":" & lsEndRange).Value = _
"=BDH($A" & gnStartRow & "&Variables!$A$2,Variables!$E$3" & "," & _
"Variables!$B$4,Variables!$C$3," & _
Chr(34) & "BarTp=T" & Chr(34) & "," & _
Chr(34) & "BarSz=40" & Chr(34) & "," & _
Chr(34) & "Dir=V" & Chr(34) & "," & _
Chr(34) & "Dts=H" & Chr(34) & "," & _
Chr(34) & "Sort=A" & Chr(34) & "," & _
Chr(34) & "Quote=C" & Chr(34) & "," & _
Chr(34) & "UseDPDF=Y" & Chr(34) & ")"
Else
sht.Range(lsStartRange & ":" & lsEndRange).Value = _
"=BDP($A" & gnStartRow & "&Variables!$A$2,Variables!$E$2)"
End If
sht.Range("C" & gnStartRow & ":" & lsEndRange).Select
Application.Run "RefreshCurrentSelection"
Application.OnTime Now + TimeValue("00:00:03"), "WaitForPriceVolume"
End Sub
Private Sub WaitForPriceVolume()
Dim rng As Range
Set rng = sht.Range("C" & gnStartRow & ":D" & fnLastRow(sht, "A"))
Dim cell As Range
Application.ScreenUpdating = True
For Each cell In rng
If cell.Value = "#N/A Requesting Data..." Then
Application.OnTime Now + TimeValue("00:00:03"), "WaitForPriceVolume"
Exit Sub
End If
Next cell
Call DoneWaitForPriceVolume
End Sub
Own stupidity. All the other instances of OnTime came at the end of the code, so the macro had nothing left to do until the OnTime triggered and I forced everything to circle back to the main macro. I hadn't done that in this case. Problem solved. This haunted me for a week
Guided by Jzz and David on another post, I discovered a VBA userform and modules that can be imported to Access DB or Excel that will ask you to select a file and it will display the EXIF external info of that file particularly GPS Longitude, Latitude, and Altitude.
My question is how do I convert this so it opens a folder instead and retrieves the GPS info on each of the files in that folder. I know it may need to loop through the contents of a folder but I have no idea how to convert this. Please see attached file and open it as Access DB. I was only able to transfer it to Excel but the code was written in too many extra calls and functions I couldn't understand right away. It would be nice to be able to modify it and make it shorter.
EXIFReader
Sarah
EDIT Thanks to David, here's my modified version:
Sub OpenFromFolder()
On Error GoTo ExifError
Dim strDump As String
'Dim fso As Scripting.FileSystemObject
'Dim fldr As Scripting.Folder
'Dim file As Scripting.file
Set fso = CreateObject("scripting.filesystemobject")
Set fldr = fso.GetFolder("C:/Users/JayP/Downloads/Camera Uploads/Pics") '#### Modify this to your folder location
For Each file In fldr.Files
'## ONLY USE JPG EXTENSION FILES!!
Select Case UCase(Right(file.Name, 3))
Case "JPG"
With GPSExifReader.OpenFile(file.Path)
currrow = Sheet1.UsedRange.Rows.Count + 1
Sheet1.Range("A" & currrow).Value = "GPSLatitudeDecimal: " & .GPSLatitudeDecimal
Sheet1.Range("B" & currrow).Value = "GPSLongitudeDecimal: " & .GPSLongitudeDecimal
Sheet1.Range("C" & currrow).Value = "GPSAltitudeDecimal: " & .GPSAltitudeDecimal
End With
End Select
NextFile:
Next
Exit Sub
ExifError:
MsgBox "An error has occurred with file: " & file.Name & vbCrLf & vbCrLf & Err.Description
Err.Clear
Resume NextFile
End Sub
That is fairly sophisticated code -- written by Wayne Phillips who is a certified Microsoft MVP. While it might be nice to make the code more human-readable, I suspect it is already quite optimized.
I am posting this answer because it's an interesting question/application, normally I would say "Show me what you have tried so far" but given the relative complexity of Wayne's code, I'll waive that requirement. HOWEVER the additional caveat is that I won't answer a dozen follow-up questions on this code to teach you how to use VBA. This code is tested and it works.
There is an unused function call that allows you to open from a path, we are going to use this in a loop, over the files in a specified folder.
Function OpenFile(ByVal FilePath As String) As GPSExifProperties
Set OpenFile = m_ClassFactory.OpenFile(FilePath)
End Function
1. Import the Class Modules from Wayne's code in to your workbook's VBProject (I think you have already done this).
2. Create a new subroutine like the one below, in a normal code module.
Sub OpenFromFolder()
On Error GoTo ExifError
Dim strDump As String
'## REQUIRES REFERENCE TO MICROSOFT SCRIPTING RUNTIME
Dim fso As Scripting.FileSystemObject
Dim fldr As Scripting.Folder
Dim file As Scripting.file
Set fso = CreateObject("scripting.filesystemobject")
Set fldr = fso.GetFolder("C:/users/david_zemens/desktop/") '#### Modify this to your folder location
For Each file In fldr.Files
'## ONLY USE JPG EXTENSION FILES!!
Select Case UCase(Right(file.Name, 3))
Case "JPG"
With GPSExifReader.OpenFile(file.Path)
strDump = strDump & "FilePath: " & .FilePath & vbCrLf
strDump = strDump & "DateTimeOriginal: " & .DateTimeOriginal & vbCrLf
strDump = strDump & "GPSVersionID: " & .GPSVersionID & vbCrLf
strDump = strDump & "GPSLatitudeDecimal: " & .GPSLatitudeDecimal & vbCrLf
strDump = strDump & "GPSLongitudeDecimal: " & .GPSLongitudeDecimal & vbCrLf
strDump = strDump & "GPSAltitudeDecimal: " & .GPSAltitudeDecimal & vbCrLf
strDump = strDump & "GPSSatellites: " & .GPSSatellites & vbCrLf
strDump = strDump & "GPSStatus: " & .GPSStatus & vbCrLf
strDump = strDump & "GPSMeasureMode: " & .GPSMeasureMode & vbCrLf
strDump = strDump & "GPSDOPDecimal: " & .GPSDOPDecimal & vbCrLf
strDump = strDump & "GPSSpeedRef: " & .GPSSpeedRef & vbCrLf
strDump = strDump & "GPSSpeedDecimal: " & .GPSSpeedDecimal & vbCrLf
strDump = strDump & "GPSTrackRef: " & .GPSTrackRef & vbCrLf
strDump = strDump & "GPSTrackDecimal: " & .GPSTrackDecimal & vbCrLf
strDump = strDump & "GPSImgDirectionRef: " & .GPSImgDirectionRef & vbCrLf
strDump = strDump & "GPSImgDirectionDecimal: " & .GPSImgDirectionDecimal & vbCrLf
strDump = strDump & "GPSMapDatum: " & .GPSMapDatum & vbCrLf
strDump = strDump & "GPSDestLatitudeDecimal: " & .GPSDestLatitudeDecimal & vbCrLf
strDump = strDump & "GPSDestLongitudeDecimal: " & .GPSDestLongitudeDecimal & vbCrLf
strDump = strDump & "GPSDestBearingRef: " & .GPSDestBearingRef & vbCrLf
strDump = strDump & "GPSDestBearingDecimal: " & .GPSDestBearingDecimal & vbCrLf
strDump = strDump & "GPSDestDistanceRef: " & .GPSDestDistanceRef & vbCrLf
strDump = strDump & "GPSDestDistanceDecimal: " & .GPSDestDistanceDecimal & vbCrLf
strDump = strDump & "GPSProcessingMethod: " & .GPSProcessingMethod & vbCrLf
strDump = strDump & "GPSAreaInformation: " & .GPSAreaInformation & vbCrLf
strDump = strDump & "GPSDateStamp: " & .GPSDateStamp & vbCrLf
strDump = strDump & "GPSTimeStamp: " & .GPSTimeStamp & vbCrLf
strDump = strDump & "GPSDifferentialCorrection: " & .GPSDifferentialCorrection & vbCrLf
Debug.Print strDump '## Modify this to print the results wherever you want them...
End With
End Select
NextFile:
Next
Exit Sub
ExifError:
MsgBox "An error has occurred with file: " & file.Name & vbCrLf & vbCrLf & Err.Description
Err.Clear
Resume NextFile
End Sub
You need to modify this:
Set fldr = fso.GetFolder("C:/users/david_zemens/desktop/")
And also this. I assume you already know how to put the data in a worksheet or display it on a form, etc. This line only prints to the console in the Immediate window of the VBA, it will not write to a worksheet/etc. unless you modify it to do so. That is not part of the question, so I will leave that up to you to work out :)
Debug.Print strDump
NOTE: I removed some object variables that you won't have in Excel, and added some new variables to do the Folder/Files iteration. I put in simple error handling to inform you of errors (msgbox) and resume the next file. In my testing, the only error I got was some files do not have EXIF data.
I am trying to use VBA in Excel to add conditional formatting to a column of a pivot table. The issue is that whenever the pivot table is refreshed, or a filter is changed, etc. the conditional formatting is lost. My solution was to add a macro to the pivot table update event in the workbook, which works ... kinda. It seems that when I run the code that creates the pivot table and adds the code to handle conditional formatting an error occurs but ONLY when the VBA window is NOT open. If the VBA window is open the code executes normally - despite no code changes or reference changes.
Private Sub setupConditionalFormattingForStatusColumn()
Dim thisSheetModule As vbcomponent
Dim formattingCodeString As String
On Error GoTo conditionalFormattingError
formattingCodeString = _
"Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)" & vbNewLine & _
" With Target.parent.Columns(" & harReportColumn("Status") & ")" & vbNewLine & _
" .FormatConditions.AddIconSetCondition" & vbNewLine & _
" .FormatConditions(.FormatConditions.Count).SetFirstPriority" & vbNewLine & _
vbNewLine & _
" With .FormatConditions(1)" & vbNewLine & _
" .IconSet = ActiveWorkbook.IconSets(xl4TrafficLights)" & vbNewLine & _
" .IconCriteria(1).Icon = xlIconYellowExclamation" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(2) " & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = -1" & vbNewLine & _
" .Operator = 5" & vbNewLine & _
" .Icon = xlIconGreenCircle" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(3)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.05" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconYellowCircle" & vbNewLine & _
" End With" & vbNewLine
formattingCodeString = formattingCodeString & vbNewLine & _
" With .IconCriteria(4)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.15" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconRedCircleWithBorder" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .ShowIconOnly = True" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .HorizontalAlignment = xlCenter" & vbNewLine & _
" .VerticalAlignment = xlCenter" & vbNewLine & _
" End With" & vbNewLine & _
"End Sub"
Set thisSheetModule = ThisWorkbook.VBProject.VBComponents(harReportSheet.CodeName)
thisSheetModule.CodeModule.AddFromString formattingCodeString
Exit Sub
conditionalFormattingError:
errorLog.logError "WARNING: An error occured while applying the conditional formatting code for the ""Status"" column."
Err.Clear
Resume Next
End Sub
The line which generates the error is: thisSheetModule.CodeModule.AddFromString formattingCodeString but the error is only generated if the VBA window is closed.
Any ideas?
So I was able to find an answer to this issue. Evidently Excel does not properly initialize the codename property of newly created worksheets when the VBA window is not open (the why here is beyond me) but only when it recompiles. A work-around is to force Excel to recompile prior to any calls to the codename property. The solution which worked for me was to place the following code:
On Error Resume Next
Application.VBE.CommandBars.ActiveMenuBar.FindControl(ID:=578).Execute
On Error GoTo conditionalFormattingError
above the line beginning with Set thisSheetModule = ... . Oddly enough the line of code which forces the recompile also throws an error for me which I was able to safely ignore with the surrounding error handling.
More information can be found here: http://www.office-archive.com/2-excel/d334bf65aeafc392.htm
Hope that helps someone out there. :-)
This code stops after a while due to protected files such as system files, "Permission Denied".
Is there a way to modify the code below so that it can handle such protected files or bypass them?
Set objFS=CreateObject("Scripting.FileSystemObject")
WScript.Echo Chr(34) & "Full Path" &_
Chr(34) & "," & Chr(34) & "File Size" &_
Chr(34) & "," & Chr(34) & "File Date modified" &_
Chr(34) & "," & Chr(34) & "File Date Created" &_
Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34)
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_
Chr(34) & strFile.Size & Chr(34) & "," &_
Chr(34) & strFile.DateLastModified & Chr(34) & "," &_
Chr(34) & strFile.DateCreated & Chr(34) & "," &_
Chr(34) & strFile.DateLastAccessed & Chr(34)
Next
End Sub
Then call it from the command line
like this.
c:\test> cscript //nologo myscript.vbs "c:\" > "C:\test\Output.csv"
I've simplified your code (based upon your duplicate question) and without trying to handle errors I can see a problem: objDIR.SubFolders fails when one of the subfolders (such as \System Volume Information) doesn't have permissions to be viewed! You need to use another method on Folder to enumerate the foldernames, combine them with the existing path and then trap the error .GetFolder may cause when you don't have permissions. (I don't have time to code that solution at the moment.)
Option Explicit
Dim objFS
Dim objArgs
Dim strFolder
Dim objFolder
Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
WScript.StdOut.WriteLine """Full Path"",""File Size""," & _
"""File Date modified"",""File Date Created""," & _
"""File Date Accessed"""
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go objFolder
Sub Go(objDIR)
Dim strFile
On Error Resume Next
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
For Each strFile In objDIR.Files
WScript.StdOut.WriteLine """" & strFile.Path & """,""" & _
strFile.Size & """,""" & _
strFile.DateLastModified & """,""" & _
strFile.DateCreated & """,""" & _
strFile.DateLastAccessed & """"
Next
End Sub
VBScript allows error trapping, though not as gracefully as VBA. Try the script below.
On Error Resume Next
'[ ... code ... ]
Dim test_result, divisor
divisor = 1 '' No error
'divisor = 0 '' raise error #11
'divisor = "zero" '' raise a different error
test_result = 2/divisor
If Err.Number = 11 then ''This line must appear at the point error is raised
MsgBox "Handled Error: " & Err.Description
ElseIf Err.Number > 0 then
MsgBox "Error: " & Err.Number & " " & Err.Description
Err.Clear ''if you wanted to proceed clean from here
End If
MsgBox "Result: " & test_result
ensure the process has permissions. see
You can ignore script errors in VBScript by adding
On Error Resume Next
before the part of the code where you want to ignore errors.
The statement to restore the default behavior is
On Error GoTo 0
And just a remark: Method calls in VB and VBScript don't use parenthesis if they appear as a single statement. So the line Go (objFolder) should be replaced by Go objFolder.