I use a lot of QueryTables.Add
in VBA and it has a ton of parameters. I was looking to "functionalize" my subs for this task and I think I've run into a limit for VBA, though I couldn't find an exact number online, but when I tried to make all parameters optional my editor went red. When removing some, it went green, but I was already considering a way to perhaps make a single optional argument that would perhaps be some sort of Param:Value
keypair that I could then split/parse/apply in the sub. This would also allow me to use this in the future for other subs with hopefully less code. I also don't currently manipulate many of the QueryTables params, but I'd like to "futureproof" this sub.
Here is my current Sub Header, note the '
If you try to add that last param, the editor goes red. What I'm looking for is suggestions how to best approach
- How to accept a large number of params
- How to then utilize them in the code.
Public Sub Query_Web_URL(URLStr As String, WSNameStr As String, Optional AdjustColumnWidth As String, Optional Application As String, Optional BackgroundQuery As String, Optional CommandText As String, Optional CommandType As String, Optional Creator As String, Optional EditWebPage As String, Optional EnableEditing As String, Optional EnableRefresh As String, Optional FetchedRowOverflow As String, Optional FieldNames As String, Optional FillAdjacentFormulas As String, Optional ListObject As String, Optional MaintainConnection As String, _
Optional Parameters As String, Optional Parent As String, Optional PostText As String, Optional PreserveColumnInfo As String, Optional PreserveFormatting As String, Optional QueryType As String, Optional Recordset As String, Optional Refreshing As String, Optional RefreshOnFileOpen As String, Optional RefreshPeriod As String, Optional RefreshStyle As String, Optional ResultRange As String, Optional RobustConnect As String, Optional RowNumbers As String, Optional SaveData As String, Optional SavePassword As String, Optional Sort As String, Optional SourceConnectionFile As String, Optional SourceDataFile As String, Optional TextFileColumnDataTypes As String, Optional TextFileCommaDelimiter As String, Optional TextFileConsecutiveDelimiter As String, _
Optional TextFileDecimalSeparator As String, Optional TextFileFixedColumnWidths As String, Optional TextFileOtherDelimiter As String, Optional TextFileParseType As String, Optional TextFilePlatform As String, Optional TextFilePromptOnRefresh As String, Optional TextFileSemicolonDelimiter As String, Optional TextFileSpaceDelimiter As String, Optional TextFileStartRow As String, Optional TextFileTabDelimiter As String, Optional TextFileTextQualifier As String, Optional TextFileThousandsSeparator As String, Optional TextFileTrailingMinusNumbers As String, _
Optional TextFileVisualLayout As String, Optional WebConsecutiveDelimitersAsOne As String, Optional WebDisableDateRecognition As String, Optional WebDisableRedirections As String, Optional WebFormatting As String, Optional WebPreFormattedTextToColumns As String, Optional WebSelectionType As String, Optional WebSingleBlockTextImport As String, Optional WebTables As String) ' , Optional WorkbookConnection As String )
Dim WS As Worksheet
Call WorksheetCreateDelIfExists(WSNameStr)
Set WS = Worksheets(WSNameStr)
With WS.QueryTables.Add(Connection:="URL;" & URLStr, Destination:=Range("$A$1"))
.Name = URLStr
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingAll
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
Update:
I see now, that string could require me to cast certain params as there correct type, so that would be first issue. And the truth is, I don't need currently to pass many params, so my main goal is more so, future proofing so I can add in more params without messing up existing code.
In this regard, my question becomes how can I pass variables as optional, but if not passed, use the default, without doing If variable <> "" Then
for each possible variable.