VBA代码大全(更新2023.01.15)

简介: 进行VBA运行,轻松高效办公~

VBA拆分工作簿

Sub 拆分工作薄()
 Dim xpath As String
 xpath = ActiveWorkbook.Path
 Dim sht As Worksheet
 For Each sht In ActiveWorkbook.Sheets
 sht.Copy
 ActiveWorkbook.SaveAs Filename:=xpath & "\" & sht.Name & ".xlsx" '将文件存放在工作薄所在的位置
 ActiveWorkbook.Close
 Next
 MsgBox "拆分完毕!"
End Sub



VBA合并工作簿

Sub 合并当前目录下所有Excel文件()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
.Cells(.Range("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个Excel文件下的全部工作表。如下:" & Chr(13) & WbN,vbInformation, "提示"
End Sub


VBA合并工作表

Sub 合并当前工作簿下的所有工作表()
Application.ScreenUpdating = False
For j = 1 To Sheets.Count
      If Sheets(j).Name  ActiveSheet.Name Then
      X = Range("A65536").End(xlUp).Row + 1
      Sheets(j).UsedRange.Copy Cells(X, 1)
      End If
Next
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "当前Excel的全部工作表已经合并完毕!", vbInformation, "提示"
End Sub



VBA一键批量修改工作表名称

Sub 一键获取工作表名称()
Dim sht As Worksheet, k&
[A:A] = ""
[A1] = "原工作表名称"
j = 1
For Each sht In Worksheets
j = j + 1
Cells(j, 1) = sht.Name
Next
End Sub
Sub 一键修改工作表名称()
Dim shtname$, sht As Worksheet, i&
On Error Resume Next
For i = 1 To Cells(Rows.Count, 1).End(3).Row
shtname = Cells(i, 1)
Set sht = Sheets(shtname)
If Err = 0 Then
Sheets(shtname).Name = Cells(i, 2)
Else
Err.Clear
End If
Next
End Sub



VBA多个Excel合并为1个文件多个工作表

Sub Books2Sheets()
    '定义对话框变量
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    '新建一个工作簿
    Dim newwb As Workbook
    Set newwb = Workbooks.Add
    With fd
        If .Show = -1 Then
            '定义单个文件变量
            Dim vrtSelectedItem As Variant
            '定义循环变量
            Dim i As Integer
            i = 1
            '开始文件检索
            For Each vrtSelectedItem In .SelectedItems
                '打开被合并工作簿
                Dim tempwb As Workbook
                Set tempwb = Workbooks.Open(vrtSelectedItem)
                '复制工作表
                tempwb.Worksheets(1).Copy Before:=newwb.Worksheets(i)
                '把新工作簿的工作表名字改成被复制工作簿文件名,这儿应用于xlsx文件,即Excel2007的文件,如果是Excel97-2003,需要改成xls
                newwb.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xlsx", "")
                '关闭被合并工作簿
                tempwb.Close SaveChanges:=False
                i = i + 1
            Next vrtSelectedItem
        End If
    End With
    Set fd = Nothing
    MsgBox Prompt:="合并完成", Buttons:=vbInformation + vbOKCancel, Title:="逗号Office技巧"
End Sub



忽略隐藏工作表拆分

Sub Copy_Every_Sheet_To_New_Workbook()
'Working in 97-2013
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim sh As Worksheet
    Dim DateString As String
    Dim FolderName As String
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With
    'Copy every sheet from the workbook with this macro
    Set Sourcewb = ThisWorkbook
    'Create new folder to save the new files in
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
    MkDir FolderName
    'Copy every visible sheet to a new workbook
    For Each sh In Sourcewb.Worksheets
        'If the sheet is visible then copy it to a new workbook
        If sh.Visible = -1 Then
            sh.Copy
            'Set Destwb to the new workbook
            Set Destwb = ActiveWorkbook
            'Determine the Excel version and file extension/format
            With Destwb
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2013
                    If Sourcewb.Name = .Name Then
                        MsgBox "Your answer is NO in the security dialog"
                        GoTo GoToNextSheet
                    Else
                        Select Case Sourcewb.FileFormat
                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                        Case 52:
                            If .HasVBProject Then
                                FileExtStr = ".xlsm": FileFormatNum = 52
                            Else
                                FileExtStr = ".xlsx": FileFormatNum = 51
                            End If
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If
            End With
            'Save the new workbook and close it
            With Destwb
                .SaveAs FolderName _
                      & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                        FileFormat:=FileFormatNum
                .Close False
            End With
        End If
GoToNextSheet:
    Next sh
    MsgBox "You can find the files in " & FolderName, , "逗号Office技巧"
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub


相关文章
|
8月前
|
前端开发 JavaScript API
我写这10+个JavaScript单行代码,被组长夸代码写得优雅!
我写这10+个JavaScript单行代码,被组长夸代码写得优雅!
|
8月前
|
IDE 安全 程序员
揭秘如何用C编写出无敌的程序代码,你绝对会后悔错过!
揭秘如何用C编写出无敌的程序代码,你绝对会后悔错过!
49 1
|
8月前
|
搜索推荐 IDE 数据库连接
创建你的第一个Visual Basic程序:步步为营
【4月更文挑战第27天】探索Visual Basic编程,从安装Visual Studio开始,创建首个&quot;HelloWorldApp&quot;。在Form Designer中布局界面,添加Label和Button,设置属性。编写代码实现Button点击显示问候语。运行并调试程序,逐步学习更多控件和VB.NET高级概念,提升编程技能。享受编程旅程,创造无限可能!
63 0
|
8月前
你还在为做原型而担心吗?神器我拿出来了,剩下的就看你的了
你还在为做原型而担心吗?神器我拿出来了,剩下的就看你的了
38 0
|
数据采集 搜索推荐 小程序
编程新手:看懂很多示例,却依然写不好一个程序
当然题目本身难度不高,和我们公众号【每周一坑】栏目里的题相比,这个算是小 case 了。不过如果你是一个刚刚接触编程不久,才掌握条件判断、循环、列表的新手来说,还是有点小挑战的。
|
API Windows
VBA 有用的小段代码收藏(日积月累)
VBA 有用的小段代码收藏(日积月累)
135 0
编程基本功:做自解释的测试文档
编程基本功:做自解释的测试文档
64 0
编程基本功:做自解释的测试文档
|
存储 C#
对于‘用C#编写一个员工工资计算’问题的代码编写风格和结构设计考虑的比较【发现自己还是太弱,大家可以在评论区中提出我代码中的不足】
对于‘用C#编写一个员工工资计算’问题的代码编写风格和结构设计考虑的比较【发现自己还是太弱,大家可以在评论区中提出我代码中的不足】
129 0
对于‘用C#编写一个员工工资计算’问题的代码编写风格和结构设计考虑的比较【发现自己还是太弱,大家可以在评论区中提出我代码中的不足】
|
存储 Web App开发 编解码
对Web设计和开发人员有用的15个Chrome插件
导读:原文作者Brian在freelancefolder.com发表了一篇《15 Useful Google Chrome Extensions for Web Designers and Developers》,由伯乐在线敏捷翻译组编译,文章介绍了非常有用的15个Chrome插件。
1540 0