Dears, i used search entire workbook return all results in Excel but i need to change cells format like (date , time, text , border ... Etc) every time doing it reset once press search or clear button, and fix 16 digit format
Dears, i used search entire workbook return all results in Excel but i need to change cells format like (date , time, text , border ... Etc) every time doing it reset once press search or clear button, and fix 16 digit format
Hi Khaled and welcome to the Forum.
One simple way to do that is to use the header row as a reference range and apply any formatting you like there - borders, fill, date, time, italics etc. (per column, even though the header text might not be affected by the formatting).
In the attached file I've just put different colour fills in B8:N8 and set the format of D8:N8 as Number but to one decimal place (for the purposes of illustration only).
Then I've added a bit to the end of the code (extract below):
'Go to the next worksheet.
Next ws
'Application.ScreenUpdating = True 'Turn it back on at the end of the macro!
End Sub
as follows (with changes and comments in bold for your guidance):
'Go to the next worksheet.
Next ws
' format the results, based on reference row 10
With dashboard.Range("B10:N10")
' copy formatting from reference row...
.Copy
'paste to all new data
With .CurrentRegion
.PasteSpecial xlPasteFormats
' but remove Bold apart from first row
.Offset(1, 0).Font.Bold = False
End With
End With
End Sub
Now when you click Search, you'll get the data in a coloured table, with borders and number formatting. Just the top row will be bold. The Clear button will work as before (leaving that reference row/formatting in place).
I added Freeze Panes to the sheet at the last minute (so you can scroll down the Search results but still see the bold headings (reference row)
Hope this helps. If so, please don't forget to mark the Answer as Selected (for the guidance of others and to add to each of our reputations on this Forum).
REVISION:
Regarding your secondary issue of displaying more than 16 digits in column 6 (F) of the data, here's a way around that. If you store your account numbers like:
'5118649865412354
you will see the full number (see cell F2 in worksheet Jan). The problem is that Excel stores them in the arrays as the full string 5118649865412354 (without the leading ') but then reverts to 15-digit accuracy when putting values in cells (so the last 4 becomes a 0).
In the SECOND file below, cell G10 (in the "reference row") is formatted as TEXT.
The code is changed to add a new variable (in bold below):
Sub Data_Search()
' TeachExcel.com
Dim ws As Worksheet
Dim dashboard As Worksheet
Dim dataArray As Variant
Dim datatoShowArray As Variant
Dim MoreThan15Dig As Integer ' identify the column with more than 15 digits e.g. an account number
and I've modified the portion which stored values in the array. If it's in 6 and a string in the array, it adds a single quotation mark so the value is stored as '5118649865412354 and will appear as 5118649865412354 is the Dashboard search results. Additional code is in bold below:
'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)
'if it's in the named MoreThan15Dig column and is a string...
If k = MoreThan15Dig And VarType(dataArray(i, k)) = vbString Then
'...add a single quotation mark to start of string...
datatoShowArray(j, k) = Chr(39) & dataArray(i, k)
'Add values to the array that will be used to put data into the Dashboard.
Else
datatoShowArray(j, k) = dataArray(i, k)
End If
Next k
Search for Jim and Name and it will be in the first result row.
Hope this fixes your second problem too.