I currently have a macro that will apply conditional formatting to a cell (in this it is cell "G" but I think it would be better if I was able to have the macro apply the conditional formatting to whatever cell I have selected so that I am not limited to only the cell I have setup in the macro.
Sub ColorCoringPluskey()
'
' ColorCoringPluskey Macro
'
Dim wb As Workbook
Dim wsKey As Worksheet
Dim wsFees As Worksheet
Dim aKeyColors(1 To 20, 1 To 2) As Variant
Dim aOutput() As Variant
Dim sKeyShName As String
Dim i As Long, j As Long
Set wb = ActiveWorkbook
Set wsFees = wb.Sheets("Fees")
sKeyShName = "Color Coding Key"
On Error Resume Next
Set wsKey = wb.Sheets(sKeyShName)
On Error GoTo 0
If wsKey Is Nothing Then
Set wsKey = wb.Sheets.Add(After:=ActiveSheet)
wsKey.Name = sKeyShName
With wsKey.Range("A1:B1")
.Value = Array("Word", "Color")
.HorizontalAlignment = xlCenter
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Else
wsKey.Range("A2:B" & wsKey.Rows.Count).Clear
End If
aKeyColors(1, 1) = "Strategize": aKeyColors(1, 2) = 10053120
aKeyColors(2, 1) = "Coordinate": aKeyColors(2, 2) = 13421619
aKeyColors(3, 1) = "Committee": aKeyColors(3, 2) = 16777062
aKeyColors(4, 1) = "Attention": aKeyColors(4, 2) = 2162853
aKeyColors(5, 1) = "Work": aKeyColors(5, 2) = 5263615
aKeyColors(6, 1) = "Circulate": aKeyColors(6, 2) = 10066431
aKeyColors(7, 1) = "Numerous": aKeyColors(7, 2) = 13158
aKeyColors(8, 1) = "Follow up": aKeyColors(8, 2) = 39372
aKeyColors(9, 1) = "Attend": aKeyColors(9, 2) = 65535
aKeyColors(10, 1) = "Attention to": aKeyColors(10, 2) = 65535
aKeyColors(11, 1) = "Print": aKeyColors(11, 2) = 10092543
aKeyColors(12, 1) = "WIP": aKeyColors(12, 2) = 13056
aKeyColors(13, 1) = "Prepare": aKeyColors(13, 2) = 32768
aKeyColors(14, 1) = "Develop": aKeyColors(14, 2) = 3394611
aKeyColors(15, 1) = "Participate": aKeyColors(15, 2) = 10092441
aKeyColors(16, 1) = "Organize": aKeyColors(16, 2) = 13369548
aKeyColors(17, 1) = "Various": aKeyColors(17, 2) = 16751103
aKeyColors(18, 1) = "Maintain": aKeyColors(18, 2) = 16724787
aKeyColors(19, 1) = "Team": aKeyColors(19, 2) = 16750950
aKeyColors(20, 1) = "Address": aKeyColors(20, 2) = 6697881
wsFees.Cells.FormatConditions.Delete
ReDim aOutput(1 To UBound(aKeyColors, 1), 1 To 2)
With wsFees.Columns("G")
For i = LBound(aKeyColors, 1) To UBound(aKeyColors, 1)
If WorksheetFunction.CountIf(.Cells, "*" & aKeyColors(i, 1) & "*") > 0 Then
j = j + 1
aOutput(j, 1) = aKeyColors(i, 1)
aOutput(j, 2) = aKeyColors(i, 2)
.FormatConditions.Add xlTextString, String:=aKeyColors(i, 1), TextOperator:=xlContains
.FormatConditions(.FormatConditions.Count).Interior.Color = aKeyColors(i, 2)
End If
Next i
End With
If j > 0 Then
wsKey.Range("A2").Resize(j, 1).Value = aOutput
For i = 1 To j
wsKey.Cells(i + 1, "B").Interior.Color = aOutput(i, 2)
Next i
wsKey.Columns("A").EntireColumn.AutoFit
End If
End Sub
Thanks for any help!
Related
I am creating an useform to create, modify, search and delete data.
when I'm running the code to create data, it shows the error:
Run-time error '-2147417848(80010108)':
Method'_Default' of object 'range'failed.
I have tried to change the object (name of te worksheet.) for (activesheet.) activating the sheet where I am registering the data I want to add but still not working.
The code has several procedures, but I'm having trouble just when I am creating new data
At the beginning it worked well but now it has this issue. I am new programming.
The code fails in this area
S
et oCelda = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
oCelda = consecutivo
complete code as follow
Private Sub Cmb_Registro_Click()
Dim oCelda As Range
Dim bCheck As Boolean
Dim Consecutivo as Double, file As long, Col As long, Final As Long
Worksheets("Data").Activate
bCheck = Txt_Fecha <> "" And Cbo_Categoria <> "" And Cbo_Subcategoria <> "" And Txt_Monto <> "" _
And Cbo_Periodo <> "" And Txt_Descripcion <> ""
Col = 1
fila = 2
Cbo_Periodo.Enabled = False
Do While ActiveSheet.Cells(fila, Col) <> Empty
fila = fila + 1
Loop
Final = fila
Consecutivo = Val(ActiveSheet.Cells(Final - 1, Col))
If Consecutivo = 0 Then
consecutivo = 1
Else
consecutivo = ActiveSheet.Cells(Final - 1, Col) + 1
End If
If bCheck And optIngreso Then
*Set oCelda = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
oCelda = consecutivo*
oCelda.Offset(0, 1) = CDate(Txt_Fecha)
oCelda.Offset(0, 2) = CLng(Txt_Monto)
oCelda.Offset(0, 3) = lblIngreso.Caption
oCelda.Offset(0, 4) = Txt_Descripcion
oCelda.Offset(0, 5) = Cbo_Categoria
oCelda.Offset(0, 6) = Cbo_Subcategoria
oCelda.Offset(0, 7) = Cbo_Periodo
MsgBox "Registro completo"
Unload Me
ElseIf bCheck And optEgreso Then
*Set oCelda = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
oCelda = consecutivo*
oCelda.Offset(0, 1) = CDate(Txt_Fecha)
oCelda.Offset(0, 2) = CLng(Txt_Monto)
oCelda.Offset(0, 3) = lblEgreso.Caption
oCelda.Offset(0, 4) = Txt_Descripcion
oCelda.Offset(0, 5) = Cbo_Categoria
oCelda.Offset(0, 6) = Cbo_Subcategoria
oCelda.Offset(0, 7) = Cbo_Periodo
MsgBox "Registro completo"
Unload Me
Else
MsgBox "Faltan datos"
End If
Worksheets("Inicio").Activate
End Sub
I have an excel sheet name as Terminal Volumes In this sheet, there are three variables (Terminal LF (log Fisher Cost) and CT Cost) that are using as input. I am using the following code for working on these.
enter code here
Sub InitiateValues()
ReDim TrmPnts(11) '<<<<CHANGE HERE Total Point + SE -JJ
Set ShDat = Worksheets("Data")
Set ShDatPth = Worksheets("DataPath")
Set tvol = Worksheets("Terminal Volumes")
'ShDatPth.Range("B2:FB157").Value = 0
With TrmPnts(1)
.Cpt = "T1"
.CptColr = "&H000088"
.Row = ShDat.Cells(2, 1)
.Col = ShDat.Cells(3, 1)
.LFamt = tvol.Cells(2, 2)
.CTamt = tvol.Cells(2, 3)
End With
With TrmPnts(3)
.Cpt = "T2"
.CptColr = "&H000088"
.Row = ShDat.Cells(6, 1)
.Col = ShDat.Cells(7, 1)
.LFamt = tvol.Cells(3, 2)
.CTamt = tvol.Cells(3, 3)
End With
With TrmPnts(4)
.Cpt = "T3"
.CptColr = "&H000088"
.Row = ShDat.Cells(8, 1)
.Col = ShDat.Cells(9, 1)
.LFamt = tvol.Cells(4, 2)
.CTamt = tvol.Cells(4, 3)
End With
With TrmPnts(5)
.Cpt = "T4"
.CptColr = "&H000088"
.Row = ShDat.Cells(10, 1)
.Col = ShDat.Cells(11, 1)
.LFamt = tvol.Cells(5, 2)
.CTamt = tvol.Cells(5, 3)
End With
With TrmPnts(6)
.Cpt = "T5"
.CptColr = "&H000088"
.Row = ShDat.Cells(12, 1)
.Col = ShDat.Cells(13, 1)
.LFamt = tvol.Cells(6, 2)
.CTamt = tvol.Cells(6, 3)
End With
With TrmPnts(7)
.Cpt = "T6"
.CptColr = "&H000088"
.Row = ShDat.Cells(14, 1)
.Col = ShDat.Cells(15, 1)
.LFamt = tvol.Cells(7, 2)
.CTamt = tvol.Cells(7, 3)
End With
With TrmPnts(8)
.Cpt = "T7"
.CptColr = "&H000088"
.Row = ShDat.Cells(16, 1)
.Col = ShDat.Cells(17, 1)
.LFamt = tvol.Cells(8, 2)
.CTamt = tvol.Cells(8, 3)
End With
With TrmPnts(9)
.Cpt = "T8"
.CptColr = "&H000088"
.Row = ShDat.Cells(18, 1)
.Col = ShDat.Cells(19, 1)
.LFamt = tvol.Cells(9, 2)
.CTamt = tvol.Cells(9, 3)
End With
With TrmPnts(10)
.Cpt = "T9"
.CptColr = "&H000088"
.Row = ShDat.Cells(20, 1)
.Col = ShDat.Cells(21, 1)
.LFamt = tvol.Cells(10, 2)
.CTamt = tvol.Cells(10, 3)
End With
With TrmPnts(11)
.Cpt = "T10"
.CptColr = "&H000088"
.Row = ShDat.Cells(22, 1)
.Col = ShDat.Cells(23, 1)
.LFamt = tvol.Cells(11, 2)
.CTamt = tvol.Cells(11, 3)
End With '<<<<<<CHANGE HERE
With TrmPnts(2)
.Cpt = "SE"
.CptColr = "&H004400"
.Row = ShDat.Cells(4, 1)
.Col = ShDat.Cells(5, 1)
End With
ReDim Mtx(156, 157) ' Cell Size X,Y -JJ
For i = 1 To 156 ' Cell Size X,Y -JJ 1 To 226
For j = 1 To 157 ' Cell Size X,Y -JJ
Mtx(i, j) = ShDat.Cells(1 + i, 1 + j)
ShDatPth.Cells(1 + i, 1 + j) = 0
Next
Next
EdtMod = True
End Sub
This code working fine to input the value to T10 (Only for 10 Rows). I want to use it for T1500 (1500 Rows) for the Column A to Column C- using loop.
The structure of the worksheet is as under.
Just make a For...next loop and use the variable of the loop to calculate the correct fields like the following (I hope I interpreted the logic correctly, please check):
Sub InitiateValues()
ReDim trmpnts(11) '<<<<CHANGE HERE Total Point + SE -JJ
Set ShDat = Worksheets("Data")
Set ShDatPth = Worksheets("DataPath")
Set tvol = Worksheets("Terminal Volumes")
'ShDatPth.Range("B2:FB157").Value = 0
Dim I As Long
For I = 1 To UBound(trmpnts)
With trmpnts(I)
.Cpt = "T" & I
.CptColr = "&H000088"
.Row = ShDat.Cells(I * 2, 1)
.Col = ShDat.Cells(I * 2 + 1, 1)
.LFamt = tvol.Cells(I, 2)
.CTamt = tvol.Cells(I, 3)
End With
Next I
....
End Sub
That should do the trick...
Im having a problem in VBA to make short code. I already used a loop
but it doesnt seems to work and the 0 value of my variable is not
displaying in the active sheet that I want. I used the code below to
make it solve my problem but it make my code very long. Please help me
with this one.
Sub update()
Dim rng1 As Range
Dim rng2 As Range
Dim i As Integer
Dim count As Integer
Dim ctr As Integer
this codes are too long
For i = 9 To 30 - 1 Step 1
Set rng1 = ThisWorkbook.Sheets("hired").Range("O" & (i))
'Production
If rng1.Value2 = Range("C7").Value2 Then
Set rng2 = ThisWorkbook.Sheets("hired").Range("J" & (i))
If rng2.Value2 = "C" Then
Dim ctrC As Integer
ctrC7 = ctrC + 1
Range("K7").Value2 = ctrC7
ElseIf rng2.Value2 = "DC" Then
Dim ctrDC7 As Integer
ctrDC7 = ctrDC7 + 1
Range("J7").Value2 = ctrDC7
ElseIf rng2.Value2 = "P" Then
Dim ctrP7 As Integer
ctrP7 = ctrP7 + 1
Range("I7").Value2 = ctrP7
End If
ElseIf rng1.Value2 = Range("C8").Value2 Then
Set rng2 = ThisWorkbook.Sheets("hired").Range("J" & (i))
If rng2.Value2 = "C" Then
Dim ctrC8 As Integer
ctrC8 = ctrC8 + 1
Range("K8").Value2 = ctrC8
ElseIf rng2.Value2 = "DC" Then
Dim ctrDC8 As Integer
ctrDC8 = ctrDC8 + 1
Range("J8").Value2 = ctrDC8
ElseIf rng2.Value2 = "P" Then
Dim ctrP8 As Integer
ctrP8 = ctrP8 + 1
Range("I8").Value2 = ctrP8
End If
ElseIf rng1.Value2 = Range("C9").Value2 Then
Set rng2 = ThisWorkbook.Sheets("hired").Range("J" & (i))
If rng2.Value2 = "C" Then
Dim ctrC9 As Integer
ctrC9 = ctrC9 + 1
Range("K9").Value2 = ctrC9
ElseIf rng2.Value2 = "DC" Then
Dim ctrDC9 As Integer
ctrDC9 = ctrDC9 + 1
Range("J9").Value2 = ctrDC9
ElseIf rng2.Value2 = "P" Then
Dim ctrP9 As Integer
ctrP9 = ctrP9 + 1
Range("I9").Value2 = ctrP9
End If
If
Next
my solution to my problem but its too long. i need to make it shorter
If ctrC7 = 0 Then
Range("K7").Value2 = ""
ElseIf ctrDC7 = 0 Then
Range("J7").Value2 = ""
ElseIf ctrP7 = 0 Then
Range("I7").Value2 = ""
ElseIf ctrC8 = 0 Then
Range("K8").Value2 = ""
ElseIf ctrDC8 = 0 Then
Range("J8").Value2 = ""
ElseIf ctrP8 = 0 Then
Range("I8").Value2 = ""
ElseIf ctrC9 = 0 Then
Range("K9").Value2 = ""
ElseIf ctrDC9 = 0 Then
Range("J9").Value2 = ""
ElseIf ctrP9 = 0 Then
End If
End Sub
I'm not sure if i'm following your logic right but you can give this a try.
It's a little shorter
Option Explicit
Sub update()
Dim rng1 As Range
Dim rng2 As Range
Dim i As Long
Dim ArrClear(0 To 2, 1 To 3) As Boolean
Dim Ws As Worksheet
For i = 9 To 29 Step 1
Set rng1 = ThisWorkbook.Sheets("hired").Range("O" & (i))
Set rng2 = ThisWorkbook.Sheets("hired").Range("J" & (i))
'Production
If rng1.Value2 = Range("C7").Value2 Then
If rng2.Value2 = "C" Then
ArrClear(0, 1) = True
ElseIf rng2.Value2 = "DC" Then
ArrClear(0, 2) = True
ElseIf rng2.Value2 = "P" Then
ArrClear(0, 3) = True
End If
ElseIf rng1.Value2 = Range("C8").Value2 Then
If rng2.Value2 = "C" Then
ArrClear(1, 1) = True
ElseIf rng2.Value2 = "DC" Then
ArrClear(1, 2) = True
ElseIf rng2.Value2 = "P" Then
ArrClear(1, 3) = True
End If
ElseIf rng1.Value2 = Range("C9").Value2 Then
If rng2.Value2 = "C" Then
ArrClear(2, 1) = True
ElseIf rng2.Value2 = "DC" Then
ArrClear(2, 2) = True
ElseIf rng2.Value2 = "P" Then
ArrClear(2, 3) = True
End If
End If
Next
For i = LBound(ArrClear()) To UBound(ArrClear())
If ArrClear(i, 1) Then
Range("K" & 7 + i).Value2 = ""
End If
If ArrClear(i, 2) Then
Range("J" & 7 + i).Value2 = ""
End If
If ArrClear(i, 3) Then
Range("I" & 7 + i).Value2 = ""
End If
Next i
End Sub
I have the follow script to put a list of people with there know skills in an array and then match the first match with a customer with the same skill. Every time it runs the results are the same. I would like to have it be a random order of the array, but keeping the two columns in the array together. How can I shuffle(rearrange) the array that keeps the rows in the array the same? Or would it be better to erase the array, randomly sort the columns and set the array back up?
Sub Assign()
Dim arOne()
ReDim arOne(1000, 15)
Dim o As Integer
Dim p As Integer
Dim StartTime As Double
Dim MinutesElapsed As String
p = 0
o = 0
For i = 2 To 920
If Cells(i, 12).Value <> Cells(i - 1, 12) Then
p = p + 1
arOne(p, 0) = Cells(i, 12).Value
arOne(p, 1) = Cells(i, 13).Value
o = 2
Else
arOne(p, o) = Cells(i, 13).Value
o = o + 1
End If
Next
For i = 2 To 612
For o = LBound(arOne, 1) + 1 To UBound(arOne, 1)
If arOne(o, 0) <> "" Then
iUsed = Application.WorksheetFunction.CountIf(Range("C2:C" & i), "=" & arOne(o, 0))
If iUsed < Application.WorksheetFunction.VLookup(arOne(o, 0), Range("Q2:R62"), 2, False) Then
For j = LBound(arOne, 2) + 1 To UBound(arOne, 2)
If arOne(o, j) = Cells(i, 2).Value Then
Cells(i, 3).Value = arOne(o, 0)
ActiveSheet.Calculate
GoTo NextIR
End If
Next j
End If
End If
Next o
NextIR:
Next i
End Sub
Multiple loops and multiple access to range objects makes your code very, very slow (I don't know if performance is important).
I would read all necessary data to arrays and use filter and rnd to get a random person with the relevant skill:
Option Explicit
Sub PeopleBusiness()
Dim People, Customers, FilterArray
Dim I As Long, Idx As Long
People = Application.Transpose([L2:L920 & "|" & M2:M8])
Customers = Range("A2:C612").Value2
For I = 1 To UBound(Customers, 1)
FilterArray = Filter(People, Customers(I, 2))
If UBound(FilterArray) > -1 Then
Idx = Round(Rnd() * UBound(FilterArray), 0)
Customers(I, 3) = Left(FilterArray(Idx), InStr(1, FilterArray(Idx), "|") - 1)
End If
Next I
Range("A2:C612").Value = Customers
End Sub
I was able to get done what I needed by erasing the array and redimming it after sorting the data based on a rand() number in the table. It takes about 15 minutes to run 7000 assignment but it is a lot better than 7+ hours it takes to do manually.
Sub Assign()
Dim arOne()
ReDim arOne(1000, 15)
Dim o As Integer
Dim p As Integer
Dim StartTime As Double
Dim MinutesElapsed As String
Application.Calculation = xlAutomatic
StartTime = Timer
NextIR:
ReDim arOne(1000, 15)
p = 0
o = 0
QAlr = Sheets("Sheet1").Range("L" & Rows.Count).End(xlUp).Row
For I = 2 To QAlr
If Cells(I, 12).Value <> Cells(I - 1, 12) Then
p = p + 1
arOne(p, 0) = Cells(I, 12).Value
arOne(p, 1) = Cells(I, 13).Value
o = 2
Else
arOne(p, o) = Cells(I, 13).Value
o = o + 1
End If
Next
AQAlr = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
AgtLr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For I = AQAlr + 1 To AgtLr
For o = LBound(arOne, 1) + 1 To UBound(arOne, 1)
If arOne(o, 0) <> "" Then
iUsed = Application.WorksheetFunction.CountIf(Range("C2:C" & I), "=" & arOne(o, 0))
If iUsed < Application.WorksheetFunction.VLookup(arOne(o, 0), Range("Q2:R62"), 2, False) Then
For j = LBound(arOne, 2) + 1 To UBound(arOne, 2)
If arOne(o, j) = Cells(I, 2).Value Then
Cells(I, 3).Value = arOne(o, 0)
ActiveSheet.Calculate
Erase arOne()
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[[#All],[Random '#]]"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
GoTo NextIR
End If
Next j
End If
End If
Next o
Next I
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "Assignments completed in " & MinutesElapsed & " minutes", vbInformation
End Sub
Not entirely sure I got your set-up right but you can try this:
Option Explicit
Sub Assign()
Randomize
Range("C2", Range("C" & Rows.Count).End(xlUp)).ClearContents
Dim R1 As Range: Set R1 = Range("L2:M920") 'People skill table
Dim R2 As Range: Set R2 = Range("A2:B612") 'Cusotmers skill talbe
Dim D0 As Object: Set D0 = CreateObject("scripting.dictionary")
Dim i As Integer, j As Integer, Rand as Integer
For i = 1 To R2.Rows.Count
Rand = Int(R1.Rows.Count * Rnd + 1)
For j = 1 To R1.Rows.Count
If R1.Cells(Rand, 2) = R2(i, 2) And Not D0.exists(Rand) Then
R2.Cells(i, 2).Offset(0, 1) = R1(Rand, 1)
D0.Add Rand, Rand
Exit For
End If
Rand = (Rand Mod R1.Rows.Count) + 1
Next j
Next i
End Sub
The idea is to check the people skill list starting from a random point and making sure a key is not used twice.
EDIT:
According to your comment I assume a "people / skill" can then be assigned more than once as there are 7000+ customers ?
Code below randomly assign with a fairly good distribution 1500 peoples to 7000 customers in +/- 1 second.
Have a try and see if you can adapt it to your project.
Option Explicit
Sub Assign()
Application.ScreenUpdating = False
Dim Start: Start = Timer
Randomize
Range("C2:C99999").ClearContents
Dim D1 As Object
Dim R1 As Range: Set R1 = Range("L2", Range("M" & Rows.Count).End(xlUp))
Dim R2 As Range: Set R2 = Range("A2", Range("B" & Rows.Count).End(xlUp))
Dim T1: T1 = R1
Dim T2: T2 = R2
Dim T3()
Dim a As Integer: a = 1
Dim i As Integer, j As Integer, k As Integer, Rnd_Val As Integer, j_loop As Integer
For i = 1 To (Int(R2.Rows.Count / R1.Rows.Count) + 1)
Set D1 = CreateObject("scripting.dictionary")
For j = (R1.Rows.Count * i - R1.Rows.Count + 1) To R1.Rows.Count * i
ReDim Preserve T3(1 To j)
Rnd_Val = Int(Rnd * R1.Rows.Count + 1)
For k = 1 To R1.Rows.Count
If T1(Rnd_Val, 2) = T2(j, 2) And Not D1.exists(Rnd_Val) And T3(j) = "" Then
T3(j) = T1(Rnd_Val, 1)
D1.Add Rnd_Val, Rnd_Val
Exit For
End If
Rnd_Val = (Rnd_Val Mod R1.Rows.Count) + 1
Next k
If T3(j) = "" Then
For k = 1 To R1.Rows.Count
If T1(Rnd_Val, 2) = T2(j, 2) Then
T3(j) = T1(Rnd_Val, 1)
Exit For
End If
Rnd_Val = (Rnd_Val Mod R1.Rows.Count) + 1
Next k
End If
a = a + 1
If a > R2.Rows.Count Then GoTo EndLoop
Next j
Set D1 = Nothing
Next i
EndLoop:
Range("C2").Resize(UBound(T3), 1) = Application.Transpose(T3)
Debug.Print Timer - Start
Application.ScreenUpdating = True
End Sub
I have a macro that has two parts. The first part is the upper portion that color codes based on keywords, the bottom portion highlights cells that are duplicates.
The first part has a format condition that makes it only work if the corresponding cell in column "D" is has a value of .6 or greater. I need the same thing to work for the second part.
The format condition is
FormatConditions.Add xlExpression, Formula1:="=AND(D1>0.6,ISNUMBER(SEARCH(""" & aKeyColors(i, 1) & """,G1)))"
Macro:
Sub oneSixColorCodingPluskey()
'
' oneSixColorCodingPluskey Macro
'
Dim wb As Workbook
Dim wsKey As Worksheet
Dim wsFees As Worksheet
Dim aKeyColors(1 To 29, 1 To 2) As Variant
Dim aOutput() As Variant
Dim sKeyShName As String
Dim i As Long, j As Long
Set wb = ActiveWorkbook
Set wsFees = wb.Sheets("Fees")
sKeyShName = "Color Coding Key"
On Error Resume Next
Set wsKey = wb.Sheets(sKeyShName)
On Error GoTo 0
If wsKey Is Nothing Then
Set wsKey = wb.Sheets.Add(After:=ActiveSheet)
wsKey.Name = sKeyShName
With wsKey.Range("A1:B1")
.Value = Array("Word", "Color")
.HorizontalAlignment = xlCenter
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
Else
wsKey.Range("A2:B" & wsKey.Rows.Count).Clear
End If
aKeyColors(1, 1) = "Strategize": aKeyColors(1, 2) = 10053120
aKeyColors(2, 1) = "Coordinate": aKeyColors(2, 2) = 10053120
aKeyColors(3, 1) = "Develop": aKeyColors(3, 2) = 10053120
aKeyColors(4, 1) = "Draft": aKeyColors(4, 2) = 10053120
aKeyColors(5, 1) = "Organize": aKeyColors(5, 2) = 10053120
aKeyColors(6, 1) = "Finalize": aKeyColors(6, 2) = 10053120
aKeyColors(7, 1) = "Maintain": aKeyColors(7, 2) = 10053120
aKeyColors(8, 1) = "Prepare": aKeyColors(8, 2) = 10053120
aKeyColors(9, 1) = "Rework": aKeyColors(9, 2) = 10053120
aKeyColors(10, 1) = "Revise": aKeyColors(10, 2) = 10053120
aKeyColors(11, 1) = "Review": aKeyColors(11, 2) = 10053120
aKeyColors(11, 1) = "Analysis": aKeyColors(11, 2) = 10053120
aKeyColors(11, 1) = "Analyze": aKeyColors(11, 2) = 10053120
aKeyColors(12, 1) = "Follow Up": aKeyColors(12, 2) = 10053120
aKeyColors(12, 1) = "Follow-Up": aKeyColors(12, 2) = 10053120
aKeyColors(13, 1) = "Maintain": aKeyColors(13, 2) = 10053120
aKeyColors(14, 1) = "Address": aKeyColors(14, 2) = 10053120
aKeyColors(15, 1) = "Attend": aKeyColors(15, 2) = 10092441
aKeyColors(16, 1) = "Confer": aKeyColors(16, 2) = 10092441
aKeyColors(17, 1) = "Meet": aKeyColors(17, 2) = 16751103
aKeyColors(18, 1) = "Work With": aKeyColors(18, 2) = 16751103
aKeyColors(19, 1) = "Correspond": aKeyColors(19, 2) = 16750950
aKeyColors(20, 1) = "Email": aKeyColors(20, 2) = 16750950
aKeyColors(20, 1) = "E-mail": aKeyColors(20, 2) = 16750950
aKeyColors(21, 1) = "Phone": aKeyColors(21, 2) = 6697881
aKeyColors(22, 1) = "Telephone": aKeyColors(22, 2) = 6697881
aKeyColors(23, 1) = "Call": aKeyColors(23, 2) = 6697881
aKeyColors(24, 1) = "Committee": aKeyColors(24, 2) = 3394611
aKeyColors(25, 1) = "Various": aKeyColors(25, 2) = 32768
aKeyColors(26, 1) = "Team": aKeyColors(26, 2) = 13056
aKeyColors(27, 1) = "Print": aKeyColors(27, 2) = 10092543
aKeyColors(28, 1) = "Wip": aKeyColors(28, 2) = 65535
aKeyColors(29, 1) = "Circulate": aKeyColors(29, 2) = 39372
wsFees.Cells.FormatConditions.Delete
ReDim aOutput(1 To UBound(aKeyColors, 1), 1 To 2)
With wsFees.Columns("G")
For i = LBound(aKeyColors, 1) To UBound(aKeyColors, 1)
If WorksheetFunction.CountIf(.Cells, "*" & aKeyColors(i, 1) & "*") > 0 Then
j = j + 1
aOutput(j, 1) = aKeyColors(i, 1)
aOutput(j, 2) = aKeyColors(i, 2)
.FormatConditions.Add xlExpression, Formula1:="=AND(D1>0.6,ISNUMBER(SEARCH(""" & aKeyColors(i, 1) & """,G1)))"
.FormatConditions(.FormatConditions.Count).Interior.Color = aKeyColors(i, 2)
End If
Next i
End With
If j > 0 Then
wsKey.Range("A2").Resize(j, 1).Value = aOutput
For i = 1 To j
wsKey.Cells(i + 1, "B").Interior.Color = aOutput(i, 2)
Next i
wsKey.Columns("A").EntireColumn.AutoFit
End If
With wsFees.Columns("G")
.FormatConditions.AddUniqueValues
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlDuplicate
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End Sub
A formula-based CF using COUNTIFS() would likely do the job.
E.g.