I am beginning a code that will open a csv file, find specific columns and then copy and paste them into the primary excel workbook.
The csv file will have various headers in potentially different orders as they wer automatically generated. Before trying to copy any information, I want to validate the csv file to make sure all the necessary data is present.
Dim WebImp As Workbook
Dim BackLog As String
Dim col As Long, res As Variant
Dim SearchValue As String
Private Sub CommandButton1_Click()
planned:
MsgBox "Open a valid Web Backlog export.", vbExclamation, "Web Import"
Application.FileDialog(msoFileDialogFilePicker).Show
BackLog = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
Set WebImp = Workbooks.Open(BackLog)
Application.DisplayAlerts = False
Application.ScreenUpdating = False
WebImp.Sheets(1).Activate
SearchValue = "Summary"
res = Application.Match(SearchValue, WebImp.Sheets(1).Rows(1), 0)
If IsError(res) Then
WebImp.Close
MsgBox "File missing necessary data.", vbExclamation, "Missing Data"
GoTo planned
End If
End Sub
This is an example of the headers. Highlighted is the column I am trying to seek, but there is no guarantee it will be in column A in the future.
Currently, my code is defaulting to
error 2042
and determines the file is incorrect, even though a cell with "Summary" is present.
What is wrong with the syntax I have written? Why is it not grabbing properly? And what are some potential solutions?