调整行高 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Sub Macro1() Dim arr, rng As Range, i& Application.ScreenUpdating = False t = Timer arr = Range("A1" ).CurrentRegion For i = 1 To UBound(arr) If Rows(i).RowHeight > 10 Then If rng Is Nothing Then Set rng = Cells(i, 1 ) Else Set rng = Union(rng, Cells(i, 1 )) End If Next If Not rng Is Nothing Then rng.EntireRow.RowHeight = 10 Application.ScreenUpdating = True MsgBox Timer - t End Sub
释义:遍历 A 列中不为空的行,将行高设置为 10。
细节说明:
Application.ScreenUpdating = False
,关闭视图跟随
t = Timer
,获取当前时间(非本案例必须)
UBound(arr)
,返回引用区域的上限值
获取行数和列数 1、方式一:
1 2 ActiveSheet.UsedRange.Rows.Count ActiveSheet.UsedRange.Columns.Count
注意事项:该方式结果可能会大于现有数量,原因曾经删除过行(或列),而且是非整行或整列删除。该语句仍返回未删除前的值,这部分行虽然已经删除,但是也记录在内。
2、方式二:
1 2 ActiveSheet.Range("A65535" ).End (xlUp).Row ActiveSheet.Range("IV1" ).End (xlToLeft).Column
或
1 2 ActiveSheet.[A65536].End (xlUp).Row ActiveSheet.[IV1].End (xlToLeft).Column
注意事项:只能计算出一列(行)的最后一个单元格所在的行(列)数。本例返回 A 列最后一个单元格所占的行数。
3、方式三:
1 2 Application.CountA(ActiveSheet.Range("A:A" )) Application.CountA(ActiveSheet.Range("1:1" ))
或
1 2 Application.CountA(ActiveSheet.Columns(1 )) Application.CountA(ActiveSheet.Columns(1 ))
注意事项:只能统计一列(行)的实际使用情况,得到的不一定是最后一行(列)的位置。方式二的数值比此方式大时,说明在 A 列的数据间有空白未填写的单元格。
判断文件是否打开 1 2 3 4 5 6 7 8 9 Function isOpen(strName As String ) As Boolean Dim w As Workbook For Each w In Application.Workbooks If w.Name = strName Then isOpen = True : Exit Function End If Next isOpen = False End Function
清除工作表中所有无内容空行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Sub 删除空行() Application.ScreenUpdating = False Dim sheet As Worksheet Dim maxLineNum Dim index As Integer Dim minLineNum As Integer : minLineNum = 0 For Each sheet In Sheets minLineNum = 0 sheet.Activate sheet.Unprotect Password:=123456 maxLineNum = sheet.[A65536].End (xlUp).Row For index = maxLineNum To 3 Step -1 If sheet.Range("B" & Trim(Str(index))).Value = "" Then minLineNum = index End If Next If minLineNum > 0 Then sheet.Range(sheet.Rows(minLineNum), sheet.Rows(maxLineNum - 1 )).Delete End If sheet.Protect Password:=123456 Next End Sub
筛选二维数组重复元素 以二维数组为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Sub RemovingDuplication() Dim indexRow, indexA, indexB As Integer Dim resultTempArray() ReDim resultTempArray(1 To 1 ) Dim tempArray() Dim tempSplit() As String Dim Temp As String For indexRow = 1 To 2 tempArray = Application.index(dataArray, indexRow, 0 ) For indexA = 1 To UBound(tempArray) For indexB = indexA + 1 To UBound(tempArray) If tempArray(indexA) = tempArray(indexB) Then tempArray(indexB) = "@" Next Next Temp = Replace(Join (tempArray, "," ), "@" , "" ) tempSplit = Split(Temp, "," ) For indexA = LBound(tempSplit) To UBound(tempSplit) If tempSplit(indexA) <> "" Then resultTempArray(UBound(resultTempArray)) = tempSplit(indexA) ReDim Preserve resultTempArray(1 To UBound(resultTempArray) + 1 ) End If Next Temp = "" For indexA = 1 To UBound(resultTempArray) - 1 Temp = Temp + "(" + Trim(Str(indexA)) + ")" + resultTempArray(indexA) If indexA <> UBound(resultTempArray) - 1 Then Temp = Temp + " " End If Next With Worksheets("样品数据" ) .Range("类别" & Trim(Str(indexRow))).Value = UBound(resultTempArray) - 1 .Range("详细" & Trim(Str(indexRow))).Value = Temp End With ReDim resultTempArray(1 To 1 ) Next End Sub
实现将指定内容跨文件复制 将 workBook1.xlsm 所有工作表的指定区域复制到 workBook2.xlsx,更新标题,工作表名称不变,并设置打印信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 Sub CopyRangeAcrossFile() Application.ScreenUpdating = False Dim desSheet, desTitle, bottomTitle, sheet As Worksheet Dim maxLineNum Set desSheet = Workbooks.Open(ThisWorkbook.Path & "\" & "workBook2.xlsx" ) For Each sheet In Sheets maxLineNum = WorksheetFunction.CountA(sheet.Columns(1 )) sheet.Activate sheet.Range("A1:M" & Trim(Str(maxLineNum))).Select Selection.Copy desSheet.Sheets(1 ).Activate desSheet.Worksheets.Add().Name = sheet.Name desSheet.Sheets(sheet.Name).Range("A1" ).Select Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False , Transpose:=False desSheet.Sheets(sheet.Name).Range("A1" ).Value = "new" & sheet.Range("A1" ).Value desSheet.Sheets(sheet.Name).Range("A1" ).Font.Size = 14 desSheet.Sheets(sheet.Name).Cells.RowHeight = 28 desSheet.Sheets(sheet.Name).Rows(1 ).RowHeight = 50 desSheet.Sheets(sheet.Name).Columns("A" ).ColumnWidth = 3 With desSheet.Sheets(sheet.Name).PageSetup .Orientation = xlLandscape .LeftMargin = Application.InchesToPoints(1 ) .RightMargin = Application.InchesToPoints(1 ) .PrintArea = "A1:L" & Trim(Str(maxLineNum + 4 )) End With Next End Sub
使用 ReDim 实现二维动态数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Sub DynamicArray() Dim cellType, rowIndex, sheet As Worksheet Dim dataArray() ReDim dataArray(1 To 2 , 1 To 1 ) For Each sheet In Sheets For rowIndex = 1 To 65535 cellType = sheet.Range("O" & Trim(Str(rowIndex))).Value If cellType = "" Then Exit For End If If cellType = "苹果" Then dataArray(1 , UBound(dataArray, 2 )) = cellName ReDim Preserve dataArray(1 To 11 , 1 To UBound(dataArray, 2 ) + 1 ) ElseIf cellType = "荔枝" Then dataArray(2 , UBound(dataArray, 2 )) = cellName ReDim Preserve dataArray(1 To 11 , 1 To UBound(dataArray, 2 ) + 1 ) End If Next Next End Sub
VBA 在工作表插入新行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Sub InsertRow() Dim currentSheet As Worksheet Set currentSheet = ActiveSheet If currentSheet.Name = "Sheet1" Or currentSheet.Name = "Sheet2" Then MsgBox "该表格不允许添加行!" Exit Sub End If Dim maxLineNum Dim insertNum As Integer insertNum = InputBox("输入要插入的行数!" , "输入行数" , "" ) currentSheet.Unprotect Password:=123456 Dim index As Integer For index = 1 To insertNum maxLineNum = WorksheetFunction.CountA(currentSheet.Columns(1 )) currentSheet.Rows(maxLineNum).Insert shift:=xlShiftDown Next currentSheet.Protect Password:=123456 End Sub