With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 观察代码,我们发现,宏录制器为设置字体和单元格背景的两段代码都使用了With … End With
方法/步骤 1 编写绘制边框的程序:'绘制置单元格边框Public Sub unitBorder() With Selection.Borders(xlEdgeLeft) '设置左边框 .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) '设置上边框 .LineStyle = xlCo...
Selection.Font.Shadow = False Selection.Font.Underline = xlUnderlineStyleNone Selection.Font.ColorIndex = xlAutomatic Selection.Font.TintAndShade = 0 Selection.Font.ThemeFont = xlThemeFontNone End With 说明:尽管要求是仅修改字体大小,但是在Excel里实际上是打开了字体设置,字体设置的其他属性是在录制宏里是...
With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 观察代码,我们发现,宏录制器为设置字体和单元格背景的两段代码都使用了With … En...
1)案例理解:以下VBA代码仅为了设置选中单元格D2的字号 Sub 宏1() ' '宏1 宏 ' ' Range("D2").Select With Selection.Font .Name = "Arial" .Size = 20 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False ...
With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 观察代码,我们发现,宏录制器为设置字体和单元格背景的两段代码都使用了With … End With结构,这就是VBA为我们提供的处理对象的有效方法之一...
要选择区域中第几行第同列的单元格也和工作表一样,如表示选定区域的第2行第2列,可以用Selection.Cells(2,2),也可以用Selection.Range("B2")。示例代码:Sub gvntw() With Selection.Range("B2") .Value = "我是选区的B2" .Font.Color = vbRed .Interior.Color = vbYellow...
Excel VBA(Visual Basic for Applications)是一种强大的编程语言,可用于自动化Excel的各种操作。其中,Selection对象是VBA中一个常用的对象,用于表示当前选定的单元格、区域或对象。掌握Selection对象的用法能够提高数据处理和操作的效率。本文将介绍Excel VBA中Selection对象的常见用法。1.基本概念 在Excel中,我们经常...
We look at the examples of selection in Excel VBA. Example #1 Here is a simple example of a “selection” property with VBA. Of course, we want to first select the cells from A1 to B5 towrite the VBA codelike this. Range (“A1:B5”).Select ...
1.使用Selection对象引用当前选择的单元格或范围:```vba Dim selectedRange As Range Set selectedRange = Selection ```2.使用Selection对象读取或修改选择的单元格的值:```vba '读取选择的单元格的值 Dim selectedValue As Variant selectedValue = Selection.Value '修改选择的单元格的值 Selection.Value = "...