I want to add a macro to a button so when a cell is selected within range E10:F20 and I click the macro button, the cell content becomes bold.
But if content already is bold, make content not bold
How do I achieve this?
I want to add a macro to a button so when a cell is selected within range E10:F20 and I click the macro button, the cell content becomes bold.
But if content already is bold, make content not bold
How do I achieve this?
Last edited by jokris; 06-08-2016 at 12:56 PM.
Hi there,
Try entering the following code in the VBA CodeModule of the worksheet for which you wish to provide this feature, and see if it does what you need:
The highlighted value may be altered to suit your own requirements.![]()
Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) Const sTARGET_RANGE As String = "E10:F20" Dim iNoOfCells As Integer On Error Resume Next iNoOfCells = Target.Cells.Count On Error GoTo 0 If iNoOfCells = 1 Then If Not Intersect(Target, _ Me.Range(sTARGET_RANGE)) Is Nothing Then Target.Font.Bold = Not Target.Font.Bold End If End If End Sub
Hope this helps - please let me know how you get on.
Regards,
Greg M
In the sheet module:
![]()
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rInt As Range Set rInt = Intersect(Target, Range("E10:F20")) If Not rInt Is Nothing Then rInt.Font.Bold = Not rInt(1).Font.Bold End Sub
Entia non sunt multiplicanda sine necessitate
It works good, thank you.
But how do I do if I want a macro button that (when clicked) switches selected cell between bold and blue vs not bold not blue within same range E10:F20?
Hi again,
See if the following code does what you need:
Select one or more cells and then run the macro to toggle the appearance of the cells.![]()
Public Sub ToggleCell() Const sTARGET_RANGE As String = "E10:F20" Const lDARK_BLUE As Long = &HFF0000 Const lBLACK As Long = 0 Dim rCell As Range For Each rCell In Selection.Cells With rCell If Not Intersect(rCell, _ ActiveSheet.Range(sTARGET_RANGE)) Is Nothing Then If .Font.Bold = True Then .Font.Bold = False .Font.Color = lBLACK Else: .Font.Bold = True .Font.Color = lDARK_BLUE End If End If End With Next rCell End Sub
Hope this helps - please let me know how you get on.
Regards,
Greg M
Greg, it's perfect!
Thank you!
Hi again,
Many thanks for your feedback and also for the Reputation increase - much appreciated
You're welcome - glad I was able to help.
Best regards,
Greg M
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks