一、实现功能要求:求总分。
1、在Excel界面中,初始数据如下:
2/7
2、进入VBE界面中,编写如下代码:
Sub OnErrorResume()
Dim i%
For i = 2 To 6:
Sheet4.Range("d" & i) = Sheet4.Range("b" & i) + Sheet4.Range("c" & i)
Next
End Sub
3、运行代码,则报一下错误;错误原因为:包含字符串类型;
3/7
接下来,将对此错误进行处理;
4、修改代码为:
Sub OnErrorResume()
Dim i%
On Error Resume Next '错误时则恢复;
For i = 2 To 6:
Sheet4.Range("d" & i) = Sheet4.Range("b" & i) + Sheet4.Range("c" & i)
Next
End Sub
4/7
5、点击插入--形状,选择一个形状;
6、选择形状,右键指定宏,选择刚刚定义的宏OnErrorResume;
查看剩余1张图
5/7
7、运行宏;则在有字符串的行中,将不执行,继续执行后面的语句;
6/7
8、当出现某个错误时,直接输出错误位置;
Sub onErrorGoTo88()
Dim i%
On Error GoTo 100 '出现错误时,执行某段语句;
For i = 2 To 6:
Sheet1.Range("d" & i) = Sheet1.Range("b" & i) + Sheet1.Range("c" & i)
Nexti
Exit Sub
100:
MsgBox ("´íÎó³öÏÖÔÚ" & i & "ÐÐ")
Return
End Sub
7/7
综上所述,本文提供两种方法:
1、On Error Resume Next:忽略错误;
2、On Error GoTo line:错误时跳转到其他执行语句;
来对错误语句进行处理。
Private Sub CommandButton1_Click()
Dim Arr, i&, Nm, col%
Dim strNm As String
strNm = "断断" '先组成 “1,2,3,4,……”这样的字符串
For i = 1 To 40
strNm = strNm & "," & i '也可以用其他分隔符
Next i
Nm = Split(strNm, ",")
Sheets("提取").Activate: Row = 1
Cells.ClearContents
For i = 0 To UBound(Nm)
With Sheets(Nm(i))
For j = 19 To 28
If Not Application.WorksheetFunction.IsError(.Cells(2, j)) Then
If .Cells(2, j) = "有" Then
Cells(Row, 36) = .Cells(6, j): Cells(Row, 37) = .Cells(8, j)
.Cells(2, j).Resize(3, 1).Copy
Cells(Row, 38).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
'Cells(1, col).Resize(7, 1) = .Cells(2, j).Resize(7, 1).Value
Row = Row + 1
End If
End If
Next
End With
Next
End Sub
当1~100个工作表时,如果运行时间长,可以在代码开始位置加:
Application.ScreenUpdating = False
结束位置再改回:Application.ScreenUpdating = True
With
Worksheets("查询")
.Cells(b,
1).NumberFormat
=
"m月d日"
'格式化日期
.Cells(b,
1).Value
=
riqi
.Cells(b,
2).NumberFormat
=
"@"
'格式化文本
.Cells(b,
2).Value
=
danhao
.Cells(b,
3).Value
=
mingchen
.Cells(b,
4).Value
=
danwei
.Cells(b,
5).Value
=
shuliang
.Cells(b,
6).Value
=
danjia
.Cells(b,
7).Value
=
jine
.Cells(b,
8).Value
=
kehu
.Cells(b,
9).Value
=
beizhu
End
With
其实这个查询的代码可以优化一下,用高级筛选的话,只要一句代码就好了。
选中2个不连续的单元格,先选中的复制到dr,后选中的复制到dr下一行是这样吗?
Selection.Areas(1).Copy
dr.Value
=
Selection.Areas(1).Value(原来那句会直接选中dr,也就不存在Selection.Areas(2))
Selection.Areas(2).Copy
dr.Offset(1,
0).PasteSpecial
Paste:=xlPasteValues