Sub SetColor() On Error GoTo ErrorHandler '添加错误处理程序 Dim r As Range, arr() As String '声明变量 For Each r In Range("A:QE") arr = Split(r, ",") r.Interior.Color = RGB(CInt(arr(0)), CInt(arr(1)), CInt(arr(2))) Next Exit Sub '正常退出子过程 ErrorHandler: '错误处理程序 MsgBox "Error: " & Err.Description '显示错误消息 End Sub加进度条
时间: 2023-12-11 10:05:56 浏览: 123
Android中Paint类和Canvas类的方法汇总
你可以使用 VBA 的 UserForm 控件来实现一个进度条,代码示例如下:
1. 首先创建一个 UserForm,命名为 ProgressBarForm,添加一个 Label 控件,命名为 Label1,设置 Caption 为 “Loading...”;
2. 在 Sub SetColor() 中添加以下代码:
```
Dim r As Range, arr() As String '声明变量
Dim total As Integer, count As Integer '声明变量
total = Range("A:QE").Count '获取单元格总数
count = 0 '初始化计数器
For Each r In Range("A:QE")
arr = Split(r, ",")
r.Interior.Color = RGB(CInt(arr(0)), CInt(arr(1)), CInt(arr(2)))
count = count + 1 '每处理一个单元格计数器加一
ProgressBarForm.Label1.Caption = "Loading..." & count / total * 100 & "%" '更新进度条
DoEvents '刷新窗口,防止窗口假死
Next
Unload ProgressBarForm '关闭进度条窗口
```
3. 在 Sub SetColor() 的开头添加以下代码,显示进度条窗口:
```
ProgressBarForm.Show vbModeless '显示进度条窗口
```
完整代码如下:
```
Sub SetColor()
On Error GoTo ErrorHandler '添加错误处理程序
Dim r As Range, arr() As String '声明变量
Dim total As Integer, count As Integer '声明变量
total = Range("A:QE").Count '获取单元格总数
count = 0 '初始化计数器
ProgressBarForm.Show vbModeless '显示进度条窗口
For Each r In Range("A:QE")
arr = Split(r, ",")
r.Interior.Color = RGB(CInt(arr(0)), CInt(arr(1)), CInt(arr(2)))
count = count + 1 '每处理一个单元格计数器加一
ProgressBarForm.Label1.Caption = "Loading..." & count / total * 100 & "%" '更新进度条
DoEvents '刷新窗口,防止窗口假死
Next
Unload ProgressBarForm '关闭进度条窗口
Exit Sub '正常退出子过程
ErrorHandler: '错误处理程序
MsgBox "Error: " & Err.Description '显示错误消息
End Sub
```
阅读全文