I have a worksheet and user form that is used to import summary data from several data files (.CSV) and copy it to a tab in the main worksheet.
The user inputs where the files are stored, what type of file they want to open, the incremental number of the file, and the name of the tab where they want the data to go. After the data is copied, the filenumber increments and the next file is opened. Everything works fine until it comes to a file that doesnt exist. Here is the main piece of code(I took out some spaces so it didnt take up so much room) for the userform. The files have the following format depending on the file number:
p1_0001.csv, p1_0010.csv, p1_0100.csv
I have two issues with the code that I can't figure out.
1. I need a way to set the initial filecount outside of the code so that the number of zeroes in the filename is correct. Currently, the user has to go into the code to make the filecount the same as the filenum they enter. For some reason, I can't link the filecount to the filenum text box because the loop doesn't work correctly.
2. When the code gets to a file that doesnt exist, the code fails on the .Refresh BackgroundQuery:=False. When a file doesnt exist, I need a way to skip to the end of the code where the filenumber increments and then continue the loop.
The user inputs where the files are stored, what type of file they want to open, the incremental number of the file, and the name of the tab where they want the data to go. After the data is copied, the filenumber increments and the next file is opened. Everything works fine until it comes to a file that doesnt exist. Here is the main piece of code(I took out some spaces so it didnt take up so much room) for the userform. The files have the following format depending on the file number:
p1_0001.csv, p1_0010.csv, p1_0100.csv
Code:
zero = "0"
zeroes = "00"
tzeroes = "000"
fileCount = 37
Do
filepath = path_txtbx.Text
filenum = filenum_txtbx.Value
fnamestor = fNamestor_cmbbx.Text
stabname = stabname_cmbbx.Text
If (parameter_cmbbx.Text = "Weight") Then
filepre = "G1"
End If
If (parameter_cmbbx.Text = "Hardness") Then
filepre = "H1"
End If
If (parameter_cmbbx.Text = "Thickness") Then
filepre = "D1"
End If
If (parameter_cmbbx.Text = "Diameter") Then
filepre = "R1"
End If
If (parameter_cmbbx.Text = "Batch") Then
filepre = "P1"
End If
If (fileCount < 1000) Then
fpname = CStr(filepath + "\" + filepre + "_" + zero + filenum + ".csv")
sname = CStr(filepre + "_" + zero + filenum)
End If
If (fileCount < 100) Then
fpname = CStr(filepath + "\" + filepre + "_" + zeroes + filenum + ".csv")
sname = CStr(filepre + "_" + zeroes + filenum)
End If
If (fileCount < 10) Then
fpname = CStr(filepath + "\" + filepre + "_" + tzeroes + filenum + ".csv")
sname = CStr(filepre + "_" + tzeroes + filenum)
End If
ChDir filepath
With ActiveSheet.QueryTables.Add(Connection:="Text;" & fpname, _
Destination:=Range("A1"))
.Name = sname
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlNone
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = ","
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False 'This errors when the file doesnt exist
End With
BatchID = Range("B1")
Range("A8").Select
Selection.Value = BatchID
Range("A8").Select
Range(Selection, Selection.End(xlToRight)).Select
lastRowdest = Sheets(stabname).Cells(Rows.Count, "A").End(xlUp).Row + 1
Selection.Copy Sheets(stabname).Range("A" & lastRowdest)
Sheet3.Cells.Select
Selection.ClearContents
Selection.QueryTable.Delete 'This errors when the file doesnt exist
Sheet3.Range("A1").Select
fileCount = fileCount + 1
filenum_txtbx.Value = fileCount
Loop Until filenum = "100"
End Sub
I have two issues with the code that I can't figure out.
1. I need a way to set the initial filecount outside of the code so that the number of zeroes in the filename is correct. Currently, the user has to go into the code to make the filecount the same as the filenum they enter. For some reason, I can't link the filecount to the filenum text box because the loop doesn't work correctly.
2. When the code gets to a file that doesnt exist, the code fails on the .Refresh BackgroundQuery:=False. When a file doesnt exist, I need a way to skip to the end of the code where the filenumber increments and then continue the loop.