我是靠谱客的博主 俏皮飞机,最近开发中收集的这篇文章主要介绍常用的一些vba代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

数组赋值给单元格区域

Sub suijishu() '随机数
    Dim shuzu(1 To 50000, 1 To 20) As Variant '定义数组,注意是从1开始的
    
    Dim hang As Long
    Dim lie As Long
    
    Randomize
    For hang = 1 To 50000
        For lie = 1 To 20
            shuzu(hang, lie) = Rnd()
            
        Next lie
    Next hang
    
    '数组赋值给单元格区域
    Sheet1.Range(Cells(1, 1), Cells(50000, 20)) = shuzu
    
End Sub

下面的代码是没有优化的代码,功能与上面一样(执行时间非常长)

Sub suiji2()
    Dim hang As Long
    Dim lie As Long
    Randomize
    For hang = 1 To 50000
        For lie = 1 To 20
            Sheet2.Cells(hang, lie) = Rnd()
            
        Next lie
    Next hang
    
End Sub

即便是前后增加关闭显示,效率提升也很有限。

运行时间

详细代码如下


Sub suiji3()
    Dim time1 As Single
    Dim time2 As Single
    
    time1 = Timer
    
    
    Dim hang As Long
    Dim lie As Long
    Randomize
    For hang = 1 To 50000
        For lie = 1 To 20
            Sheet2.Cells(hang, lie) = Rnd()
            
        Next lie
    Next hang
    
    
    time2 = Timer
    Dim timecha As Single
    timecha = (time2 - time1)
    
    MsgBox "运行时间" & timecha & "秒"
    
    
End Sub

另外的一段代码

对内容进行处理之后再赋值到相应的表格

参考代码如下

’参考代码https://blog.csdn.net/iamlaosong/article/details/46878321
Sub fuzhi() '大数据复制
    Dim shuzu() ' As Variant
    shuzu = Sheets(1).Range(Sheets(1).Cells(1, 1), Sheets(1).Cells(50000, 20)).Value
    'shuzu = Sheet1.Range(Sheets(1).Cells(1, 1), Sheets(1).Cells(50000, 20)).Value  '注意此句无法通过
    
    Dim shuzu2(1 To 50000, 1 To 20) As Variant '定义数组,注意是从1开始的
    Dim hang As Long
    Dim lie As Long
    '此循环内可以添加相应的计算
    For hang = 1 To 50000
        For lie = 1 To 20
            shuzu2(hang, lie) = shuzu(hang, lie)
        Next lie
    Next hang
    
    '数组赋值给单元格区域
    Sheets(3).Range(Cells(1, 1), Cells(50000, 20)) = shuzu2
    
End Sub

大数据分拣


Sub fuzhi() '大数据复制

    
    Const DEF_CGQSL As Long = 14    '定义 传感器数量
    Const DEF_TiaoM As Long = 35000  '定义 处理条目
    
    
    Dim DEF_BiaoGZHS As Long
    DEF_BiaoGZHS = 1 + DEF_CGQSL * DEF_TiaoM
    
    Dim shuzu()  As Variant
    shuzu = Sheets(1).Range(Sheets(1).Cells(2, 1), Sheets(1).Cells(DEF_BiaoGZHS, 26)).Value
    
    Dim i As Long
    Dim hang As Long
    Dim book2hang As Long
    book2hang = 1
    
    Dim Low1 As Long
    Dim Up1 As Long
    Low1 = LBound(shuzu, 1) '1
    Up1 = UBound(shuzu, 1) 'hang
    Dim Low2 As Long
    Dim Up2 As Long
    Low2 = LBound(shuzu, 2) '1
    Up2 = UBound(shuzu, 2) '26
    

    Dim shuzu2(1 To DEF_TiaoM + 1, 1 To 26 * DEF_CGQSL) As Variant '定义数组,注意是从1开始的
'
        Dim cgq As Long
        For cgq = 1 To DEF_CGQSL
            book2hang = 1
            For hang = cgq To DEF_BiaoGZHS Step DEF_CGQSL
                For i = 1 To 26
                    'If (hang > 149950) Then Debug.Print hang
                    If hang <= DEF_BiaoGZHS And book2hang < DEF_TiaoM Then
                        shuzu2(book2hang, i + (cgq - 1) * 26) = shuzu(hang, i)
                    End If
                Next i
                book2hang = book2hang + 1
           Next hang
        Next cgq
'
    book2hang = 1
    '数组赋值给单元格区域
    Sheets(2).Range(Cells(1, 1), Cells(DEF_TiaoM + 1, 26 * DEF_CGQSL)) = shuzu2

End Sub

 删除空行

sub  删除空行()
     Sheets(1).Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
end sub

anlog  2021年3月24日00点46分

特此记录

最后

以上就是俏皮飞机为你收集整理的常用的一些vba代码的全部内容,希望文章能够帮你解决常用的一些vba代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部