Sub FormatData()
'Used to convert results that are 'numbers' from TEXT to NUMERIC
Dim varMax As Long
Dim varLastRow As Long
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim fmt As Worksheet: Set fmt = shFormat
Dim rpt As Worksheet: Set rpt = shReport
Dim Lookup_Value As String
Dim If_Not_Found As String
Dim Result As Variant
Dim Lookup_Array As Object
Dim This_Row As Long
varLastRow = shReport.Range("A" & Rows.Count).End(xlUp).Row
varMax = shFormat.Range("A" & Rows.Count).End(xlUp).Row
Set Lookup_Array = CreateObject("Scripting.Dictionary")
For This_Row = 2 To varMax
Lookup_Array.Item(shFormat.Cells(This_Row, 1).Value & "|" & shFormat.Cells(This_Row, 2).Value & "|" & shFormat.Cells(This_Row, 3).Value) = shFormat.Cells(This_Row, 4).Value
Next This_Row
'Perform after all queries
For Each cl In shReport.Range("E4:G" & varLastRow).Cells
If IsNumeric(cl.Value) Then
'Inputs
Lookup_Value = rpt.Cells(cl.Row, 1).Value & "|" & rpt.Cells(cl.Row, 2).Value & "|" & rpt.Cells(cl.Row, 3).Value
If_Not_Found = "Not Found"
'Perform XLOOKUP and Store To Variable
If Lookup_Array.Exists(Lookup_Value) Then
Result = Lookup_Array.Item(Lookup_Value)
Else
Result = If_Not_Found
End If
'***********************************************************************
'***********************************************************************
Select Case Result
Case "0" '0-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0")
Case "1" '1-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.0")
Case "2" '2-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.00")
Case "3" '3-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.000")
Case "4" '4-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.0000")
Case "5" '5-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.00000")
Case "6" '6-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.000000")
Case "7" '7-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.0000000")
Case "8" '8-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.00000000")
Case "9" '9-decimals
cl.Value = Format(Val(Range(cl.Address)), "#,##0.000000000")
Case "General" 'AS-IS
cl.Value = Val(Range(cl.Address))
Case "Date1" 'M/D/YY
cl.Value = Format(Val(Range(cl.Address)), "MM-DD-YY")
Case "Date2" 'MM/DD/YYYY
cl.Value = Format(Val(Range(cl.Address)), "MM-DD-YYYY")
Case "Date3" 'D-MMM-YY
cl.Value = Format(Val(Range(cl.Address)), "D-MMM-YY")
Case "Date4" 'DD-MMM-YY
cl.Value = Format(Val(Range(cl.Address)), "DD-MMM-YY")
Case "Date5" 'DD-MMM-YYYY
cl.Value = Format(Val(Range(cl.Address)), "DD-MMM-YYYY")
Case "Date6" 'D-MMM-YYYY
cl.Value = Format(Val(Range(cl.Address)), "D-MMM-YYYY")
Case "Date7" 'M/D/YY H:NN AM/PM
cl.Value = Format(Val(Range(cl.Address)), "M/D/YY H:mm AM/PM")
Case "Date8" 'M/D/YY H:NN 24
cl.Value = Format(Val(Range(cl.Address)), "MM-DD-YY HH:mm")
Case "Date9" 'H:NN AM/PM
cl.Value = Format(Val(Range(cl.Address)), "H:mm AM/PM")
Case "Date10" 'H:NN 24
cl.Value = Format(Val(Range(cl.Address)), "HH:mm")
Case "Exponential1" '2-decimals + 2 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.00E+00")
Case "Exponential2" '3-decimals + 2 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.000E+00")
Case "Exponential3" '4-decimals + 2 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.0000E+00")
Case "Exponential4" '1-decimals + 3 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.0E+000")
Case "Exponential5" '2-decimals + 3 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.00E+000")
Case "Exponential6" '3-decimals + 3 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.000E+000")
Case "Exponential7" '4-decimals + 3 Expo
cl.Value = Format(Val(Range(cl.Address)), "0.0000E+000")
End Select
End If
Next
'Perform after ALL queries and formatting
For Each cl In Range("TrendTop", Cells(varLastRow, Range("TrendTop").Column)).Cells
cl.Formula = "=IF(OR(ISTEXT(" & Cells(cl.Row, 5).Address & _
"),ISTEXT(" & Cells(cl.Row, 6).Address & _
"),ISTEXT(" & Cells(cl.Row, 7).Address & ")),"""",IF(AND(" & _
Cells(cl.Row, 5).Address & ">" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & ">" & Cells(cl.Row, 7).Address & "),DD,IF(AND(" & _
Cells(cl.Row, 5).Address & "<" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "<" & Cells(cl.Row, 7).Address & "),UU,IF(AND(" & _
Cells(cl.Row, 5).Address & "=" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "=" & Cells(cl.Row, 7).Address & "),SS,IF(AND(" & _
Cells(cl.Row, 5).Address & ">" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "=" & Cells(cl.Row, 7).Address & "),DS,IF(AND(" & _
Cells(cl.Row, 5).Address & "<" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "=" & Cells(cl.Row, 7).Address & "),US,IF(AND(" & _
Cells(cl.Row, 5).Address & ">" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "<" & Cells(cl.Row, 7).Address & "),DU,IF(AND(" & _
Cells(cl.Row, 5).Address & "<" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & ">" & Cells(cl.Row, 7).Address & "),UD,IF(AND(" & _
Cells(cl.Row, 5).Address & "=" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & ">" & Cells(cl.Row, 7).Address & "),SD,IF(AND(" & _
Cells(cl.Row, 5).Address & "=" & Cells(cl.Row, 6).Address & "," & Cells(cl.Row, 6).Address & "<" & Cells(cl.Row, 7).Address & "),SU,""Error""))))))))))"
Next cl
End Sub
WBD
Bookmarks