获取工作簿中工作表名称
- Public Sub 获取工作簿表名称()
- Dim sh
- Dim j
- For Each sh In ThisWorkbook.Sheets
- ThisWorkbook.Sheets("测试").Cells(1, "C").Value = sh.Name
- Debug.Print sh.Name
- Next
- End Sub
获取文件夹下所有文件的名称
- Public Sub 获取文件夹下的文件名称()
- Dim mypath, myfile, path
- Dim arr()
- Dim i As Integer
- 'On Error Resume Next
- mypath = "C:\Users\Administrator\Desktop\汇总\汇总\" 'ThisWorkbook.path + "\汇总\"
- myfile = Dir(mypath, vbDirectory)
- Do While myfile <> ""
- If myfile <> "." And myfile <> ".." Then
- path = mypath + myfile
- i = i + 1
- ReDim Preserve arr(1 To 1)
- arr(i) = myfile
- ThisWorkbook.Sheets("测试").Cells(1, "C").Resize(UBound(arr), 1) = Application.Transpose(arr)
- Debug.Print arr(0)
- myfile = Dir
- Else
- myfile = Dir
- End If
- Loop
- End Sub
工作簿和工作表表示
- Public Sub 工作簿和工作表表示()
- '====工作簿
- 'Debug.Print Windows.Count
- 'Debug.Print Workbooks(1).Name
- 'Debug.Print Workbooks("工作表-计划-2022.xlsm").Parent
- 'Dim i
- 'For i = 1 To Workbooks.Count
- '' Debug.Print Workbooks(i).Name
- ' Sheets("测试").Cells(i, "C") = Workbooks(i).Name
- 'Next
- '工作表的表示
- 'Debug.Print Worksheets(1).Name
- 'Debug.Print Sheets(1).Name
- 'Debug.Print Worksheets("目录").Name
- 'Debug.Print Sheet1.Name
- 'Debug.Print ActiveSheet.Name
- 'Dim i
- 'For i = 1 To Sheets.Count
- '' Debug.Print Workbooks(i).Name
- ' Sheets("测试").Cells(i, "C") = Sheets(i).Name
- 'Next
- 'Dim sh, k
- 'For Each sh In Sheets
- ' k = k + 1
- ' Sheets("测试").Cells(k, "i") = Sheets(k).Name
- 'Next
- 'For Each sh In Worksheets
- ' k = k + 1
- ' Sheets("测试").Cells(k, "i") = sh.Name
- 'Next
- '判断工作表是否存在
- 'Dim sh, k, sn
- 'For Each sh In Sheets
- ' sn = sh.Name
- ' If sn = "测试" Then
- ' MsgBox "存在" + sh
- ' Exit Sub
- ' End If
- 'Next
- 'MsgBox "不存在"
- 'Dim i
- 'For i = 1 To Sheets.Count
- ' If Sheets(i).Name = "测试" Then
- ' MsgBox "存在"
- ' Exit Sub
- ' End If
- 'Next
- 'MsgBox "不存在"
- End Sub
ike和offset和resize和end的使用
- Public Sub like和offset和resize和end的使用()
- ''like的使用
- 'Dim a
- ''a = "abc" Like "*c"
- ''a = "abc" Like "?c"
- ''a = "abc1" Like "*c#"
- 'Debug.Print a
- 'offset 的使用
- 'Dim i
- 'For i = 2 To 8 Step 2
- '' [c4:c9].Copy [c4:c9].Offset(i)
- ' [c4:c9].Copy [c4:c9].Offset(, 1) '偏离行、列
- 'Next
- 'resize使用
- '[a1].Resize(2, 3).Select
- 'Debug.Print [b2].CurrentRegion.Rows.Count
- 'Debug.Print [b2].CurrentRegion.Columns.Count
- '[a2].Resize(1, 2).Copy [a3].Offset(3) '偏离三行
- 'end 属性
- Dim i, j, k, l
- i = Cells(Rows.Count, 3).End(xlUp).Row
- j = Cells(Rows.Count, 3).End(xlUp).Address
- k = Cells(1, 3).End(xlToLeft).Column
- l = Cells(2, "e").End(xlToLeft).Address '最后一个行位置
-
- Debug.Print l
- 'ActiveSheet.UsedRange.Select
- [e3].CurrentRegion.Select
- End Sub
工作表合并
- Public Sub 工作表合并()
- Dim i%, rs%, rss%, st As Worksheet, zst As Worksheet
- Dim crs
- On Error Resume Next
- Set zst = Sheets("测试") '
- For i = 1 To Sheets.Count
- If Sheets(i).Name <> "测试" Then
- rs = Sheets(i).UsedRange.Rows.Count ' 计算复制表格行数
- crs = Sheets(i).UsedRange.Columns.Count ' 计算复制表格列数
- ' Debug.Print crs
- rss = zst.UsedRange.Rows.Count + 3 '计算目标表格的最后行位置
- Sheets(i).UsedRange.Copy zst.Cells(rss, 1) '复制1-3表的数据到总表中
- zst.Cells(rss, crs + 1).Resize(rs - 1) = Sheets(i).Name '将复制的表名填充
- End If
- Next
- End Sub
拆分到工作簿
- Sub 拆分到工作簿()
- Dim wk As Workbook, ss$, k%
- Dim sht
- Application.DisplayAlerts = False
- For Each sht In ThisWorkbook.Sheets
- Set wk = Workbooks.Add
- k = k + 1
- Workbooks(1).Sheets(k).Copy Workbooks(2).Sheets(1)
- ss = ThisWorkbook.path & "\" & sht.Name & ".xlsx"
- wk.SaveAs ss
- wk.Close
- Next
- Application.DisplayAlerts = True
- MsgBox "拆分工作簿完成!"
- End Sub
获取文件夹下的文件名称
- Public Sub 获取文件夹下的文件名称()
- Dim mypath, myfile, path
- Dim arr()
- Dim i As Integer
- 'On Error Resume Next
- mypath = "C:\Users\Administrator\Desktop\汇总\汇总\" 'ThisWorkbook.path + "\汇总\"
- myfile = Dir(mypath, vbDirectory)
- Do While myfile <> ""
- If myfile <> "." And myfile <> ".." Then
- path = mypath + myfile
- i = i + 1
- ReDim Preserve arr(1 To 1)
- arr(i) = myfile
- ThisWorkbook.Sheets("测试").Cells(1, "C").Resize(UBound(arr), 1) = Application.Transpose(arr)
- Debug.Print arr(0)
- myfile = Dir
- Else
- myfile = Dir
- End If
- Loop
- End Sub
合并目录所有工作簿全部工作表
- Sub 合并目录所有工作簿全部工作表()
- On Error Resume Next
- Dim MP, MN, AW, Wbn, wn
- Dim Wb As Workbook
- Dim i, a, b, d, c, e, last_row, ni
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- '--初始化
- Workbooks("台账自检.xlsm").Sheets("热联").Delete
- Workbooks("台账自检.xlsm").Sheets("明细").Delete
- Workbooks("台账自检.xlsm").Sheets("汇总").Range("A:M") = ""
- '--
- MP = ActiveWorkbook.path '工作簿路径
- 'MP = "C:\Users\HONORS\Desktop\结算小组数据检查" '工作簿路径
- MN = Dir(MP & "\" & "*.xlsx") '工作簿路径
- 'Set Newbook = Workbooks.Add
- AW = ActiveWorkbook.Name
- Num = 0
- ni = 0
- e = 3 '标题栏数量
- Do While MN <> ""
- If MN <> AW And MN <> MP & "台账自检.xlsm" Then '"C:\Users\HONORS\Desktop\结算小组数据检查\台账自检.xlsm"
- Debug.Print MN
- ni = ni + 1 '判断导入表的顺序
- Debug.Print "导入第" & ni & "表"
- Set Wb = Workbooks.Open(MP & "\" & MN)
- a = a + 1
- '工作簿判断
- ' Newbook.Sheets.Add After:=Newbook.Sheets(Newbook.Sheets.Count) '新建工作表
- ' Newbook.Sheets.Add.Name = Wb.ActiveSheet.Name
- Workbooks("台账自检.xlsm").Sheets.Add.Name = Wb.ActiveSheet.Name
- ' With Newbook.Sheets("Sheet1") 'Workbooks(1).Sheets("Sheet4")
- With Workbooks("台账自检.xlsm").ActiveSheet
- ' With Newbook.ActiveSheet
- d = Wb.ActiveSheet.UsedRange.Columns.Count '判断列数
- c = Wb.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'Wb.Sheets(1).UsedRange.Rows.Count - 1 '判断行数
- Debug.Print Wb.Sheets(1).Name&; "单表最后一行" & c
- last_row = .Cells(Rows.Count, 1).End(xlUp).Row '.Range("a1048576").End(xlUp).Row + 1 '最后一行位置
- Debug.Print "终表最后一行" & last_row
- Wb.ActiveSheet.Range("a1:BP" & c).Copy .Cells(last_row + 1, 1) '复制数据
- wn = Wb.ActiveSheet.Name
- .Cells(4, "Z") = "表名"
- .Cells(e + 1, "Z").Resize(c - 2, 1) = MN & wn
- e = e + c '累计行数
- .Range("A:L").RowHeight = 12 '行高
- .Range("A:L").ColumnWidth = 10 '列宽
- Wbn = Wbn & Chr(13) & Wb.Name
- Wb.Close False '关闭工作簿
- End With
- End If
- MN = Dir
- Loop
- '--
- 'Newbook.SaveAs Filename:=MP & "\" & "进出库汇总3.xlsx"
- '复制数据
- With Workbooks("台账自检.xlsm").Sheets("汇总")
- '运输台账复制
- Workbooks("台账自检.xlsm").Sheets("热联").Range("C:C").Copy .Cells(1, "A")
- Workbooks("台账自检.xlsm").Sheets("热联").Range("D:D").Copy .Cells(1, "B")
- Workbooks("台账自检.xlsm").Sheets("热联").Range("H:H").Copy .Cells(1, "C")
- Workbooks("台账自检.xlsm").Sheets("热联").Range("o:o").Copy .Cells(1, "D")
- Workbooks("台账自检.xlsm").Sheets("热联").Range("p:p").Copy .Cells(1, "E")
- '工程台账复制
- Workbooks("台账自检.xlsm").Sheets("明细").Range("A:A").Copy .Cells(1, "h") '计划号
- Workbooks("台账自检.xlsm").Sheets("明细").Range("h:h").Copy .Cells(1, "i") '计划号
- Workbooks("台账自检.xlsm").Sheets("明细").Range("i:i").Copy .Cells(1, "j") '计划号
- Workbooks("台账自检.xlsm").Sheets("明细").Range("j:j").Copy .Cells(1, "k") '计划号
- Workbooks("台账自检.xlsm").Sheets("明细").Range("l:l").Copy .Cells(1, "k") '车牌号
- .Cells(2, "L").Value = "匹配"
- c = .Cells(Rows.Count, "j").End(xlUp).Row
- .Cells(3, "L").Resize(c, 1) = "=VLOOKUP(J3,A:E,3,FALSE)"
- .Range("A:L").RowHeight = 12 '行高
- .Range("A:L").ColumnWidth = 12 '列宽
- .Range("A:L").Font.Size = 8 '字号
- .Range("A:L").Font.Name = "微软雅黑" '字体
- End With
- Workbooks("台账自检.xlsm").Sheets("汇总").Activate
- Workbooks("台账自检.xlsm").Save
- Range("a1").Select
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- MsgBox "共合并了" & a & "个工作薄下全部工作表。如下:" & Chr(13) & Wbn, vbInformation, "提示"
- End Sub
新建文件夹
- Public Sub 新建文件夹()
- Dim spatch
- Dim newmd, t
- Dim i
- spatch = "C:\Users\Administrator\Desktop\汇总" 'Excel.ThisWorkbook.path
- newmd = spatch & "\" & ActiveSheet.Name
- t = Dir(newmd, vbDirectory)
- If t = "" Then
- i = MsgBox("文件夹不存在是否创建并写入?", vbYesNo + vbQuestion, "存盘提示")
- Else
- i = MsgBox("文件夹已存在", vbOKOnly, "存盘提示")
- Exit Sub
- End If
- If i = 6 Then mkdir newmd Else Exit Sub
- End Sub
字典和数组使用
- Public Sub 字典和数组()
- '数组的使用
- 'Dim arr(1 To 4)
- 'Dim rng
- 'Dim n
- 'For Each rng In Sheets("测试").[a1:a3]
- ' n = n + 1
- ' arr(n) = rng
- 'Next
- 'Dim arr
- 'arr = Application.Transpose(Sheets("测试").[a1:a3])
- 'Sheets("测试").Range("d2:d" & UBound(arr)) = arr
- 'Sheets ("测试")
- '=========字典使用
- Dim d As New Dictionary
- Dim j, k
- Set d = CreateObject("scripting.dictionary")
- d.Add "张三", "123"
- d.Add "李四", "345"
- j = d.Keys
- 'Debug.Print 'Application.Index(d.Keys, 2) 'd.Keys(0)
- 'Debug.Print d("李四") 'Application.Index(d.Items, 2) 'd.Items(1)
- '------exist,remove ,remove all 方法
- 'd.Remove ("李四")
- d.RemoveAll
- 'Debug.Print d.Exists("李四")
- 'd.Key("李四") = "王五"
- 'd.Item("王五") = "789"
- 'k = d.Items
- 'Debug.Print d.Item("王五") 'd.Count
- '字典写入
- Dim arr, arr1
- Dim rng, rngs
- arr = Sheets("测试").Range("a1:a" & Sheets("测试").Cells(Rows.Count, "a").End(xlUp).Row)
- For Each rng In arr
- ' arr1 = VBA.Split(rng, "|")
- For Each rngs In arr
- d(rngs) = ""
- Next
- ' i = VBA.Join(d.Keys, "|")
- ' n = n + 1
- ' Sheet2.Cells(n, "a") = i
- ' d.RemoveAll
- Next
- Sheets("测试").[f2].Resize(d.Count, 1) = Application.Transpose(d.Keys)
- End Sub
判断工作表是否存在并新建
- Public Sub 新建工作表()
- On Error Resume Next
- Dim sh
- Dim sname
- Dim ws
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- sname = "测试"
- ' Debug.Print ws
- If ThisWorkbook.Worksheets(sname) Is Nothing Then
- '新建工作表
- ThisWorkbook.Sheets.Add after:=Sheets(ThisWorkbook.Worksheets.Count)
- ThisWorkbook.Sheets(ThisWorkbook.Worksheets.Count).Name = sname
- '复制数据
- ThisWorkbook.Sheets("记录").Range("A:T").Copy 'UsedRange.Copy
- ThisWorkbook.Worksheets(sname).Paste 'PasteSpecial Paste:=xlPasteValues '复制数据
- ThisWorkbook.Worksheets(sname).Range("A:j").RowHeight = 23 '行高
- Else
- ThisWorkbook.Worksheets(sname).Delete
- ' MsgBox "新增错误,表名已存在", vbOKOnly, "提示"
- ' Exit Sub
- End If
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End Sub
批量获取区域
- Public Sub 批量获取区域()
- Dim k%
- Dim lr
- Dim di, di1
- Dim i
- Dim rngs, rng
- Dim ad, ads
- Dim d As New Dictionary
- 'On Error Resume Next
- Set d = CreateObject("scripting.dictionary")
- With Sheets("测试")
- lr = .Cells(Rows.Count, "G").End(xlUp).Row '获取最后一行
- Set rngs = .Range("G1:G" & lr) '确认列
- For Each rng In rngs
- If rng.Value Like "1" Then
- ' Debug.Print rng.Row
- ad = rng.EntireRow.Range("a1:EQ1").Value
- d(rng.Row) = ad
- ' Set ads = Union(ad, ads)
- di = Application.Transpose(Application.Transpose(Application.Transpose(d.Items)))
- End If
- Next
- ' Debug.Print UBound(di, 2)'二维数组列数
- For i = 1 To d.Count
- Sheets("测试").Range("a" & i).Resize(, UBound(di, 2)) = Application.Index(di, i)
- Next i
-
- End With
- '区域合并,整体合并
- End Sub