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

In Fast Search Entire Excel Workbook & Return All Results into a Dashboard

0

How to ge the search values vertically rather than horizontally? 

The output should be :

ID

NAME 

REGION 1

Rather than 

ID  NAME REGION 1

What changes are required in the below code to get the output vertically?

nextRow = dashboard.Cells(Rows.Count, dashboardDataColumnStart).End(xlUp).Row + 1

ashboard.Range(dashboard.Cells(nextRow, dashboardDataColumnStart), dashboard.Cells(nextRow + UBound(datatoShowArray, 1) - 1, dashboardDataColumnStart + dataColumnWidth)).Value = datatoShowArray

Answer
Discuss

Answers

0
Selected Answer

Nigel

In the modified file attached, I've changed one variable to say which column (rather than row) the data should start from. I then let the macro gather data as before. Once that's done (for a worksheet), the macro finds the next free column in row 10 and pastes the same array datatoShowArray but it's (perhaps sneakily) transposed using the function:

Application.WorksheetFunction.Transpose(datatoShowArray)
The changes to  macro are in bold in the extract below (full code in the file):
Sub Data_Search()
' TeachExcel.com

.....
...... << code unchanged>>
......
......          
dashboardDataRowStart = 10 ' Row for the data on the dashboard
......
...... << other code unchanged>>
......
......          
        Next i

        'Find next empty column in the dashboard.
        nextColumn = dashboard.Cells(dashboardDataRowStart, Columns.Count).End(xlToLeft).Column + 1

        'Put data into the dashboard.
        'Format = Range(Cells(1,1),Cells(2,2)).Value = datatoShowArray
        dashboard.Range(Cells(dashboardDataRowStart, nextColumn), Cells(dashboardDataRowStart + dataColumnWidth, nextColumn + UBound(datatoShowArray, 1) - 1)).Value = Application.WorksheetFunction.Transpose(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
I also modified the data to clear the dashboard, as follows (change in bold):
Sub Clear_Data()

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

'Data table information
dashboardDataColumnStart = 3 ' Column for the data on the dashboard
dashboardDataRowStart = 10 ' Changed for vertical headers

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

End Sub
Hope this works for you.
Discuss

Discussion

thats brilliant it works. Thank you for the quick and detailed response. I have another question for which il raise it in the forum 
Nigel (rep: 6) Dec 26, '21 at 1:59 pm
Glad it worked for you. Thanks for selecting my answer Nigel.
John_Ru (rep: 6142) Dec 26, '21 at 3:27 pm
Add to Discussion
0

Is there any way the "dataArray" can be filled using currentregion and other stuff remains same. I am not expert but just a thought came in my mind. Would it become simpler or more complicated than current code. Also I am having dificulty in understanding the last portion of the below expression, the one that picks the last value of the sheet.

dataArray = ws.Range(ws.Cells(dataRowStart, dataColumnStart), ws.Cells(ws.Cells(Rows.Count, dataColumnStart).End(xlUp).Row, dataColumnEnd)).Value

Discuss

Discussion

Hi Rahul and welcome to the Forum. 

The above is not an Answer (in terms of this Forum)-  really you should ask a new question, referring back to this one. It would be helpful if you could also attach a representative Excel file to that new question too. You could then delete your Answer.
John_Ru (rep: 6142) Jan 20, '23 at 2:51 am
BTW in the line:
dataArray = ws.Range(ws.Cells(dataRowStart, dataColumnStart), ws.Cells(ws.Cells(Rows.Count, dataColumnStart).End(xlUp).Row, dataColumnEnd)).Value
the first argument of the Range defines the first cell of the desired range. The second argument (in bold) determines the last cell using the (defined) last column, preceded by the determined last used row in that column. To understand that, please see Don's tutorial here:
Get the Last Row using VBA in Excel
and search for the one about last column via VBA (in the Tutorials page).

It's kind of equivalent to CurrentRegion (from the upper left cell, provided there are no columns beyond the column defined as dataColumnEnd) but omitting the first header row..
John_Ru (rep: 6142) Jan 20, '23 at 3:15 am
Add to Discussion


Answer the Question

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