abenitez77
Registered User.
- Local time
- Today, 13:07
- Joined
- Apr 29, 2010
- Messages
- 141
I have this code to copy conditional formatting from 1 textbox to all the textboxes (more than 75 textboxes) in a form. It works when I have the form open and i run it, but when i close it and then reopen the form it is not there again. I open it in design mode to check the other textboxes, it is not saved. Am I missing something?
FYI, I tried copying the info using "format painter" , and it does copy the conditional formatting, but it does not update the textbox name, it is still referencing the previous textbox that I copied it from, so I can't use that.
Format Painter results:
Copying from:
Right([p24_dmg],1)='G'
Copying To:
"p23_dmg"
and it keeps "p24_dmg"
FYI, I tried copying the info using "format painter" , and it does copy the conditional formatting, but it does not update the textbox name, it is still referencing the previous textbox that I copied it from, so I can't use that.
Format Painter results:
Copying from:
Right([p24_dmg],1)='G'
Copying To:
"p23_dmg"
and it keeps "p24_dmg"
Code:
Function AddFormats(ctlSource As Control, frm As Form) As Integer
Dim ctl As Control
Dim fcdSource As FormatCondition
Dim fcdDestination As FormatCondition
Dim varOperator As Variant
Dim varType As Variant
Dim varExpression1 As Variant
Dim varExpression2 As Variant
Dim intConditionCount As Integer
Dim intCount As Integer
intConditionCount = ctlSource.FormatConditions.Count
For Each ctl In frm.Controls
If ctl.Name = ctlSource.Name Then
' This is the source. Don't apply formatting.
ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
Select Case ctl.Name
Case "legal_entity_id", "curr", "asin", "pubcdap", "pubcdpo", "marketplaceid", "gl_grp", "descr", "upc", "upc6", "cat", "sub_cat", "pubcode", "studio", "brand"
' Skip these, don't need formatting them.
Case Else
intCount = 0
' Bulk remove all current FormatConditions
ctl.FormatConditions.Delete
Do Until intCount = intConditionCount
Set fcdSource = ctlSource.FormatConditions.Item(intCount)
varOperator = fcdSource.Operator
varType = fcdSource.Type
varExpression1 = fcdSource.Expression1
varExpression2 = fcdSource.Expression2
' Add the FormatCondition
ctl.FormatConditions.Add varType, varOperator, varExpression1, varExpression2
'ctl.FormatConditions.Add acExpression, , varExpression1
' Reference the FormatCondition to apply formatting.
' Note: The FormatCondition cannot be referenced
' in this manner until it exists.
Set fcdDestination = ctl.FormatConditions.Item(intCount)
With fcdDestination
.BackColor = fcdSource.BackColor
.FontBold = fcdSource.FontBold
.FontItalic = fcdSource.FontItalic
.FontUnderline = fcdSource.FontUnderline
.ForeColor = fcdSource.ForeColor
End With
' Move to the next FormatCondition
intCount = intCount + 1
Loop
'ctl.FormatConditions.Item(intCount).Modify
End Select
End If
Next ctl
' Cleanup
AddFormats = intConditionCount
MsgBox "There were " & AddFormats & " Conditional Format(s) applied to all text and combo boxes except the source."
Set ctl = Nothing
Set fcdSource = Nothing
Set fcdDestination = Nothing
Set varOperator = Nothing
Set varType = Nothing
Set varExpression1 = Nothing
Set varExpression2 = Nothing
intConditionCount = 0
intCount = 0
End Function
Last edited: