这是一个常用而且经典的例子:根据内容,把N个工作表中的内容,合并到一个工作表中;
¤主要知识点¤
1、工作表选取和内容的复制;
2、IF分支语句和For循环语句的使用;
¤工作表样式¤
Option Explicit
Sub 合并工作表()
Dim i As Integer
Dim x As Integer, y As Integer
Dim k As Integer
' Range("A1").CurrentRegion.Select
' Selection.Delete
Range("A1").CurrentRegion.Value = ""
For i = Worksheets.Count To 2 Step -1
If Worksheets(i).Name = "合并工作表" Then Exit For
x = Worksheets(i).Range("A1").CurrentRegion.Rows.Count
y = Worksheets(i).Range("A1").CurrentRegion.Columns.Count
If Worksheets("合并工作表").Range("A1").Value = "" Then
Worksheets(i).Range("A1").Resize(x, y).Copy Worksheets("合并工作表").Range("A1")
Else
k = Range("A1").CurrentRegion.Rows.Count + 1
Worksheets(i).Range("A3").Resize(x - 2, y).Copy Worksheets("合并工作表").Range("A" & k)
End If
Next i
Columns.AutoFit
End Sub