admin 管理员组

文章数量: 887021

VBA

Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
'封装延时函数
Sub delay(T As Long)
    Dim time1 As Long
    
    time1 = timeGetTime
    Do
        DoEvents
    Loop While timeGetTime - time1 < T
End Sub

Sub 宏1()
    Dim i As Long, row_book As Long, column_book As Long
    
    '遍历所有工作表(sheet)
    For i = 1 To ActiveWorkbook.Worksheets.Count
        '下面这句,亦可以用ActiveWorkbook.Worksheets(i).Activate 进行替代,代表激活此工作表
        ActiveWorkbook.Worksheets(i).Select
        '获取此激活的工作表的行和列
        row_book = ActiveSheet.UsedRange.Rows.Count
        column_book = ActiveSheet.UsedRange.Columns.Count
        
        '选中所有数据,复制并粘贴为数值
        Range("A1:" & Cells(row_book, column_book).Address).Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        
        '延迟2s 这里存粹是为了测试,看他有没有异常,实际运行中,上面的时间封装,和这个delay就可以不用了~
        delay (2000)
        Debug.Print (ActiveWorkbook.Worksheets(i).Name)
    Next
    
End Sub

 

截图如下:

本文标签: VBA