下一个没有为Vba编译错误 [英] Next without For Vba Compile error
本文介绍了下一个没有为Vba编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有我的代码不编译,错误信息是
There is my code that doesn't compile, the error message is
下一步不用
我可以做什么?
Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
N = Range(Rows.Count, "A2").End(xlUp).Select
M = Range("B2").End(xlUp).Select
For i = 1 To N
If Cells(i, "A").Value = "2015 Xor 2011" Then
Cells(j, "B").Value = "blue"
Else
If Cells(i, "A").Value = "2001 Xor 2003" Then
Cells(j, "B").Value = "green"
Else
If Cells(i, "A").Value = "2014 Xor 2006" Then
Cells(j, "B").Value = "red"
j = j + 1
End If
Next
End Sub
推荐答案
你的代码中有很多麻烦,甚至混淆了那些回答,所以这是它:
many mistackes in your code, even confusing those who answered, so this is it:
Option Explicit 'this avoids forgetting declaring variable, i put it on top of each code
Sub CommandButton1_Click()
Dim i&, j&, n& 'as Long, not integer
'declare and assign sheets:
Dim Ws As Worksheet
Set Ws = ActiveSheet ' you might want to correctely name the sheet, exept if the sub has to be dynamic with anysheet where you are...
'you missed declaring N !! so like you wrote it it looks like a range and 'to n' will mean to N.value
n = Ws.Range(Ws.Rows.Count, "A").End(xlUp).Row ' garbage => .Select
'supposing n is supposed to give the last line with something inside in column ("A"=1)
'garbage and not declared (and why select, again!?) => M = Range("B2").End(xlUp).Select
'j=1 'if ommited, on first loop it tries to write at row 0 (=>errpr)
With Ws
For i = 1 To n
Select Case .Cells(i, "A").Value2 'like this the code is 3 times faster (and with arrays even faster , but no need to complicate...) 'note i use .value2 and not .value, faster but not working with dates or time formating of cells.
Case "2015 Xor 2011": .Cells(i, 2).Value2 = "blue"
Case "2001 Xor 2003": .Cells(i, 2).Value2 = "green"
Case "2014 Xor 2006": .Cells(i, 2).Value2 = "red"
End Select
'j = j + 1
Next i
End With
End Sub
此代码只读取行i上的值
this code reads only once the value at the row i
编辑:我只是注意到:j =我在任何时候都这么麻烦?
edit : i just noticed: j=i at all times so why bother ?
这篇关于下一个没有为Vba编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文