vba - Excel VBA 进度条显示基于活动工作表的宏进度
问题描述
我在公司的第三周是一名学徒软件开发人员,他们决定让我开始使用 excel 宏和 VBA。到目前为止,这是一个很好的挑战,我正在学习越来越多的阅读代码。我目前的任务是了解公司用来向客户收费的 Excel 拆分器。我已经设法理解代码,以某种方式操纵它,甚至增加了更多的客户和公司。
他们现在要求我对代码进行一些优化并尝试使其运行得更快。我的想法是关闭屏幕更新和所有这些,并尝试显示宏的进度条。我最初希望根据不同的子/功能运行进度条,但这似乎不太容易。
所以:我希望你们中的一个可以帮助/建议我如何根据活动工作表的数量来运行进度条,因为每次拆分器与客户一起完成时,它都会创建一个新工作表。
这是第一家公司的代码:
'Calling the functions
Private Sub CommandButton1_Click()
UserForm1.Show
code
Dim lastrow As Long
lastrow = Sheets("Koh").Range("A" & Rows.count).End(xlUp).row
For i = 2 To lastrow
Call GenericFindAndSplit(Sheets("Koh").Range("A" & i), Sheets("Koh").Range("B" & i), Sheets("Koh").Range("C" & i))
Next i
End Sub
Function GenericFindAndSplit(SheetName As String, LowExt As Integer, HighExt As Integer)
Dim count As Integer
count = 2
'Add a new sheet to the workbook
'Worksheet name is obtained from the "Koh" tab
Sheets.Add(After:=Sheets(Sheets.count)).Name = SheetName
'Copy first row of the Data sheet and paste to the newly created sheet
Sheets("Data-KM").Rows(1).EntireRow.Copy
Sheets(SheetName).Rows(1).PasteSpecial Paste:=xlPasteFormulas
'Search the Data sheet for the selected extensions and paste them on the newly created sheet
For r = Sheets("Data-KM").UsedRange.Rows.count To 1 Step -1
If Sheets("Data-KM").Cells(r, "L") >= LowExt And Sheets("Data-KM").Cells(r, "L") <= HighExt Then
Sheets("Data-KM").Rows(r).EntireRow.Copy
Sheets(SheetName).Range("A" & count).PasteSpecial Paste:=xlPasteFormulas
count = count + 1
End If
Next
End Function
Sub ExportSheetsToCSV()
'If Dir("C:\Temp\", vbDirectory) = "" Then
'Kill ("C:\Temp\") & "*.*"
'End If
Dim xWs As Worksheet
Dim xcsvFile As String
For Each xWs In Application.ActiveWorkbook.Worksheets
'Loop for each worksheet on the workbook
If xWs.Name <> "Sheet1" And xWs.Name <> "Data-KM" And xWs.Name <> "Koh" Then
xWs.Copy
'Copy worksheet
xcsvFile = "GlobalCDR_" & ActiveSheet.Name & ".csv"
'Copies files into the C Drive under Temp
Application.ActiveWorkbook.SaveAs Filename:="C:\Temp\" & xcsvFile, FileFormat:=xlCSV
ThisWorkbook.Saved = True
Application.DisplayAlerts = False
Application.ActiveWorkbook.Close
End If
Next
'Automatically saves and closes workbook
ThisWorkbook.Saved = False
Application.DisplayAlerts = False
'Application.Quit
End Sub
以下是我尝试根据活动工作表编写进度的尝试
Sub code()
Dim i As Integer, j As Integer, pctCompl As Single
For i = 1 To 100
For ActiveWorkbook = 1 To 7
ActiveWorkbook.Value = j
Next j
pctCompl = i
progress pctCompl
Next i
End Sub
Sub progress(pctCompl As Single)
UserForm1.Text.Caption = pctCompl & "% Completed"
UserForm1.Bar.Width = pctCompl * 2
DoEvents
End Sub
最后这是我创建的用户窗体的代码:
Private Sub UserForm_Activate()
code
End Sub
非常感谢您提供的任何建议,在此先感谢您,也感谢您之前的贡献,在这三周的时间里,它们对我来说非常宝贵:)
解决方案
推荐阅读
- java - 用 Java 模拟 SQL shell 模拟器
- amazon-web-services - 网络负载均衡器侦听具有多个目标的多个端口
- javascript - 温斯顿日志记录仅在回调中有效,在 catch 块中无效
- c# - 如何使用 SGen 为映射到两级数组的 XML 模式生成序列化程序集?
- git - git标签不再被识别
- excel - 如何在索引列(目录)旁边添加动态列?
- angular - PrimenG 单选按钮图标未显示
- javascript - pikaday 输入语言还是英文
- angular - 将单个订阅变量与 BehaviorSubject 一起使用
- python - 如何在 Python Selenium 中使用 xpath 或 css 选择器查找元素