VBA 对 range() 或 cells() 的内容格式的修改, 如 range.address(0,0) cells().formulaR1C1

1 range() cells() 内容格式的调整

显示属性

   debug.print range().address

设置属性

with range() /cells()

  • address 只读
  •  
  • font
  1. .bold =true      '加粗
  2. .italic =true     '斜体
  3. .Name = "华文新魏"
  4. .Size = 15
  5. .ColorIndex = 3
  6. .color=vbcolor   ‘?
  7. .underline=true   
  • interior
  1. color
  2. colorindex
  3.  

end with

 

试验代码

Sub test91()

'使用range ,selection, activecell  cells 应该都一样

Range("a1:b3").Select
With Selection
    .Font.Bold = True
    .Font.Italic = True
    
    .Font.Size = 30
    .Font.Color = vbcolor
    .Font.Underline = True

     With .Interior
        .ColorIndex = 6
        .Color = 100   '不方便,interior.color的值是一个十进制数,范围是0-16777215. 属于long长整形范围
     End With
End With


With Cells(7, 3)
'     .Address = "$c$8" 不能修改
     .Value = 11 + 22
     .Formula = 1 + 2
     .FormulaR1C1 = 0 + 1
End With

Debug.Print
Debug.Print Cells(7, 3).Value
Debug.Print Cells(7, 3).Address  '不能修改
Debug.Print Cells(7, 3).Formula
Debug.Print Cells(7, 3).FormulaR1C1

Dim r1 As Object
Set r1 = Cells(7, 3).Resize(2, 2)
r1.Interior.ColorIndex = 5
Debug.Print r1.Address

Dim r2 As Object
Set r2 = Cells(7, 3).Offset(5, 2)
r2.Interior.ColorIndex = 6
Debug.Print r2.Address

End Sub

 

 

 

2  .address属性

  •  比如默认的  cells().address= "$c$8" 可以用 split() 转为一个array处理
  • address属性带参数,可以返回 绝对地址,和相对地址
  • 默认返回绝对地址,即锁定地址
  • address(0,0) 返回相对地址
Sub tt3()

Debug.Print Cells(7, 3).Address
Debug.Print Cells(7, 3).Address(1, 1)
Debug.Print Cells(7, 3).Address(0, 0)
Debug.Print Cells(7, 3).Address(1, 0)
Debug.Print Cells(7, 3).Address(0, 1)


End Sub

2  .formula 和  .formulaR1C1

  • 如:选定单元格为C8 
  • R[-1]C[-1]为B7单元格,行列都-1,R[1]C[2]为E9单元格,行+1,列+2 
  • R1C1代表A1单元格,R5C6代表F5单元格 
  • 例:C1单元格为"=A1+B1" 
  • Range("C1").FormulaR1C1 = "=RC[-2]+RC[-1]"      '也就是R[1]C[-2]  可以写成RC[-2] 
  • 例:C1单元格为"=A2+E3" 
  • Range("C1").FormulaR1C1 = "=R[1]C[-2]+R[2]C[2]"
Sub tt5()

Debug.Print Cells(5, 6).Value
Debug.Print Cells(5, 6).Formula
Debug.Print Cells(5, 6).FormulaR1C1
Debug.Print

Debug.Print Cells(8, 6).Value
Debug.Print Cells(8, 6).Formula
Debug.Print Cells(8, 6).FormulaR1C1


End Sub

发布了416 篇原创文章 · 获赞 46 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xuemanqianshan/article/details/104162073