回復 5#學到老死
'彙出到分頁
'先決條件:"彙總表"欄B中的sheets必須存在
Sub 彙出到分頁()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim Lst1 As Integer, Lst2 As Integer
Dim I As Integer, J As Integer, shName As String
Set sh1 = Sheets("彙總表")
Lst1 = sh1.[B65536].End(xlUp).Row
For I = 5 To Lst1
shName = sh1.Cells(I, 2)
For J = 1 To Sheets.Count
If Sheets(J).Name = shName Then
Lst2 = Sheets(J).[B65536].End(xlUp).Row + 1
If Lst2 < 5 Then Lst2 = 5
sh1.Cells(I, 2).Resize(1, 4).Copy Sheets(J).Cells(Lst2, 2)
Exit For
End If
Next
Next
End Sub作者: c_c_lai 時間: 2016-2-20 08:56
'若資料龐大, 彙出資料到分頁, 可改用本VBA, 會快很多
'先決條件:"彙總表"欄B中的工作表名稱的sheets必須存在
'且已按工作表名稱排序
Sub 彙出到分頁2()
Dim sh1 As Worksheet
Dim Lst1 As Integer, shNameCnt As Integer
Dim I As Integer
Set sh1 = Sheets("彙總表")
Lst1 = sh1.[B65536].End(xlUp).Row
I = 5
Do
shName = sh1.Cells(I, 2)
sh1.[C3].FormulaR1C1 = "=COUNTIF(C[-1],""=""&R" & I & "C[-1])" '計算同名的工作表有幾個
sh1.Cells(I, 2).Resize(sh1.[C3], 4).Copy Sheets(shName).[B5] '批次複製
I = I + sh1.[C3]
Loop Until I > Lst1
End Sub作者: 學到老死 時間: 2016-2-20 17:09
If Sheets(flg).Name = vSht Then checkShts = flg: Exit Function
Next flg
checkShts = 0
End Function
複製代碼
作者: yen956 時間: 2016-2-21 13:40
'借用 c大 的概念, 新增分頁, 這樣較有彈性
'請貼到 "彙總表"
'彙出到分頁3
'判判分頁是否存在
Function shExist(ByVal shName As String) As Boolean
Dim I As Integer
shExist = False
For I = 1 To Sheets.Count
If Sheets(I).Name = shName Then
shExist = True
Exit Function
End If
Next
End Function
Sub 彙出到分頁3()
Dim sh1 As Worksheet
Dim Lst1 As Integer, shNameCnt As Integer
Dim I As Integer, J As Integer
'********************
'清除分頁內容, 如有其他重要分頁, 如"統計"等, 兩列*****間, 請註解掉或刪掉
For J = 1 To Sheets.Count
If Sheets(J).Name <> "彙總表" Then Sheets(J).Cells.Clear
Next
'**************
Sub Macro1()
Dim xArea As Range, i&, T$, TT$, Sht As Worksheet
Set xArea = Range([B4], Cells(Rows.Count, "B").End(xlUp)(1, 4))
For i = 2 To xArea.Rows.Count
T = xArea(i, 1): Set Sht = Nothing
If T = "" Or InStr(TT & "/", "/" & T & "/") Then GoTo 101
On Error Resume Next: Set Sht = Sheets(T): On Error GoTo 0
If Sht Is Nothing Then Set Sht = Sheets.Add(After:=Sheets(Sheets.Count))
Sht.Name = T: Sht.UsedRange.Clear
With xArea
.Parent.Select
.AutoFilter Field:=1, Criteria1:=T
.Copy Sht.[B4]
End With
TT = TT & "/" & T
101: Next i
ActiveSheet.AutoFilterMode = False
End Sub作者: yen956 時間: 2016-2-22 10:22
回復 13#准提部林
准大你好!!
又學到一招, 直接
Set Sht = Nothing
If T = "" Or InStr(TT & "/", "/" & T & "/") Then GoTo 101
On Error Resume Next
Set Sht = Sheets(T)
On Error GoTo 0
If Sht Is Nothing Then
Set Sht = Sheets.Add(After:=Sheets(Sheets.Count))
End If
就可以不必先判斷sht是否存在,真高, 收下, 謝謝!!
但請問 InStr(TT & "/", "/" & T & "/") 的作用是什麼?謝謝!!作者: lpk187 時間: 2016-2-22 11:40