Excel VBA: function to turn activecell to bold

前端 未结 2 1633
挽巷
挽巷 2021-01-08 01:15

I have the following function inside my module.

Function Colorize(myValue)
    ActiveCell.Select
    Selection.Font.Bold = True
    Colorize = myValue
End Fu         


        
相关标签:
2条回答
  • 2021-01-08 01:38

    A UDF will only return a value it won't allow you to change the properties of a cell/sheet/workbook. Move your code to a Worksheet_Change event or similar to change properties.

    Eg

    Private Sub worksheet_change(ByVal target As Range)
      target.Font.Bold = True
    End Sub
    
    0 讨论(0)
  • 2021-01-08 01:42

    I use

                chartRange = xlWorkSheet.Rows[1];
                chartRange.Font.Bold = true;
    

    to turn the first-row-cells-font into bold. And it works, and I am using also Excel 2007.

    You can call in VBA directly

                ActiveCell.Font.Bold = True
    

    With this code I create a timestamp in the active cell, with bold font and yellow background

               Private Sub Worksheet_SelectionChange(ByVal Target As Range)
                   ActiveCell.Value = Now()
                   ActiveCell.Font.Bold = True
                   ActiveCell.Interior.ColorIndex = 6
               End Sub
    
    0 讨论(0)
提交回复
热议问题