Why doesn't this code change Font from Bold to non-Bold

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
232
Office Version
  1. 365
Platform
  1. Windows
Here is the code
VBA Code:
Function Cell_Font(Sheet_Spec, ByVal Rng_Arg, _
                     Optional Bold_Test As Boolean = False, _
                     Optional Italic_Test As Boolean = False, _
                     Optional Font_Size As Integer = 0, _
                     Optional Font_Name = "", _
                     Optional Underline_Style As XlUnderlineStyle = xlUnderlineStyleNone)
    ' 10/6/15 Put in All_Rng_Arg call. WML
    ' 9/2/19 Added ""Name" to argument list. WML
    ' 6/1/20 Changed name from "Cell_Font" and changed args around. WML
    ' 8/31/20 WML Reworked. WML
    
    ' Note: If the Font Name is illegal, Excel will revert to using the current font.
      
    Dim SHEET As Worksheet
    
    Prog = "Cell_Font"
    
    Call Wait(0, 0, 0.5)
    Call Sheet_Arg(Sheet_Spec, SHEET, Sheet_Name)
    
    If InStr(Rng_Arg, ":") = 0 Then Rng_Arg = Rng_Arg & ":" & Rng_Arg
    
    With SHEET
        Range(Rng_Arg).Select
            
        Selection.Font.Bold = Bold_Test   '  <-- Bold_Test is set to False and the cell to be operated on is showing in Bold.
        Selection.Font.Italic = Italic_Test
        If Len(Font_Name) Then Selection.Font.Name = Font_Name
        If FontSize > 0 Then Selection.Font_Size = Font_Size
        
        Selection.Font.Underline = Underline_Style
        
    End With
    
End Function ' Cell_Font()

When it gets called inside a Program, it doesn't change the indicated cells from Bold to non_Bold.
If I stop the code on the call and then execute it, it does.
Can anyone tell me what's gong on?

Thanks, Mac
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your With block is currently not doing anything. Also, there is no need to select the range - you can simply manipulate it directly:

VBA Code:
With SHEET.Range(Rng_Arg)

.Font.Bold = Bold_Test ' <-- Bold_Test is set to False and the cell to be operated on is showing in Bold.
.Font.Italic = Italic_Test
If Len(Font_Name) Then .Font.Name = Font_Name
If FontSize > 0 Then .Font_Size = Font_Size

.Font.Underline = Underline_Style

End With
 
Upvote 0
Forgot to add the call used as
VBA Code:
Call Cell_Format(A_WorkSheet, 3,False)

It's a big system, and this proceedure gets called from many places and is used in other programs.
 
Upvote 0
If the block is the problem, why does it work when I execute the individual call line?
 
Upvote 0
Re the Block: You are fight. Apparently it only works on the ActiveSceeen like Freeze does.

Here is the updated code:
Code:
Function Cell_Font(SHEET as Worksheet, ByVal Rng_Arg, _
                     Optional Bold_Test As Boolean = False, _
                     Optional Italic_Test As Boolean = False, _
                     Optional Font_Size As Integer = 0, _
                     Optional Font_Name = "", _
                     Optional Underline_Style As XlUnderlineStyle = xlUnderlineStyleNone)
    ' 10/6/15 Put in All_Rng_Arg call. WML
    ' 9/2/19 Added ""Name" to argument list. WML
    ' 6/1/20 Changed name from "Cell_Font" and changed args around. WML
    ' 8/31/20 WML Reworked. WML
    
    ' Note: If the Font Name is illegal, Excel will revert to using the current font.
      
    Dim SHEET As Worksheet
    
    Prog = "Cell_Font"
        
    If InStr(Rng_Arg, ":") = 0 Then Rng_Arg = Rng_Arg & ":" & Rng_Arg
    
    ORIG_SHEET = ActiveSheet.Name
    SHEET.Activate
    
    Range(Rng_Arg).Select
        
    Selection.Font.Bold = Bold_Test
    Selection.Font.Italic = Italic_Test
    If Len(Font_Name) Then Selection.Font.Name = Font_Name
    If FontSize > 0 Then Selection.Font_Size = Font_Size
    
    Selection.Font.Underline = Underline_Style       
    
    Sheets(ORIG_SHEET).Activate
    
End Function ' Cell_Font()
 
Upvote 0
Or just use the code I posted, which is more efficient. ;)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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