【1】工作表批量另存為獨立的工作簿 Sub 工作表批量另存為獨立的工作簿() Dim oWK As Worksheet Dim oWB As Workbook Dim sPath As String Dim sName As String If MsgBox('現(xiàn)在開始將把各工作表獨立另存為工作簿文件,請再次檢查格式數(shù)據(jù)是否正確?', vbYesNo, '重要提示') = vbYes Then sPath = Excel.ThisWorkbook.Path Excel.Application.ScreenUpdating = False Excel.Application.DisplayAlerts = False For Each oWK In Excel.ThisWorkbook.Worksheets With oWK '將工作表名稱作為工作簿的名稱保存 sName = .Name .Copy Set oWB = Excel.Application.ActiveWorkbook oWB.SaveAs sPath & '\' & .Name, xlOpenXMLWorkbook oWB.Close End With Next Excel.Application.ScreenUpdating = True Excel.Application.DisplayAlerts = True MsgBox '操作結(jié)束' End If End Sub 【2】插入圖片批注 Sub 插入圖片批注() Dim a a = MsgBox('使用說明:1、請確認您的圖片文件存在與此文件同一目錄下的名稱為pic的文件夾中。2、選中要添加圖片批注的單元格。') If a = 1 Then On Error Resume Next Dim MR As Range Dim Pics As String For Each MR In Selection If Not IsEmpty(MR) Then MR.Select MR.AddComment MR.Comment.Visible = False MR.Comment.Text Text:='' MR.Comment.Shape.Fill.UserPicture PictureFile:=ActiveWorkbook.Path & '\pic\' & MR.Value & '.jpg' End If Next End If End Sub [3]行列轉(zhuǎn)換 Sub 行轉(zhuǎn)列() Dim i As Long, j As Long, k As Long Dim m As Long, n As Long Dim arr, brr, t On Error GoTo last t = Timer Application.ScreenUpdating = False Worksheets('【行】數(shù)據(jù)').Activate Worksheets('【行】數(shù)據(jù)').AutoFilterMode = False Worksheets('【行】數(shù)據(jù)').Rows('1:1').AutoFilter ActiveWorkbook.Worksheets('【行】數(shù)據(jù)').AutoFilter.Sort.SortFields.Add Key:=Range( _ 'A1'), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets('【行】數(shù)據(jù)').AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Worksheets('【行】數(shù)據(jù)').AutoFilterMode = False Worksheets('【列】數(shù)據(jù)').Rows('1:1048576').ClearContents i = 2 m = Worksheets('【行】數(shù)據(jù)').Cells(1, 1).CurrentRegion.Columns.Count n = WorksheetFunction.CountA(Worksheets('【行】數(shù)據(jù)').Range(Cells(1, 2), Cells(WorksheetFunction.CountA(Worksheets('【行】數(shù)據(jù)').Columns('A:A')), m))) If n <= 1048580 Then '判斷是否超出excel表的行數(shù) ReDim arr(1 To n, 1 To 2) brr = Worksheets('【行】數(shù)據(jù)').Cells(1, 1).CurrentRegion.Value For j = 2 To UBound(brr) For k = 1 To UBound(brr, 2) If Len(brr(j, k)) = 0 Then Exit For If k = 1 Then arr(i, 1) = brr(j, 1) k = k + 1 arr(i, 2) = brr(j, k) Else i = i + 1 arr(i, 1) = arr(i - 1, 1) arr(i, 2) = brr(j, k) End If Next k i = i + 1 Application.StatusBar = '正在處理數(shù)據(jù):' & j & '行/' & k - 1 & '列' Next j Worksheets('【列】數(shù)據(jù)').Rows('1:1048576').ClearContents arr(1, 1) = Worksheets('【行】數(shù)據(jù)').Cells(1, 1).Value arr(1, 2) = Worksheets('【行】數(shù)據(jù)').Cells(1, 2).Value Worksheets('【列】數(shù)據(jù)').Cells(1, 1).Resize(i, 2) = arr Worksheets('【列】數(shù)據(jù)').Activate Application.StatusBar = '處理完成!' Erase arr Erase brr Else MsgBox '行轉(zhuǎn)換成列后的數(shù)據(jù)將超出Excel表行數(shù)限制!' Exit Sub End If Application.ScreenUpdating = True MsgBox '共用時:' & Round(Timer - t, 3) & ' s' last: End Sub
|
|
來自: Excel實用知識 > 《行列轉(zhuǎn)換》