vba 学习(一)

在线学习

http://excel.walk-nie.com/vba-sample/vba-sample-ctrl/803

 

基础学习看附件 vba学习文档.rar

 

一、用VBA为选定的单元格加上边框

Cells(1, 3).Select   //当前选中的第一行第三列的单元格为例。
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)//为左边上边框。
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)//为上边上边框。
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)//为下边上边框。
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)//为右边边上边框。
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

 

加边框之前最好用宏录制一下整个操作过程就了解流程了。

 

1.     虽然用录制代码的方法可以很方便的获得设置单元格边框的代码,但可以看出,得出的代码非常复杂,实际上就是对每一步操作进行录制得来的。

2.     利用Range 对象的Borders 属性及BorderAround 方法分别可以设置单元格区域的内部线框及外部边框。

3.     Borders 属性的属性值及BorderAround 方法的参数值可以复制刚才录制的代码而得。

下面是利用Range和borders的示例演示

Dim rng As Range

  Set rng = Range("A1:B6")

  With rng.Borders

    .LineStyle = xlContinuous//边框设置

    .ColorIndex = xlAutomatic//颜色设置

    .TintAndShade = 0

    .Weight = xlHairline//是否粗细

  End With

  rng.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic

 

2:vba设置Excel单元格左对齐、右对齐、居中对齐、字体等

 

左对齐、右对齐、居中对齐

  '选择区域或单元格右对齐  
  Selection.HorizontalAlignment = Excel.xlRight

  '选择区域或单元格左对齐
  Selection.HorizontalAlignment = Excel.xlLeft

  '选择区域或单元格居中对齐  
  Selection.HorizontalAlignment = Excel.xlCenter

  固定区域的对齐方式的代码:

  Range("A1:D9"). HorizontalAlignment = Excel.xlLeft

字体、字号、字型

  '当前单元格字体为粗体
  Selection.Font.Bold = True

  '当前单元格字体为斜体
  Selection.Font.Italic = True

  '当前单元格字体为宋体20号字

  With Selection.Font
   .Name = "雅黑体"
   .Size = 20
  End With

 

 

VBA控制Excel的边框线,打印区域需要用到的函数有Borders.LineStyle

Sheet.Range(from,to).Borders[].LineStyle=1

Sheet.Range(cells(x1,y1),cells(x2,y2).Borders.LineStyle

Borders[]里面也可以有参数 ,如edgetop(顶部),edgebottom(底部)之类的

LineStyle的属性 有xlLineStyleNone(clear Line),1(单行线),5(虚线),xlDouble(双线),xlDouble取value值也是可以的

打印区域设置: ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(ShtRow_End, SubjCnt + 2 )).Address

 

 

二、vba字符串函数

Trim(string)            去掉string左右两端空白

Ltrim(string)           去掉string左端空白


Rtrim(string)           去掉string右端空白


Len(string)             计算string长度


Left(string, x)         取string左段x个字符组成的字符串


Right(string, x)        取string右段x个字符组成的字符串


Mid(string, start,x)    取string从start位开始的x个字符组成的字符串


Ucase(string)           转换为大写


Lcase(string)           转换为小写


Space(x)                返回x个空白的字符串


Asc(string)             返回一个 integer,代表字符串中首字母的字符代码


Chr(charcode)           返回 string,其中包含有与指定的字符代码相关的字符

 

InStr()    例如:

Dim a As String
a = "中华人民共和国"
If InStr(a, "国") > 0 Then
   MsgBox ("a有")
Else
    MsgBox ("a无")
End If

   

VBA中如何表示一个字符串包含另一个字符串

    instr 函数 instr(a,b),返回值为0或者其他数值。0代表a 中,不包含b .数值代表 b 在a中出现的位置。如:instr("12","2")=2

 

    InStr(S1,S2)>0说明S1中包含S2
    InStr(S1,S2)=0说明S1中不包含S2

 

 

 

三、VBA 取到范围内所有值,并去重复

 

Sub test()
    Dim ar
    Dim d As Object
    Dim i As Integer
    Set d = CreateObject("scripting.dictionary")        '创建 字典 对象
    ar = Range("a2:a" & Range("a65536").End(xlUp).Row)  '将 数据写入 数组 Ar
    For i = 1 To UBound(ar)     '循环 数组
        If ar(i, 1) <> "" Then  '如果 不是 空
            d(ar(i, 1)) = ""    '添加到字典中,"字典自身具有相同的内容不重复添加功能"
        End If
    Next
    Range("e2:e65536").ClearContents  '清除 E2:E5536 区域中的内容
    Range("e2").Resize(d.Count) = Application.Transpose(d.keys)
    '以 E2 为基点,扩展 D.count 行的数据 为 字典 转置 后的内容
End Sub

 

LBound(数组) 可以得到数组的最小“学号”(下标)

UBound(数组)可以得到数组中的最大“学号”(上标)

Sub aa()
   Dim arr
   arr = Range("a1:a10")
   MsgBox LBound(arr)
   MsgBox UBound(arr)
End Sub

 

 

上面是针对一维数组,如果是二维数组呢?

LBound(数组,2) 可以得到数组中第二维最小“学号”(下标)

UBound(数组,2)可以得到数组中第二维的最大“学号”(上标)

Sub aa()
   Dim arr
   arr = Range("a1:d10")
   MsgBox LBound(arr,2)
   MsgBox UBound(arr,2)
End Sub

 

 

四、VBA String 转Integer ⇒ val()

 

VAL是返回包含于字符串内的数字,字符串中是一个适当类型的数值。
在它不能识别为数字的第一个字符上,停止读入字符串。那些被认为是数值的一部分的符号和字符,例如美圆号与逗号,都不能被识别。但是函数可以识别进位制符号 &O(八进制)和 &H(十六进制)。空白、制表符和换行符都从参数中被去掉。
Val("2457")    返回 2457
Val(" 2 45 7")    返回 2457
Val("24 and 57")    返回 24
 
InStr 函数来查找某字符串在另一个字符串中首次出现的位置。

 

 

 

五、VBA 获取远程 excel 的 sheet

 

public sub remoteFile()
dim a as string, b as worksheet
       a ="\\100.100.100.100\e:\file\data.xls" 
    Workbooks.Open Filename:= a   '打开工作簿
       Set b = Workbooks("data.xls").Worksheets("lab")       '打开工作簿下的工作表
end sub

 

 

 

 

六、VBA 单精度浮点数 (Single), 双精度浮点数 (Double), 财务上使用的:Currency 类型

通常单精度浮点数 (Single) 在计算机中是用 4个字节来表示的,范围为:负数 -3.402823 * 10^38 ~ -1.401298 * 10^(-45),正数 1.401298 * 10 (-45) ~ 3.402823 * 10 ^38

而双精度浮点数 (Double) 在计算机中是用 8个字节来表示的,范围为:负数  -1.79769313486232 * 10^308 ~ -4.94065645841274 *10^ (-324),正数4.94065645841274 *10^ (-324) ~ 1.79769313486232 * 10^308

在 VBA 中还有一种数据类型是专为财务上使用的:Currency 类型。它在计算机中也是用 8个字节来存储的,范围为:-922,337,203,685,477.5808 到 922,337,203,685,477.5807。

 

 

 

七、循环 for each   next 循环的用法

 

Sub test7()
'激活工作表
Worksheets("Sheet1").Activate
Dim i As Integer
i = 1
For Each C In Range("A22:I30")
    C.Value = i
    i = i + 1
Next C
End Sub

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值