4 min readfrom Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

What is the best way to check if the file exists, is not open, and is a CSV file?

Our take

When verifying the integrity of CSV files in your workflow, it’s essential to ensure they exist, are not currently open, and are formatted correctly. This guide will explore an effective approach using VBA to create functions that check these criteria. The provided code demonstrates how to validate file existence, detect if a file is open, and confirm the CSV structure by comparing it against expected headers.

First, I will show my code that I currently have:

Public Function IsThisCSV(sFile As String) As Boolean Dim ff As Long Dim fLine As String Dim sLineSrc As Variant Dim sLineRpt() As String Dim i As Long sLineSrc = Array("Interval Start", "Interval End", "Interval Complete", "Filters", "Agent Id", "Agent Name", "Release Date/Time", "Score", "Critical Score", "Average Score", "Average Critical Score", "Highest Score", "Highest Critical Score", "Lowest Score", "Lowest Critical Score", "Evaluation Form Name", "Evaluator", "Assignee", "Reviewed By Agent", "Interaction Date/Time", "Evaluation Date/Time", "Media Type", "Agent Comments", "Status", "Disputes", "Revisions") If Not DoesFileExist(sFile) Then IsThisCSV = False Exit Function End If If IsFileOpen(sFile) Then ' File is locked, cannot read it IsThisCSV = False Exit Function End If If FileLen(sFile) = 0 Then IsThisCSV = False Exit Function End If ff = FreeFile Open sFile For Input As #ff If Not EOF(ff) Then Line Input #ff, fLine End If Close #ff fLine = fLine = Replace(fLine, """", "") sLineRpt = Split(fLine, ",") If UBound(sLineSrc) <> UBound(sLineRpt) Then IsThisCSV = False Exit Function End If For i = LBound(sLineSrc) To UBound(sLineSrc) If UCase(Trim(sLineRpt(i))) <> UCase(Trim(sLineSrc(i))) Then IsThisCSV = False Exit Function End If Next i 'If we reach here, it's valid IsThisCSV = True End Function Public Function IsValidWB(sFile As String) As Boolean Dim wb As Workbook Dim xlApp As New Excel.Application xlApp.Visible = False SetAppSettings False, xlApp If Not DoesFileExist(sFile) Then IsValidWB = False GoTo Cleanup End If If IsFileOpen(sFile) Then ' File is locked, cannot open it for this check IsValidWB = False GoTo Cleanup End If If FileLen(sFile) = 0 Then IsValidWB = False GoTo Cleanup End If On Error Resume Next Set wb = xlApp.Workbooks.Open(FileName:=sFile, ReadOnly:=True) On Error GoTo 0 If Err.Number <> 0 Then ' An error occurred, so it is likely not a valid or non-corrupt workbook IsValidWB = False Err.Clear ' Clear the error Else ' No error occurred, so it is a valid workbook IsValidWB = True ' Close the workbook without saving changes wb.Close SaveChanges:=False End If Cleanup: Set wb = Nothing SetAppSettings True, xlApp If Not xlApp Is Nothing Then xlApp.Quit Set xlApp = Nothing End Function Public Function DoesFileExist(sFile As String) As Boolean Dim FSO As Object Set FSO = CreateObject("Scripting.FileSystemObject") On Error Resume Next DoesFileExist = FSO.FileExists(sFile) On Error GoTo 0 Set FSO = Nothing End Function Public Function IsFileOpen(sFile As String) As Boolean Dim fileNum As Integer Dim errNum As Long On Error Resume Next fileNum = FreeFile() Open sFile For Input Lock Read Write As #fileNum errNum = Err.Number On Error GoTo 0 If errNum = 0 Then Close #fileNum IsFileOpen = (errNum <> 0) End Function 

So, to further explain: Sheet1 = Start Here, on this sheet, there are 3 files that need to be loaded. The user will click on a button to select that file.
File1 = This is a file downloaded from Sharepoint, it's a CSV file. File2 = This is a daily file emailed from our Call Center Application. It only contains the updated information. File3 = This is the report file, it'll by manually created to start it, but file1 and file2 are used for data.

Right now, I am trying to make sure that file1 and file2 are good using the above Subs/Functions. I included the SetFile as that is how the user selects the file.

I've been using the Google AI from google.com and it keeps saying I should change this, I make the change, and recheck, it says I need to change that. So I change that, recheck, and it says I have to change this again.

So I am checking with the experts here. If more questions are needed, please ask. Any changes to make it more robust and more generic would be helpful.

Edit 1:

So, I think this now catches everything. I modified the above code to what I have now.

submitted by /u/Difficult_Cricket319
[link] [comments]

Read on the original site

Open the publisher's page for the full experience

View original article

Tagged with