Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

error in running downloaded VBA Code from Teach Excel

0

I ran into " Fast Search Multiple Excel Workbooks using a File Picker Window" on youtube channel of teachexcel, and have since been trying to run it, only that I keep getting subscript out of range error at  "  If (dataArray(i, searchField) = searchValue) Then....

could I get some help in configuring out could have been the problem,

Sub Data_Search(wbMaster As Workbook, wbSlave As Workbook)
' TeachExcel.com

Dim ws As Worksheet
Dim dashboard As Worksheet
Dim dataArray As Variant
Dim datatoShowArray As Variant

'Application.ScreenUpdating = False 'Turning off for the macro can speed things up - not so much here though.

'Dashboard sheet
Set dashboard = wbMaster.Sheets("Dashboard")

'Data table information
dataColumnStart = 1
dataColumnEnd = 9
dataColumnWidth = dataColumnEnd - dataColumnStart ' Number of columns for the raw data (+1 not included because it makes lower calculations more confusing)
dataRowStart = 2
dashboardDataColumnStart = 7 ' Column for the data on the dashboard

'Get user input
searchValue = dashboard.Range("C2").Value
fieldValue = dashboard.Range("C4").Value

'Clear Dashboard
'Call Clear_Data()

'Figure out by which field we will search.
If (fieldValue = "CONTACT") Then
    searchField = 3
ElseIf (fieldValue = "CONTACT POSTAL") Then
    searchField = 9
End If

'Loop through the worksheets
For Each ws In wbSlave.Worksheets
    
    'Ignore the Dashboard worksheet
    If (ws.Name <> "Dashboard" And ws.Name <> "Sheet3" And ws.Name <> "WorkSheet1") Then
    
        'Get the range values into a variable that can be looped through.
        'Example usage: dataArray(1,1) [row,column]
        'Simple version: ws.Range(Cells(1,1),Cells(2,2)).Value
        dataArray = ws.Range(ws.Cells(dataRowStart, dataColumnStart), ws.Cells(ws.Cells(Rows.Count, dataColumnStart).End(xlUp).Row, dataColumnEnd)).Value
    
        'Increase size of array that will hold the data to display to its max possible size for the current worksheet.
        ReDim datatoShowArray(1 To UBound(dataArray, 1), 1 To UBound(dataArray, 2))
        
        'Row increment for the final data array (datatoShowArray).
        J = 1

        'Loop through the rows in the data range.
        For i = 1 To UBound(dataArray, 1)
            
            'Check if the value in the row equals our search value
            If (dataArray(i, searchField) = searchValue) Then
                
                'MATCH FOUND! Now do something!
                
                'Loop through the columns in the data range so can get their values.
                For k = 1 To UBound(dataArray, 2)
                    
                    'Add values to the array that will be used to put data into the Dashboard.
                    datatoShowArray(J, k) = dataArray(i, k)
                    
                Next k
                
                'Increment the counter for the datatoShowArray
                J = J + 1
                
            End If
            
        Next i
        
        'Find next empty row in the dashboard.
        nextRow = dashboard.Cells(Rows.Count, dashboardDataColumnStart).End(xlUp).Row + 1

        'Put data into the dashboard.
        'Format = Range(Cells(1,1),Cells(2,2)).Value = datatoShowArray
        'dashboard.Range(Cells(nextRow, dashboardDataColumnStart), Cells(nextRow + UBound(datatoShowArray, 1) - 1, dashboardDataColumnStart + dataColumnWidth)).Value = datatoShowArray
        dashboard.Range(dashboard.Cells(nextRow, dashboardDataColumnStart), dashboard.Cells(nextRow + UBound(datatoShowArray, 1) - 1, dashboardDataColumnStart + dataColumnWidth)).Value = datatoShowArray
    
    End If
    
'Go to the next worksheet.
Next ws

'Application.ScreenUpdating = True 'Turn it back on at the end of the macro!

End Sub
Sub Clear_Data()

'Dashboard sheet
Set dashboard = Sheets("Dashboard")

'Data table information
dashboardDataColumnStart = 7 ' Column for the data on the dashboard
dashboardDataRowStart = 2

dashboard.Range(dashboard.Cells(dashboardDataRowStart, dashboardDataColumnStart), dashboard.Cells(Rows.Count, Columns.Count)).Clear

End Sub
Sub Search_Separate_Workbooks()
' TeachExcel.com

Dim wbMaster As Workbook
Dim wbSlave As Workbook
Dim filePicker As FileDialog

'Dont update the screen - makes it work faster
Application.ScreenUpdating = False

'Clear Dashboard
Call Clear_Data

'Set the master workbook - one that will display the results.
Set wbMaster = ActiveWorkbook

'Setup the file picker dialog box/window.
Set filePicker = Application.FileDialog(msoFileDialogFilePicker)

'Control the file picker.
With filePicker
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xl*" ' Show only Excel files
    .AllowMultiSelect = True
    .Title = "My Title!"
End With

'Get the button that was pressed.
filePickerButtonClicked = filePicker.Show

'Check if use hit the Cancel button or not.
If filePickerButtonClicked <> 0 Then
    'File(s) was selected, now do something!
    
    'Loop through selected files.
    For Each fileSelected In filePicker.SelectedItems
    
        ' Save a reference to, and open, the workbook to search through.
        Set wbSlave = Workbooks.Open(fileSelected)
        
        ' Open the method that searches the workbooks and tell them which workbook to search and where to place the data.
        Call Data_Search(wbMaster, wbSlave)
        
        ' Close the workbook that was searched through and don't save any changes to it, if any were made.
        wbSlave.Close SaveChanges:=False
    
    Next fileSelected

Else

    'Cancel button clicked.
    MsgBox "No File Selected!"
    
End If

'Turn on screen updating again - makes Excel usable
Application.ScreenUpdating = True

End Sub

[/CODE]

Answer
Discuss

Answers

0
Selected Answer

Hi Mahadiyu and welcome to the Forum.

(REVISED answers, 04 January 2023 based on files attached to Question)

The problem is that you did not modifiy the code which sets the value of the variable "searchField".

You had this portion:

'Figure out by which field we will search.
If (fieldValue = "CONTACT") Then
    searchField = 3
ElseIf (fieldValue = "CONTACT POSTAL") Then
    searchField = 9
End If

but the data validation on cell C4 alllows the value CONTACTS (and other values) not CONTACT or CONTACT POSTAL so the variable has no value and the error occurs.

In the attached file, I've copied the single search macro to a standard module and changed that portion to read as follows:

'Figure out by which field we will search.
Select Case FieldValue
    Case "CONTACTS"
        searchField = 3
    Case "SUBJECT CELL"
        searchField = 6
    Case "CONTACT CELL"
        searchField = 7
    Case "SUBJECT IMEI"
        searchField = 8
    Case "CONTACT IMEI"
        searchField = 9
End Select

I've assigned that corrected macro to the Search button on tbe dashboard. Now you should find that the file/ code works very well.

Hope this helps. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

I have tried this yet having the same error, I am trying to ttach the files but couldnt see the option to do so.
mahadiyu (rep: 2) Jan 4, '23 at 1:32 am
To attach an Excel file, edit your question and use the Attach Files... button below the text box. 
John_Ru (rep: 6152) Jan 4, '23 at 1:54 am
Thanks, I have done it, the Fast Search is the Dashboard workbook, while the Search File is the Workbook to make the search from and return to the dashboard work book
mahadiyu (rep: 2) Jan 4, '23 at 2:07 am
Have revised my answer and corrected file. 

Actually I don't think your original subject title is fair to refer to an error (since the error arises from your changes to the file/code, not the original code). Kindly change it to something like "Problems using the Fast Search macro with multiple criteria". Thanks in advance (and for hopefully selecting my Answer).
John_Ru (rep: 6152) Jan 4, '23 at 3:53 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login