Kill filepath 删除之间,先用 Dir 判断文件是否存在。
Function ExistSheetName(name As String) Dim sht As Worksheet For Each sht In Worksheets If sht.name = name Then ExistSheetName = True Exit Function End If Next ExistSheetName = False End Function
默认工作簿中添加表 Worksheets.Add 同时设置表名称:Worksheets.Add().Name="New Sheet" 指定工作簿中添加表 myWorkbook.Sheets.Add .Add() 方法参数 名称 必需/
' 创建工作簿, Dim newWb As Workbook '或在另一个应用实例(进程)中创建 Dim newWb as appExcel.Workbook ? Set newWb = Workbooks.Add 如果要给工作簿命名,只有在保存时,指定文件名。 wb.SaveAs Filename:="NewWB"
隐藏/取消隐藏工作表 sheet.Visible = xlSheetVisible '取消隐藏 sheet.Visible = xlSheetHidden '隐藏 sheet.Visible=xlSheetVeryHidden '高级隐藏(必需使用VBA才能恢复显示)
VBA对单元格数据进行批量修改时,可以暂时关闭自动计算和屏幕刷新,加快代码运行速度。 '关闭屏幕刷新 Application.ScreenUpdating = False '关闭自动计算(调整
大于等于某个固定值 例如:大于等于3 举例:">=3" 大于等于某个单元格或函数 语法:">="
Function FindColOfValue(ws As Worksheet, val As Variant, rowNum As Long, startCol As Long) Dim i As Long With ws For i = startCol To .Range(.Cells(startCol, rowNum), .Cells(startCol, rowNum)).CurrentRegion.Columns.Count() If .Cells(rowNum, i).Value = val Then FindColOfValue = i Exit Function End If Next End With FindColOfValue = -1 End Function
Function FindRowOfValue(ws As Worksheet, val As Variant, colNum As Long, startRow As Long) Dim r As Long With ws For r = startRow To .Range(.Cells(startRow, colNum), .Cells(startRow, colNum)).CurrentRegion.Rows.Count() If .Cells(r, colNum).Value = val Then FindRowOfValue = r Exit Function End If Next End With End Function
Sort a dictionary by value 依据字典的值进行排序 newDict = {k: v for k, v in sorted(x.items(), key=lambda item: item[1])} import operator # 示例字典 d = {'a': 3, 'e': 4, 'b': 2, 'c': 5, 'd': 1} # 按 key 排序 def dict_sort_by_key(d: dict): sorted_d = sorted(d.items(), key=operator.itemgetter(0)) return dict(sorted_d) # 按 value 排序 def dict_sort_by_val(d: dict): # sorted_dic