3

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:

  1. Saves Col A (table names) and Col B (column names) to a 2D array

  2. Looks through the array to find if the name of the table exists in column one of the array.

  3. If the table name exists, then a counter is increased and another array is re-dimensioned each time the value occurs in the array.
  4. 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.

7
  • I would just use a dictionary and keep appending the column names to the value for each "ACCESS$", "COLUMNS, D_OBJ#, ..." Commented Dec 6, 2018 at 14:52
  • 1
    No Joke, I was literally just writing something to do that very thing, lol. The thing is, the code worked PERFECTLY yesterday and then I decided that I wanted to move a couple of things around. I am paranoid about this kind of thing happening so I always either export the module or save a backup of the working Excel file, however, for some reason, the I didn't and here I am. It's frustrating because the dern thing worked like a dream yesterday and I would love to remember or figure out what I did, or didn't do to make it function as intended. Commented Dec 6, 2018 at 15:02
  • Why doesn't application match or index work on 100K rows? Commented Dec 6, 2018 at 15:05
  • @user10735198 See the following link dailydoseofexcel.com/archives/2013/10/11/… Commented Dec 6, 2018 at 15:16
  • @user10735198 Actually I take that back about match, that might work, but I don't think so for Index. I could be wrong though. Commented Dec 6, 2018 at 15:20

1 Answer 1

1

At the beginning you wrote

For i = LBound(Big_Array, 1) To UBound(Big_Array, 1)

and later you wrote

ElseIf Not (Big_Array(i, 1) Like LookUpValue) Then
    MsgBox "You must enter the the full name of the Table.", vbExclamation, "Error Encountered"

Think about it for a while.

It will always show you the message if the LookUpValue isn't the first one in Big_Array. I bet you wanna first loop through the whole array and then when it is not found you wanna show the message.

Implementing a Boolean will do the trick ' ***:

Option Compare Text
Option Explicit
Sub Testingggg()

    Dim Big_Array(), small_array() As Variant
    Dim i, j, LookUpValue_Counter As Long
    ' ***
    Dim blnfound As Boolean
    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

             'If the table name exists and  is in the correct _
              format, then execute the following
             ElseIf Big_Array(i, 1) Like LookUpValue Then

                ' ***
                blnfound = True
                '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

        ' *** If no 'Like' was found
        If blnfound = False Then
           MsgBox "You must enter the the full name of the Table.", vbExclamation, "Error Encountered"
           Exit Sub
        End If

        '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

BTW, why are i and j declared as Variant?

Sign up to request clarification or add additional context in comments.

3 Comments

IT WORKS! You are a gentleman and a scholar! I thought about it for a while as you suggested, tried moving around the logic and loop structure a bit more, aaaaannnnnd I was still not getting, lol. I was so focused on trying to replicate what I did yesterday that I was mentally blocked from using simple logic. I can't thank you enough for taking the time to answer my question thoughtfully and provide a working solution. As for i and j, I have them declared as Long so I am not sure what you mean??
When you declare variables in one row it goes like this: 'Dim i As Long, j As Long, LookUpValue_Counter As Long', in your version 'Dim i, j, LookUpValue_Counter As Long', variables i and j are declared as Variant.
My life is a lie, lol! All this time I thought that one could declare variables in a single line as I did and they would revert as they are declared. I did at least know that VBA assigns undeclared variables as variant (I'm only saying this to make myself feel better about not knowing simple variable declaration structure...*sigh*).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.