I have an Excel file that houses the name of tables and their corresponding columns as follows: Tables and Columns
I have written a sub procedure that returns all corresponding columns for the name of a table.
My current code does the following:
Saves Col A (table names) and Col B (column names) to a 2D array
Looks through the array to find if the name of the table exists in column one of the array.
- If the table name exists, then a counter is increased and another array is re-dimensioned each time the value occurs in the array.
- I then set that array equal to the original array and return that array of column names back to the sheet.
I have an error handler in there that says if the name of the table is NOT the full name of the table (i.e. the user was a character(s) short, or entered a table name that does not exist in the array) then show the following message:
"You must enter the the full name of the Table."
My issue is that the above error handler is showing the error no matter if the correct table name is entered or not and I have no idea why. If I remove the error handler, the code runs perfect...that is until someone enters a table name that doesn't exist in the array, which is what the frazzling error handler is supposed to address, except it won't work right, lol.
Does anybody know what is wrong and how to fix it?? It has to be able to handle pretty big array with lots of data, really fast.
Thank you for any ideas, suggestions, or answers in advance!!!
Note: I have over 100,000 rows, so application.Match, Index, or a combination will not work.
Here is the full code:
Option Compare Text
Option Explicit
Sub Testingggg()
Dim Big_Array(), small_array() As Variant
Dim i, j, LookUpValue_Counter As Long
Dim LookUpValue As String
Application.ScreenUpdating = False
'Clear previously returned data
Worksheets("New TRAX").Range("C2:D300").ClearContents
If UserForm2.txttablecols.Value <> "" Then
LookUpValue = UserForm2.txttablecols.Value
Else
MsgBox "You must enter a the name of the table.", vbExclamation, "Error Encountered"
Exit Sub
End If
'Store cols A and B in the Big_Array
Big_Array = Range("A2").CurrentRegion
'Starting in the second row of the Big_Array loop through _
each element of the Big_Array
For i = LBound(Big_Array, 1) To UBound(Big_Array, 1)
'Note: I used Option Compare Text at the _
beginning of the module to make the text _
comparisons case insensitive
'This searches the second Col (i.e. Column Names) only
If Big_Array(i, 2) Like LookUpValue Then
MsgBox "You must enter the name of the Table, NOT the Column", vbExclamation, "Error Encountered"
Exit Sub
'*******************************************************
'This ElseIf is what gives me problems. Like I said _
before, if I remove this, the other code and error _
handlers work perfectly. However if the the user _
enters a table name that doesn't exist, then the code _
won't run, but that makes sense, b/c that's what error _
handler is for...except it doesn't work, lol.
'*******************************************************
This searches the first Col (i.e. Table Names) only
ElseIf Not (Big_Array(i, 1) Like LookUpValue) Then
MsgBox "You must enter the the full name of the Table.", vbExclamation, "Error Encountered"
Exit Sub
'If the table name exists and is in the correct _
format, then execute the following
ElseIf Big_Array(i, 1) Like LookUpValue Then
'increase the LookUpValue_Counter by 1 each _
time the LookUpValue matches a value in col A
LookUpValue_Counter = LookUpValue_Counter + 1
'Redimension the small_array array with each instance _
of a the LookUpValue in the Big_Array.
ReDim Preserve _
small_array(1 To 2, 1 To LookUpValue_Counter)
'*******************************************
'NOTE: FOR THOSE OF YOU WHO HAVE BEEN TRYING TO _
FIND A WAY AROUND SUB-SETTING AN ARRAY THAT _
WORKS FOR WAY MORE THAN 65,536 ROWS (see index _
for sub-setting arrays) I FOUND A WAY :), NOTE THE _
2 to 2 IN THE FOR LOOP; THIS PULLS ONLY THE SECOND _
COLUMN OF THE ARRAY _
The following Starts a counter (j) to populate _
the small_array.
'*******************************************
For j = 2 To 2
'The small_array array equals the current Big_Array
small_array(j, LookUpValue_Counter) _
= Big_Array(i, j)
Next j
End If
Next i
'Transpose the small_array onto sheet
ActiveSheet.Range("C2", Range("C2").Offset(LookUpValue_Counter - 1, 1)) _
= Application.Transpose(small_array)
'Write LookUpValue to sheet
Worksheets("New TRAX").Cells(2, 3).Value2 = LookUpValue
Application.ScreenUpdating = True
End Sub
I have also tried the following:
ElseIf Not InStr(1, Big_Array(i, 1), LookUpValue, vbTextCompare) Then
ElseIf Big_Array(i, 1) <> LookUpValue Then
ElseIf Not (Big_Array(i, 1) = LookUpValue) Then
None of which worked.
"ACCESS$", "COLUMNS, D_OBJ#, ..."