excel 巨集使用記錄
Excel 一個工作表進行按行數拆分
1. 如下Excel表,總共有120多行資料,如何將以50行資料為一個工作表進行拆分 Sub ZheFenSheet() Dim r, c, i, WJhangshu, WJshu, bt As Long r = Range("A" & Rows.Count).End(xlUp).Row b = InputBox("請輸入分錶行數") If IsNumeric(b) Then WJhangshu = Int(b) Else MsgBox "輸入錯誤", vbOKOnly, "錯誤" End End If c = Cells(1, Columns.Count).End(xlToLeft).Column bt = 1 '標題行數 'WJhangshu = 50 '每個檔案的行數 WJshu = IIf(r - bt Mod WJhangshu, Int((r - bt) / WJhangshu), Int((r - bt) / WJhangshu) + 1) '------ Set fs = CreateObject("Scripting.FileSystemObject") ' For i = 0 To WJshu Workbooks.Add Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Format(i + 1, String(Len(WJshu), 0)) & "." & fs.GetExtensionname(ThisWorkbook.FullName) '副檔名 Application.DisplayAlerts = True ThisWorkbook.ActiveSheet.Range("A1").Resize(bt, c).Copy ActiveSheet.Range("A1") ThisWorkbook.ActiveSheet.Range("A" & bt + i * WJhangshu + 1).Resize(WJhangshu, c).Copy _ ActiveSheet.Range("A" & bt + 1) ActiveWorkbook.Close True Next End Sub
快速將多個excel表合併成一個excel表
Sub 合併當前目錄下所有工作簿的全部工作表()
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 & "個工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
關於[A65536]的含義: 在1995-2006年,excel工作簿包含65536行,但現在的office 2007中工作簿包含1048576行。[A65536]就是A列的最後一行的意思,這段話的意思就是,從A列最後一行向上找,找到有資料的行為止。 如果是[B65536]則是從B列最後一行向上找,找到有資料的行為止。