|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub newSheets() '创建新表
- Dim i As Byte, r As Byte, c As Byte, j As Byte, bm
- Dim ws As Worksheet
- r = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
- c = Worksheets(1).Cells(2, Columns.Count).End(xlToLeft).Column
- Application.DisplayAlerts = False
- On Error Resume Next
- For Each ws In Worksheets '用遍历工作表法删除非数据源表
- If Not ws Is Worksheets(1) Then
- ws.Delete
- End If
- Next
- Worksheets.Add after:=Worksheets(Worksheets.Count)
- ActiveSheet.Name = "清镇市外"
- Worksheets(1).[a1].Resize(2, c).Copy Worksheets("清镇市外").[a1]
- For i = 3 To r
- For Each ws In Worksheets
- If Worksheets(1).Cells(i, "H") <> "清镇" Then
- j = Worksheets("清镇市外").Cells(Rows.Count, 2).End(xlUp).Row + 1
- Worksheets(1).Cells(i, 1).Resize(1, c).Copy Worksheets("清镇市外").Cells(j, 1)
- Worksheets("清镇市外").Cells(j, 1) = j - 2
- Exit For '从这里跳出for each循环worksheet是带值的(重点)
- Else
- If ws.Name = Worksheets(1).Cells(i, "I") Then
- Exit For '同上
- End If
- End If
- Next '从这里跳出for each循环worksheet是不带值的(重点)
- If ws Is Nothing Then
- Worksheets.Add after:=Worksheets(Worksheets.Count)
- ActiveSheet.Name = Worksheets(1).Cells(i, "I")
- Worksheets(1).[a1].Resize(2, c).Copy ActiveSheet.[a1]
- End If
- bm = Worksheets(1).Cells(i, "I")
- j = Worksheets(bm).Cells(Rows.Count, 2).End(xlUp).Row + 1
- Worksheets(1).Cells(i, 1).Resize(1, c).Copy Worksheets(bm).Cells(j, 1)
- Worksheets(bm).Cells(j, 1) = j - 2
- Next
- Worksheets(1).Activate
- Application.DisplayAlerts = True
- End Sub
复制代码 学了一个月了,硬是整出来了,不容易,交个作业与大家共享。感谢老师将我带进门,这种从实例逐步入手的方法非常适合初学者。
|
|