[英]Progress Bar to show how much of the process is completed Excel vba
I have written code to copy paste certain rows from one workbook to another. 我已经编写了将某些行中的某些行复制粘贴到另一个工作簿的代码。 I want a progress bar to show me the progress of the job taking into account each row pasted.
我希望有一个进度条来显示我的工作进度,并考虑到粘贴的每一行。 For example: If I have to copy-paste 10 rows, then once 1 row is pasted it should show: 10% completed.
例如:如果我必须复制粘贴10行,那么一旦粘贴1行,它应该显示:10%完成。
This is a snippet of my code: 这是我的代码的片段:
Sub Automate_Estimate()
Set Wb = ThisWorkbook
MyFile = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")
Set wkb = Workbooks.Open(MyFile, UpdateLinks:=0)
Application.StatusBar = "Copying In progress..." & Cells(Rows.Count, 2).End(xlUp).Row & "% completed"
Debug.Print MyFile, DestName
Set rng = Sheets(SourceName).Range("C12:R12")
rng.Copy
Wb.Sheets(DestName).Cells(1, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C30:R30")
rng.Copy
Wb.Sheets(DestName).Cells(24, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C22:R22")
rng.Copy
Wb.Sheets(DestName).Cells(4, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C20:R20")
rng.Copy
Wb.Sheets(DestName).Cells(14, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C40:R40")
rng.Copy
Wb.Sheets(DestName).Cells(17, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C16:R16")
rng.Copy
Wb.Sheets(DestName).Cells(7, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C17:R17")
rng.Copy
Wb.Sheets(DestName).Cells(8, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C21:R21")
rng.Copy
Wb.Sheets(DestName).Cells(16, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Sheets(SourceName).Range("C52:R52")
rng.Copy
Wb.Sheets(DestName).Cells(56, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.StatusBar = "Copying Is complete"
wkb.Close
End Sub
The progress bar code is after 'Set wkb' (After line 2). 进度条形码位于“设置wkb”之后(第2行之后)。 The data is being pasted from the 2nd column.
正在从第二列粘贴数据。 Can somebody help me with this?
有人可以帮我吗? Thanks :)
谢谢 :)
You have to set the StatusBar after every Copy to show a new text. 您必须在每次复制后设置StatusBar才能显示新文本。
You could define a small SubRoutine: 您可以定义一个小的SubRoutine:
Sub CopyRange(fromRange As Range, toRange As Range, completed As Double)
fromRange.Copy
toRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.StatusBar = "Copying In progress..." & Round(completed, 0) & "% completed"
DoEvents
End Sub
Define a Constant that holds the number of steps and increase a variable after every step: 定义一个常量,用于保存步数,并在每个步骤后增加一个变量:
Sub Automate_Estimate()
Const Steps = 10
Dim completed As Double
' ... (Set your Wb stuff here)
completed = 0
Application.StatusBar = "Copying In progress..."
Call CopyRange(Sheets(SourceName).Range("C12:R12"), Wb.Sheets(DestName).Cells(1, 2), completed)
completed = completed + (100 / Steps)
' ... (Add all your copying here and increase completed after every step)
Application.StatusBar = False
End Sub
Updated code as suggested by @FunThomas @FunThomas建议的更新代码
Second sub which is used to call 用于呼叫的第二子
Sub CopyRange(fromRange As Range, toRange As Range, completed As Double)
fromRange.Copy
toRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Application.StatusBar = "Copying In progress..." & Round(completed, 0) & "%
completed"
DoEvents
End Sub
This is the Main Sub 这是主子
Sub Automate_Estimate()
Dim completed As Double
Set Wb = ThisWorkbook
Const steps = 9
'Number of rows copied
MyFile = Application.GetOpenFilename(FileFilter:="Excel
Files,*.xl*;*.xm*")
Set wkb = Workbooks.Open(MyFile, UpdateLinks:=0)
completed = 0
Application.StatusBar = "Copying In progress..."
Call CopyRange(Sheets(SourceName).Range("C12:R12"), Wb.Sheets(DestName).Cells(1, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C30:R30"), Wb.Sheets(DestName).Cells(24, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C22:R22"), Wb.Sheets(DestName).Cells(4, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C20:R20"), Wb.Sheets(DestName).Cells(14, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C40:R40"), Wb.Sheets(DestName).Cells(17, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C16:R16"), Wb.Sheets(DestName).Cells(7, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C17:R17"), Wb.Sheets(DestName).Cells(8, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C21:R21"), Wb.Sheets(DestName).Cells(16, 2), completed)
completed = completed + (100 / steps)
Call CopyRange(Sheets(SourceName).Range("C52:R52"), Wb.Sheets(DestName).Cells(56, 2), completed)
completed = completed + (100 / steps)
Application.StatusBar = False
wkb.Close
DoEvents
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.