作者zzatr (我还小)
看板Office
标题[算表] VBA On Error Resume Next
时间Mon Dec 18 20:52:49 2017
软体:excel
版本:2016
大家好 目前看书自学VBA中
书本中范例要把其中一栏的名称,逐一新增成工作表名称并不重复
Sub test()
Dim i As Integer, sht As Worksheet
Set sht = Worksheets("工作表21")
i = 2
Do While sht.Cells(i, "C") <> ""
On Error Resume Next
If Worksheets(sht.Cells(i, "C").Value) Is Nothing Then
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = sht.Cells(i, "C").Value
End If
i = i + 1
Loop
End Sub
其中加入了On Error Resume Next,估狗了一下这个指令是忽略错误继续执行
当我拿掉这行指令事就无法执行程序了,代表
If Worksheets(sht.Cells(i, "C").Value) Is Nothing Then
有问题吗?可是我看不出问题在哪,想问为啥差这个指令差这麽多,还有上面那行错在哪
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.143.15.249
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Office/M.1513601571.A.79D.html
1F:→ newton41: Cells(r,c) 里面放的是整数,不能用”C”。 12/18 23:28
2F:→ newton41: 抱歉,我错了,可以用”C”。 12/18 23:33
3F:→ newton41: 如果少了那句,当worksheets is nothing 的时候就有Erro 12/18 23:38
4F:→ newton41: r,因为他不知道那是什麽,所以要加入那句让错误发生时继 12/18 23:38
5F:→ newton41: 续执行。 12/18 23:38
6F:→ zzatr: 可是 worksheets is nothing不就是表示当没有这张工作表时 12/19 09:45
7F:→ zzatr: 请excel增加一张吗? 我不知道这指令错在哪XD 12/19 09:46
8F:→ soyoso: on error resume nexte後因worksheeets无该张工作表会回传 12/19 09:57
9F:→ soyoso: 值nothing如不加就产生错误讯息 12/19 09:57
10F:→ soyoso: 回传值方面可於on error resume next下设个变数来set该 12/19 10:12
11F:→ soyoso: worksheet()的部分,并以区域变数视窗或快速监看,看该变 12/19 10:14
12F:→ soyoso: 数(值)的部分 12/19 10:15
※ 编辑: zzatr (218.173.0.192), 12/21/2017 16:16:50