VBA(11)单元格的操作(一)附汇总表转明细例子.

1.复习一下单元格基本的操作:

Sub s()         '表示一个单元格
    Range("a1").Select
    Cells(1, 1).Select
    Range("a" & 1).Select
    Cells(1, "A").Select
    Cells(1).Select
    [a1].Select
End Sub
Sub s1()         '表示相邻的单元格
    Range("a1:c5").Select
    Range("a1", "c5").Select
    Range(Cells(1, 1), Cells(5, 3)).Select
    Range("a1:a10").Offset(0, 1).Select
    Range("a1").Resize(5, 3).Select
End Sub
Sub s2()         '表示不相邻的单元格
    Range("a1,c1:c4,a7").Select
    Union(Range("a1"), Range("c1:c4").Range("a7")).Select
End Sub
Sub s3()    'union用法示例
    Dim rg As Range, x As Integer
    For x = 2 To 10 Step 2
        If x = 2 Then Set rg = Cells(x, 1)
        Set rg = Union(rg, Cells(x, 1))
    Next x
    rg.Select
End Sub

S3例子备注< If x = 2 Then Set rg = Cells(x, 1)>'if语句后跟THEN写在一块在本机上OFFICE13版相当于_
     If x = 2 Then
     Set rg = Cells(x, 1)
   End If
_操作,原因不明


Sub s4()         '表示行
    Rows(1).Select
    Rows("1:2").Select
    Range("1:2,4:5").Select
    Range("c4:f5").EntireRow.Select
End Sub
Sub s5()         '表示列
    Columns(1).Select
    Columns("A:B").Select
    Range("A:B,D:E").Select
    Range("c4:f5").EntireColumn.Select
End Sub
Sub s6()         '重置单元格内的单元格
    Range("b2").Range("a1") = "100"     '相当于在限定里面重新计算;实际上还是B2
End Sub
Sub s7()
    Selection.Value = 100           'selection表示正在选取的单元格
End Sub
Sub s8()        
    Sheets("sheet2").UsedRange.Select         '表2已使用单元格
    'sheets(2).range("a1:a10").copy range("a1")   '复制未知大小的区域可指定到某一单元格右上
End Sub
Sub s9()
    Range("b8").CurrentRegion.Select        '单元格所在的已使用单元格区域
End Sub
Sub s10()                 
    Application.Intersect(Columns("b:c"), Rows("3:5")).Select                                                    '共同区域(返回两个单元格参数内相同的部分)
End Sub


Sub s11()                         '调用定位条件选取单元格
    Range("a1:a6").SpecialCells(xlCellTypeBlanks).Select    '[A1:A6]定位条件(F5)为空值选中
End Sub
Sub s12()                         '端点单元格
    Range("a65536").End(xlUp).Offset(1, 0) = 1000       'A列最后一行再往下一行的单元格输1000
    Range(Range("b6"), Range("b6").End(xlToRight)).Select   '区域[B6:B6最右]选中
End Sub

2.实例简单的汇总表转明细表
模拟数据如下:

模拟背景:此表为总仓管理进销存明细总表:

使用场景:当总表需要拆分单独发送的时候某一客户仓的单独明细时候,如下:

Sub zhuan1()            '汇总表转明细表
'调用内存时运行速度较快
    Dim x As Integer, y As Integer, z As Integer
    Dim a As Integer, b As Integer
    Dim arr(1 To 2999, 1 To 17)             '创建数组放置提取的数据
    a = 1                       '记录行
    z = Sheets(1).Range("a65536").End(xlUp).Row     '定位至最后一行
    For x = 3 To z                  '从第3行开始
        For y = 11 To 15                '提取的行区域
            If Cells(x, y) <> "" Then               '如果不为空将数据将入数组
                arr(a, 1) = Cells(x, 1)     '日期
                arr(a, 2) = Cells(2, y)     '客户仓
                arr(a, 3) = Cells(x, 7)     '负责人
                arr(a, 4) = Cells(x, 8)     '代发仓编号
                arr(a, 5) = Cells(x, 9)     '代发仓
                arr(a, 6) = Cells(x, 2)     '大类
                arr(a, 7) = Cells(x, 3)     'SKU
                arr(a, 8) = Cells(x, 4)     '品名
                arr(a, 9) = Cells(x, 5)     '单位
                arr(a, 10) = Cells(x, 6)    '总订单数量
                arr(a, 11) = Cells(x, 10)   '总实发数量
                arr(a, 12) = Cells(x, y)        '客户实发数量
                arr(a, 17) = Cells(x, 16)       '备注
                a = a + 1
            End If
        Next
    Next
    Dim sh As Worksheet
    Set sh = Worksheets.Add     '创建新的表
        sh.Name = "明细表"      '更改表名
        sh.Range("A1:M1") = Array("日期", "客户仓", "负责人", "代发仓编号", "代发仓", "大类", "SKU", "品名", "单位", "总订单数量", "总实发数量", "发给客户实发数量", "备注") '表头
        sh.Range("a2").Resize(UBound(arr, 1), UBound(arr, 2)) = arr '将数组装入
    Set sh = Nothing
        With Range("a1:M" & a + 1) '设置格式
        .EntireColumn.AutoFit           '自动调整列宽
        .Borders.LineStyle = xlContinuous       '单元格边框样式
        .Borders.ColorIndex = xlAutomatic       '边框颜色
        .Borders.TintAndShade = 0       '颜色深浅程度,你可以输入一个从 -1 (最深) 到 1 (最亮) 个数。 零 (0) 中性值
        .Borders.Weight = xlHairline        '边框的粗细
    End With
End Sub

效果:

简单的应用思路截至到拆分数据完成。如需另存为单独表参考VBA(9)拆分合并表.

谢谢阅读!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值