VBA batch copy Excel table and update the formula in the table

1. Presentation of the question

There is template workbook 1, which has two worksheets, Sheet A and Sheet B. Now it is required to batch copy worksheet B in workbook 1 to nearly a hundred other workbooks in the current directory, keep the original worksheet name unchanged, and update the formulas in it to refer to the content of the current workbook.

2. Algorithm analysis

If you manually copy, paste, and then use the method of find and replace to update the formula, the format of the worksheet may be changed, and it will also be very cumbersome and time-consuming.

So consider using VBA to solve this problem in batches. The algorithm steps are as follows:

  1. To get the contents of sheet B in workbook 1
  2. Traverse all xls files in the current directory and exclude workbook 1.xls
  3. After opening the files in batches, copy them, then update the formulas, and finally save them.

3. Code display

The following is a program written by VBA. You need to put this program in the newly created module in workbook 1.xls, and then run it.

Sub BatchCopyWorksheetFromAnotherWorkbook()
    Dim fso As Object
    Dim folderPath As String
    Dim file As Object
    Dim sourceWorkbook As Workbook
    Dim targetWorkbook As Workbook
    Dim targetWorksheet As Worksheet
    Dim sourceWorksheet As Worksheet
    Dim sourceWorkbookName As String
    Dim sourceWorksheetName As String
    ' 获取当前目录路径
    folderPath = ThisWorkbook.Path
    
    ' 创建FileSystemObject对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    sourceWorkbookName = ActiveWorkbook.Path & "/工作簿1.xls"  ' 替换为实际的源工作簿文件名
    sourceWorksheetName = "工作表B"  ' 替换为实际的源工作表名称
    Set sourceWorkbook = Workbooks.Open(sourceWorkbookName) 
    
    ' 遍历当前目录下的所有文件
    For Each file In fso.GetFolder(folderPath).Files
        ' 排除文件名中包含"工作簿1.xls"的文件
        If Not file.Name Like "*工作簿1.xls*" And file.Name Like "*.xls*" Then
            ' 打开源工作簿
            Set targetWorkbook = Workbooks.Open(file.Path)
            
            ' 创建目标工作表
            'Set targetWorkbook = ThisWorkbook ' 当前工作簿
            'Set targetWorksheet = targetWorkbook.Sheets.Add(After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count))
            'targetWorksheet.Name = "工作表B"
            
            ' 复制源工作表到目标工作簿
            Set sourceWorksheet = sourceWorkbook.Sheets(sourceWorksheetName) ' 替换为实际的源工作表名称
            'sourceWorksheet.Copy After:=targetWorksheet
            
            sourceWorksheet.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
            
            ' 替换公式中的引用
            targetWorkbook.Sheets(targetWorkbook.Sheets.Count).Cells.Replace What:="[工作簿1.xls]", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
                
            ' 关闭源工作簿,不保存更改
            targetWorkbook.Close SaveChanges:=True

        End If
    Next file
End Sub

4. Post-school reflection

  1. VBA has obvious advantages in operating Excel, especially in format copying.
  2. Absolute references should be used in VBA, not relative references, otherwise an error may be reported.
  3. Enable before writing: Option Explicit, which explicitly requires all variables to be declared in advance.
  4. VBA is also an object-oriented programming language, and it has something in common with Python, so it can learn from each other.

Guess you like

Origin blog.csdn.net/henanlion/article/details/131679547