Change Column Type and Format - excel

I have a spreedsheet with more than 200k rows and 85 columns. I need to change the column type to a specify format for each Column if the Column Name is found on another Sheet. The problem is, the code below is taking more than 25 min to finish.
aTemplate = The Sheet with I have the Column name and Types
aTemplate2 = The Sheet with my Data which I need to format each Column
Is it possible to improve this method? See the code Below.
aTemplate = Worksheets("Format Parameters").Range("A2", Worksheets("Format Parameters").Cells(Rows.Count, "B").End(xlUp))
aTemplate2 = Worksheets("DADOS").Range("A1", Worksheets("DADOS").Cells(Rows.Count, "CI").End(xlUp))
lCount2 = UBound(aTemplate2, 1)
For cCount2 = LBound(aTemplate2, 2) To UBound(aTemplate2, 2)
For lCount = LBound(aTemplate, 1) To UBound(aTemplate, 1)
If aTemplate(lCount, 1) = aTemplate2(1, cCount2) Then 'Compare Column name
For i = 2 To lCount2 'for each row
Select Case aTemplate(lCount, 2) ' Verify the type which I need to change the column
Case "Text"
With ActiveCell(i, cCount2)
.NumberFormat = "#"
.Value = .Value
End With
Case "Integer"
With ActiveCell(i, cCount2)
.NumberFormat = "0"
.Value = .Value
End With
Case "Date"
With ActiveCell(i, cCount2)
.NumberFormat = "mm/dd/yyyy"
.Value = .Value
End With
Case "Decimal"
With ActiveCell(i, cCount2)
.NumberFormat = "0.000"
.Value = .Value
End With
Case Else
End Select
Next i
Exit For
End If
Next
Next
End Sub
I believe that the ActiveCell Method is why is taking so much to run this function.

Something like the following should be exponentially faster.
Loop over each column in the main table.
Look up the column name (using Match) in the first column of the lookup table.
Return the corresponding format type from the second column of the lookup table.
Apply the corresponding number format to the entire column of the main table.
With Worksheets("Format Parameters")
Dim aTemplate As Range
Set aTemplate = .Range("A2", .Cells(.Rows.Count, "B").End(xlUp))
End With
With Worksheets("DADOS")
Dim aTemplate2 As Range
Set aTemplate2 = .Range("A1", .Cells(.Rows.Count, "CI").End(xlUp))
End With
With aTemplate2
Dim col As Range
For Each col In .Columns
Dim matchRow
matchRow = Application.Match(col.Cells(1).Value, aTemplate.Columns(1), 0)
If Not IsError(matchRow) Then
With col.Offset(1).Resize(.Rows.Count - 1)
Select Case aTemplate.Columns(2).Cells(matchRow).Value
Case "Text"
.NumberFormat = "#"
.Value = .Value
Case "Integer"
.NumberFormat = "0"
.Value = .Value
Case "Date"
.NumberFormat = "mm/dd/yyyy"
.Value = .Value
Case "Decimal"
.NumberFormat = "0.000"
.Value = .Value
End Select
End With
End If
Next
End With

Related

VBA Highlight matching records

I wanted to know how to apply conditional formatting rules using vba...
I have the following dataset:
As you can see the first column is the UNIQUE_ACCOUNT_NUMBER which consists usually in two matching records, followed by other columns showing data related to the account numbers.
I want to apply a conditional formatting rule that if UNIQUE_ACCOUNT_NUMBER is matching, but any other column isnt(for the two matching records) then I want to highlight it yellow.
For example:
As you can see the account number MTMB^1^10000397 was matching twice but the Arrears_flag wasnt matching so i want to highlight it yellow.
I hope this makes sense.
In this example I can only apply the match & Mismatch for one column...
Dim rg1 As Range
Set rg1 = Range("E3", Range("E3").End(xlDown))
Dim uv As UniqueValues
Set uv = rg1.FormatConditions.AddUniqueValues
uv.DupeUnique = xlDuplicate
uv.Interior.Color = vbRed
Thanks!
I managed to get it working, adding a new helper column, concatenating the account number and the "ACC_HEADER_POOL" column in column "I", and using =COUNTIF(I$2:I$5,I2)=1 as the formula on which the conditional formatting is based, as you can see in this screenshot:
Please find the answer
Sub actin()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = 2 To Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
mrow = 0
If Application.WorksheetFunction.CountIf(Sheet1.Range(Sheet1.Cells(1, "E"), Sheet1.Cells(i - 1, "E")), Sheet1.Cells(i, "E")) > 0 Then
mrow = Application.WorksheetFunction.Match(Sheet1.Cells(i, "E"), Sheet1.Range(Sheet1.Cells(1, "E"), Sheet1.Cells(i - 1, "E")), 0)
End If
If mrow = 0 Then GoTo eee
If Sheet1.Cells(i, "G") <> Sheet1.Cells(mrow, "G") Then
Sheet1.Cells(i, "G").Interior.Color = vbYellow
Sheet1.Cells(mrow, "G").Interior.Color = vbYellow
End If
If Sheet1.Cells(i, "H") <> Sheet1.Cells(mrow, "H") Then
Sheet1.Cells(i, "H").Interior.Color = vbYellow
Sheet1.Cells(mrow, "H").Interior.Color = vbYellow
End If
eee:
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Excel AutoFilter Criteria Variant to string

I'm trying to get an AutoFilter setting as a string.
I set A1:A5 in a worksheet to:
Rows
valA
valB
valC
valD
Then I AutoFilter the worksheet and select Rows values valA, valB, valC. (I.e., I have filtered out Rows value valD.)
I run the following VBA:
Sub CaptureFilters()
With ActiveSheet.AutoFilter.Filters.Item(1)
Cells(1, 3).Value = Join(.Criteria1)
Cells(1, 4).Value = Replace(Join(.Criteria1), "#=", "")
End With
End Sub
Now both worksheet cells C1 and D1 give a #NAME? error, but their formulas show =#valA =#valB =#valC.
VBA shows that .Criteria1 is an Array of Variant/Strings.
How can I get .Criteria1 as a string value in a worksheet cell? In this example I want a cell that contains the string valA valB valC.
Rewrite to
Sub CaptureFilters()
With ActiveSheet.AutoFilter.Filters.Item(1)
Cells(1, 3).Value2 = "'" & Join(.Criteria1)
Cells(1, 4).Value = "'" & Replace(Join(.Criteria1), "#=", "")
End With
End Sub
Otherwise Excel will interpret the string as formula as it starts with a =.
Or you try
Sub CaptureFiltersA()
With ActiveSheet.AutoFilter.Filters.Item(1)
Cells(1, 3).Value2 = Replace(Join(.Criteria1), "=", " ")
Cells(1, 4).Value = Replace(Join(.Criteria1), "=", "")
End With
End Sub
There are four scenarios for a text autofilter – one value, two, and more than two. To handle them all:
Sub CaptureFiltersA()
With ActiveSheet.AutoFilter.Filters.Item(1)
If IsArray(.Criteria1) Then ' CASE: More than two values
Cells(1, 3).Value = Replace(Join(.Criteria1, ","), "=", "")
Else
Cells(1, 3).Value = Replace(.Criteria1, "=", "") ' CASE: One value
If .Count = 2 Then ' CASE: Two values
Cells(1, 3).Value = Cells(1, 3).Value + "," + Replace(.Criteria2, "=", "")
End If
End If
End With
End Sub

I got "Type Mismatch" error in multiple condition scenario ( case function is used)

I am trying to do a vba code for multiple conditions and selection scenario.
Column AR is the column I would like to show the result of the selection.
My selection logic is:
If Column E (cells(m,5)) equals "Not IBC", then put value in the cell in AR (cells(m,44)) as "Exclude"
If Column AQ (cells(m,43)) equals "Yes", then put value in the cell in AR (cells(m,44)) as "Exclude"
If Column W (cells(m,23)) equals "X" or "Y", then put value in the cell in AR (cells(m,44)) as "Exclude"
If Column G (cells(m,7)) equals "A" or "B" or "C" and Column I (cells(m,9)) is not equal to "1.1" nor "1.2", then put value in the cell in AR (cells(m,44)) as "Exclude"
Rest would give AR as Include
My actual code is
However, I am given Type Mismatch Error even I changed the cell type to General.
Is there a way to remove the Type Mismatch Error or if my code is correct based on the logic?
Thank you!
Sub inclusion()
Columns("AR:AR").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AR:AR").Cells(1, 1) = "Include?"
Application.ScreenUpdating = False
With Range("G:G") '
.NumberFormat = "General"
.Value = .Value
.NumberFormat = "0"
End With
Application.ScreenUpdating = True
With Range("I:I") '
.NumberFormat = "General"
.Value = .Value
.NumberFormat = "0"
End With
Application.ScreenUpdating = True
Dim lastRow4 As Long
lastRow4 = Range("K" & Rows.Count).End(xlUp).Row
Dim legacy As String
Dim tax As String
Dim inclusion As String
Dim IBC As String
Dim pact As String
Dim agg As String
Dim m As Long
For m = 2 To lastRow4
legacy = Cells(m, 7)
tax = Cells(m, 9)
IBC = Cells(m, 5)
pact = Cells(m, 23)
agg = Cells(m, 43)
Select Case True
Case IBC = "Not IBC"
inclusion = "Exclude"
Case agg = "Yes"
inclusion = "Exclude"
Case (pact = "X" Or Impact = "Y")
inclusion = "Exclude"
Case (legacy = "A" Or legacy = "B" Or legacy = "C") And tax <> "1.1" And tax <> "1.2"
inclusion = "Exclude"
Case Else
inclusion = "Include"
Cells(m, 44) = inclusion
End Select
Next m
End Sub

How to identify all format properties in each cell VBA?

I want to get the format properties of each cell in column E. I have no issue to identify the properties if the text that is
in each cell has a unique Font.Name, Font.Size, Font.ColorIndex and Font.FontStyle.
The issue I have is that in some cells there is text with one format and other text with another and in that case my current code it seems that is only printing
the format of the first character.
for example in one cell is abc def., where abc is Arial, 9, black, Regular and def is Calibri, 18, green, Bold:
How to identify all format properties in each cell?
My current code is below:
Sub GetFormat()
For i = 1 To 8
nName = Cells(i, "E").Font.Name
sSize = Cells(i, "E").Font.Size
cColor = Cells(i, "E").Font.ColorIndex
sStyle = Range("E" & i).Font.FontStyle
Cells(i, "A") = nName
Cells(i, "B") = sSize
Cells(i, "C") = cColor
Cells(i, "D") = sStyle
Next
End Sub
You'll need to loop over each cell's Characters collection and check each character in turn.
For example:
Sub Tester()
Dim rng As Range, c, n As Long
Set rng = Range("A1")
For n = 1 To rng.Characters.Count
With rng.Characters(n, 1)
Debug.Print n, "Bold", .Font.Bold
Debug.Print n, "Color", .Font.ColorIndex
End With
Next
End Sub

Search String and If found then paste value - then loop

I'm trying to build a macro that searches a column for two Strings ("Tip Fee" or "Non-Deal". If it finds this then it pastes a "Y" value in another column. If it doesn't then it pastes "N".
I'm struggling to get it to work and not sure what to do for the "not equal to then "N") part.
Example for just finding "Tip Fee" below:
Sheets("Pipeline simplified").Select
Dim TipFee As String
Dim NonDeal As String
Dim t As Integer
Dim LastRowtip As Long
TipFee = "Tip Fee"
NonDeal = "Non-Deal"
LastRowtip = Cells(Rows.Count, "H").End(xlUp).Row
For t = 7 To LastRowtip
If Cells(t, 8).Value = TipFee Then
Cells(t, 30).Value = "Y"
End If
Next t
Can still use a formula in VBA, that way there's no need to loop. Formula can be made to search for text within the cell, and can also be case insensitive. Then just convert to values afterwards.
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Pipeline simplified")
With ws.Range("AD7:AD" & ws.Cells(ws.Rows.Count, "H").End(xlUp).Row)
.Formula = "=IF(OR(ISNUMBER(SEARCH({""Tip Fee"",""Non-Deal""},H7))),""Y"",""N"")"
.Value = .Value
End With
Try this:
If Cells(t, 8).Value = TipFee Or Cells(t, 8).Value = NonDeal Then
Cells(t, 30).Value = "Y"
Else
Cells(t, 30).Value = "N"
End If
Also, check how IF sentence works:
If...Then...Else statement

Resources