VBA: Make selection bold when selected

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
I would like it when a cell is selected it will make the text Bold and when you click it again it makes it back to normal ??
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Here is one way to do it. Put the following codes where indicated...


General Module (Insert/Module on VB editor's menu bar, place at top)
Code:
Public LastBoldedCell As Range


Worksheet Code Module (double click sheet name in VB editor's Project Window)
Code:
Private Sub Worksheet_Activate()
  If LastBoldedCell Is Nothing Then
    Set LastBoldedCell = ActiveCell
    LastBoldedCell.Font.Bold = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  LastBoldedCell.Font.Bold = False
  Target.Font.Bold = True
  Set LastBoldedCell = Target
End Sub


Workbook Code Module (double click ThisWorkbook item in VB editor's Project Window)
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
  If Sh.Name = "Sheet2" Then LastBoldedCell.Font.Bold = False
  Set LastBoldedCell = Nothing
End Sub
 
Upvote 0
This sort of does what you are looking for...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Font.Bold = False Then
    Target.Font.Bold = True
Else
    Target.Font.Bold = False
End If
End Sub

The problem is that it won't work if you click the cell you already have selected.

Could be a starting point though...
 
Upvote 0
Here is one way to do it. Put the following codes where indicated...


General Module (Insert/Module on VB editor's menu bar, place at top)
Code:
Public LastBoldedCell As Range


Worksheet Code Module (double click sheet name in VB editor's Project Window)
Code:
Private Sub Worksheet_Activate()
  If LastBoldedCell Is Nothing Then
    Set LastBoldedCell = ActiveCell
    LastBoldedCell.Font.Bold = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  LastBoldedCell.Font.Bold = False
  Target.Font.Bold = True
  Set LastBoldedCell = Target
End Sub


Workbook Code Module (double click ThisWorkbook item in VB editor's Project Window)
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
  If Sh.Name = "Sheet2" Then LastBoldedCell.Font.Bold = False
  Set LastBoldedCell = Nothing
End Sub
[/B][/COLOR]

Slight revision... I am not sure what I was thinking, but there is no need to use the Workbook_SheetDeactivate event procedure I showed above as the Worksheet has it own Deactivate event. While the above code works, so will this revision to it (just use what is below and do not include the SheetDeactivate event from the ThisWorkbook code module)...


General Module (Insert/Module on VB editor's menu bar, place at top)
Code:
Public LastBoldedCell As Range


Worksheet Code Module (double click sheet name in VB editor's Project Window)
Code:
Private Sub Worksheet_Activate()
  If LastBoldedCell Is Nothing Then
    Set LastBoldedCell = ActiveCell
    LastBoldedCell.Font.Bold = True
  End If
End Sub

Private Sub Worksheet_Deactivate()
  If Not LastBoldedCell Is Nothing Then LastBoldedCell.Font.Bold = False
  Set LastBoldedCell = Nothing
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  LastBoldedCell.Font.Bold = False
  Target.Font.Bold = True
  Set LastBoldedCell = Target
End Sub
 
Last edited:
Upvote 0
This should also work:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static oldcell As Range
If oldcell Is Nothing Then
    Target.Font.Bold = True
    Set oldcell = Target
Else
    oldcell.Font.Bold = False
    Target.Font.Bold = True
    Set oldcell = Target
End If
End Sub
 
Upvote 0
General Module (Insert/Module on VB editor's menu bar, place at top)
Rich (BB code):
Public LastBoldedCell As Range


Worksheet Code Module (double click sheet name in VB editor's Project Window)
Rich (BB code):
Private Sub Worksheet_Activate()
  If LastBoldedCell Is Nothing Then
    Set LastBoldedCell = ActiveCell
    LastBoldedCell.Font.Bold = True
  End If
End Sub

Private Sub Worksheet_Deactivate()
  If Not LastBoldedCell Is Nothing Then LastBoldedCell.Font.Bold = False
  Set LastBoldedCell = Nothing
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Not LastBoldedCell Is Nothing Then LastBoldedCell.Font.Bold = False
  Target.Font.Bold = True
  Set LastBoldedCell = Target
End Sub
I have to make one change to the above code (I ran across certain circumstances where the above red highlighted test that I added was needed.
 
Upvote 0
This should also work:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static oldcell As Range
If oldcell Is Nothing Then
    Target.Font.Bold = True
    Set oldcell = Target
Else
    oldcell.Font.Bold = False
    Target.Font.Bold = True
    Set oldcell = Target
End If
End Sub
One possible minor problem with it... select a cell on the managed sheet, then select another sheet, then activate the "Go To" dialog box (ALT+EG) and type a reference back to the managed sheet to a cell different than the one that was selected when you switched sheets... both the previously selected and newly selected cells will both be highlighted.
 
Upvote 0
Here is one way to do it. Put the following codes where indicated...


General Module (Insert/Module on VB editor's menu bar, place at top)
Code:
Public LastBoldedCell As Range


Worksheet Code Module (double click sheet name in VB editor's Project Window)
Code:
Private Sub Worksheet_Activate()
  If LastBoldedCell Is Nothing Then
    Set LastBoldedCell = ActiveCell
    LastBoldedCell.Font.Bold = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  LastBoldedCell.Font.Bold = False
  Target.Font.Bold = True
  Set LastBoldedCell = Target
End Sub


Workbook Code Module (double click ThisWorkbook item in VB editor's Project Window)
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
  If Sh.Name = "Sheet2" Then LastBoldedCell.Font.Bold = False
  Set LastBoldedCell = Nothing
End Sub

I tried clicking in a cell that had data in it and it debugged " Object variable or With block variable not set"
 
Upvote 0
I tried clicking in a cell that had data in it and it debugged " Object variable or With block variable not set"
Try using the revised code I posted in Message #7 as I think it addresses that issue (be sure to read Message #5 to make sure you have all the right code installed). For future consideration... you should read all the messages before using the solution from any one of them so that you can see correction that were posted later on.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top