Greetings Outstanding Mr. Excel Community,
I have some experience with using Excel macros and VBA scripting but I don't know enough about loops and arrays to solve this little problem. I think I have just about all the code correct, so this should be an easy fix.
Basically I have a workbook with an instructions and a tmp (template) worksheet along with a bunch of other worksheets that are almost identical to the tmp worksheet.
The worksheet tabs look like this as an example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Instructions[/TD]
[TD]templatecopy1[/TD]
[TD]templatecopy2[/TD]
[TD]templatecopy3[/TD]
[TD]templatecopy4[/TD]
[TD]templatecopy5[/TD]
[TD]tmp[/TD]
[/TR]
</tbody>[/TABLE]
For example, on the instructions sheet, in cells j3:j30, I have this information
[TABLE="width: 500, align: left"]
<tbody>[TR]
[TD]list1.csv[/TD]
[/TR]
[TR]
[TD]list2.csv[/TD]
[/TR]
[TR]
[TD]list3.csv[/TD]
[/TR]
[TR]
[TD]list4.csv[/TD]
[/TR]
[TR]
[TD]list5.csv[/TD]
[/TR]
</tbody>[/TABLE]
Also on the Instructions Sheet, I have a list of all the duplicate worksheet names that are based upon the tmp worksheet in cells I3:I30.
With the exception of the instructions worksheet and the tmp worksheet, I am trying to setup a worksheet connection for each of the worksheets that are a copy of the tmp worksheet. So for templatecopy1 worksheet, I want to setup a connection to the list1.csv data file. For the templatecopy2 worksheet, I want to setup a connection to the list2.csv data file.
The following code will perform this task except that the data connection that is always setup for each worksheet is list1.csv. I know this is because the ImportFileName variable assigns an absolute value, but when I attempt setting it up with an array value based on the values in J3:J30, I get a type mismatach error.
This is the code that I am testing that presents a type mismatch error message.
Any assistance that can be provided is greatly appreciated.
Thanks!
Seth
I have some experience with using Excel macros and VBA scripting but I don't know enough about loops and arrays to solve this little problem. I think I have just about all the code correct, so this should be an easy fix.
Basically I have a workbook with an instructions and a tmp (template) worksheet along with a bunch of other worksheets that are almost identical to the tmp worksheet.
The worksheet tabs look like this as an example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Instructions[/TD]
[TD]templatecopy1[/TD]
[TD]templatecopy2[/TD]
[TD]templatecopy3[/TD]
[TD]templatecopy4[/TD]
[TD]templatecopy5[/TD]
[TD]tmp[/TD]
[/TR]
</tbody>[/TABLE]
For example, on the instructions sheet, in cells j3:j30, I have this information
[TABLE="width: 500, align: left"]
<tbody>[TR]
[TD]list1.csv[/TD]
[/TR]
[TR]
[TD]list2.csv[/TD]
[/TR]
[TR]
[TD]list3.csv[/TD]
[/TR]
[TR]
[TD]list4.csv[/TD]
[/TR]
[TR]
[TD]list5.csv[/TD]
[/TR]
</tbody>[/TABLE]
Also on the Instructions Sheet, I have a list of all the duplicate worksheet names that are based upon the tmp worksheet in cells I3:I30.
With the exception of the instructions worksheet and the tmp worksheet, I am trying to setup a worksheet connection for each of the worksheets that are a copy of the tmp worksheet. So for templatecopy1 worksheet, I want to setup a connection to the list1.csv data file. For the templatecopy2 worksheet, I want to setup a connection to the list2.csv data file.
The following code will perform this task except that the data connection that is always setup for each worksheet is list1.csv. I know this is because the ImportFileName variable assigns an absolute value, but when I attempt setting it up with an array value based on the values in J3:J30, I get a type mismatach error.
Code:
'Select Instructions worksheet
Sheets("Instructions").Select
'Specify memory space for necessary information
Dim CurrentSheet As Worksheet
Dim WorkSheetList As Variant
Dim ImportCSVPath As String
Dim ImportFileName As Variant
'Assign values to the specified memory space variables
WorkBookPath = ThisWorkbook.Path
WorkSheetList = Sheets("Instructions").Range("I3:I30").Value
ImportCSVPath = "\sites_CSV_files\"
ImportFileName = "list1.csv"
FullFileNamePath = WorkBookPath & ImportCSVPath & ImportFileName
'Start counter at position 1
x = 1
'This for procedure loops to all sheets except Instructions and tmp sheets
For Each CurrentSheet In Worksheets
' Avoid worksheets named "Instructions" and "tmp"
If CurrentSheet.Name <> "Instructions" And CurrentSheet.Name <> "tmp" Then
' Activate the Worksheet in the loop
CurrentSheet.Activate
'Setup worksheet connection to the CSV file located at the full file name path
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" + FullFileNamePath, Destination:=Range("$A$4"))
.Name = ImportFileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = True
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Increase X by one value after every repetition
x = x + 1
' Exit for loop if X is positioned at last value in the range
If x = UBound(WorkSheetList, 1) + 1 Then Exit For
End If
'Parse commands on the next worksheet
Next CurrentSheet
This is the code that I am testing that presents a type mismatch error message.
Code:
'Select Instructions worksheet
Sheets("Instructions").Select
'Specify memory space for necessary information
Dim CurrentSheet As Worksheet
Dim WorkSheetList As Variant
Dim ImportCSVPath As String
Dim ImportFileName As Variant
'Assign values to the specified memory space variables
WorkBookPath = ThisWorkbook.Path
WorkSheetList = Sheets("Instructions").Range("I3:I30").Value
ImportCSVPath = "\sites_CSV_files\"
ImportFileName = Sheets("Instructions").Range("J3:J30").Value
FullFileNamePath = WorkBookPath & ImportCSVPath & ImportFileName
'Start counter at position 1
x = 1
'This for procedure loops to all sheets except Intructions and tmp sheets
For Each CurrentSheet In Worksheets
' Avoid worksheets named "Instructions" and "tmp"
If CurrentSheet.Name <> "Instructions" And CurrentSheet.Name <> "tmp" Then
' Activate the Worksheet in the loop
CurrentSheet.Activate
'Setup worksheet connection to the CSV file located at the full file name path
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" + FullFileNamePath, Destination:=Range("$A$4"))
.Name = ImportFileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = True
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Increase X by one value after every repetition
x = x + 1
' Exit for loop if X is positioned at last value in the range
If x = UBound(WorkSheetList, 1) + 1 Then Exit For
End If
'Parse commands on the next worksheet
Next CurrentSheet
Any assistance that can be provided is greatly appreciated.
Thanks!
Seth