VBA-对于每个循环:已完成的进度条不起作用

时间:2020-03-30 21:23:38

标签: excel vba foreach userform

我创建的宏已完成,并成功遍历范围内的每个客户; rngCust是一列单元格,每个单元格cust中都有不同的客户名称。该宏可能需要很长时间才能运行,因此我尝试插入进度条以显示完成百分比。 rngCust中目前大约有50个单元格(行),但是它会定期更改,因此完成百分比条需要找出最后一行才能创建百分比。

当我使用它们的简单代码复制它们时,我有几个在线示例可以使用,但是即使在论坛和网站上进行了大量研究,我仍无法将其中任何一个成功地集成到宏中。我还是VBA的新手。我确实将其发布在MrExcel上,但我也无法使其与该建议一起使用。

该想法是让进度条显示从cust中的每个rngCust单元到下一个单元的完成百分比,基本上,循环的每次迭代都应更新它。

这是我尝试的最后一组更改,在这里我得到了error msg 1004-"Method Range of Object Global Failed" on the lastrow= in the variable definition;,在error 91-"object variable or with block variable not set"行中也得到了pctCompl=,宏主体中的进度计算

这是我到目前为止所拥有的:

用户表单宏为:

Private Sub UserForm_Activate()

Call CreateCustomerInvoices

End Sub

主要宏CreateCustomerInvoices是:

Dim wb As Workbook
Dim ws As Worksheet
Dim rngBilling As Range
Dim erps As Worksheet
Dim rngERPsumm As Range
Dim rngCust As Range
Dim cust As Range
Dim pctCompl As Single *'this is the first line of new code for the user form, everything above this is from the completed macro which runs correctly*
Dim lastrow As Long *'this is also for the user form*

lastrow = Range("rngCust" & Rows.Count).End(xlUp).Row 
 *'  this is where I get the first error msg, also =rngCust.End(xlUp).Row does not get an error here
  but I still get the one below*

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Billing")
Set rngBilling = ws.Range("rngBilling")
Set erps = wb.Worksheets("ERP Summary")
Set rngERPsumm = erps.Range("rngERPsumm")
Set rngCust = ws.Range("rngCust")

UserForm1.Show *'show user form- this works, the user form comes up*

For Each cust In rngCust.Columns(1).Cells        
*'This is the command for the start of the loop- to look at a cell in each row of the 
first column of the range*

*'below is the user form part which is just as shown, right after the start of the loop:*

pctCompl = cust / lastrow *'measure of progress, this is the second error msg, also tried '=cust/rngCust with no luck; i have not been able to get any further to even know if the rest of the code will work*

With UserForm1
    UserForm1.Text.Caption = pctCompl & "% Completed"
    UserForm1.Bar.Width = pctCompl * 2
End With

DoEvents
*'then it continues into the main body of the macro and the first IF statement, which creates an invoice for the customer, saves and closes the invoice and moves on to the next cust cell in the range*
If statement
    IfElse
    IfElse
Else
End If
Next cust
Unload UserForm1

End Sub

我想学习这一点,并能够为其他宏进行修改。

0 个答案:

没有答案