Vba 在列中查找值,并使用if语句将单元格更改为左侧
此VBA脚本应获取单元格A37中的值,并检查其是否位于另一工作表的C列中。找到数字后,左侧的列应更改为0。如果已为0,则会有一个消息框通知用户,如果该号码不存在,则会有另一个消息框通知用户。Vba 在列中查找值,并使用if语句将单元格更改为左侧,vba,if-statement,Vba,If Statement,此VBA脚本应获取单元格A37中的值,并检查其是否位于另一工作表的C列中。找到数字后,左侧的列应更改为0。如果已为0,则会有一个消息框通知用户,如果该号码不存在,则会有另一个消息框通知用户。 这是我用来完成这项任务的VBA。但是,每次我尝试运行它时,都会出现一个“编译错误:下一步不带For” 更新现在的问题是,在执行Active.cell偏移之前,我需要激活fcell所在的单元格 Sub Cancelled() Dim x As Long Dim regRange As Range Dim f
这是我用来完成这项任务的VBA。但是,每次我尝试运行它时,都会出现一个“编译错误:下一步不带For” 更新现在的问题是,在执行Active.cell偏移之前,我需要激活fcell所在的单元格
Sub Cancelled()
Dim x As Long
Dim regRange As Range
Dim fcell As Range
x = ThisWorkbook.Sheets("Welcome").Range("A37").Value
Set regRange = ThisWorkbook.Sheets("Registration").Range("C:C")
For Each fcell In regRange.Cells
If fcell.Value = x Then
ActiveCell.Offset(0, -1).Select
If ActiveCell.Value = 1 Then
ActiveCell.Value = 0
MsgBox "Changed to zero"
Exit Sub
Else
MsgBox "That registration number is already cancelled"
Exit Sub
End If
End If
Next fcell
MsgBox "That number does not exist"
End Sub
编辑新问题:无需使用
Select
和ActiveCell
If fcell.Value = x Then
If fcell.Offset(0,-1).Value = 1 Then
fcell.Offset(0,-1).Value = 0
...
编辑2:进一步的建议:您也可以使用Range.Find
方法。如果未找到任何内容,这将引发错误,因此您必须捕获:
On Error Resume Next 'If an error occurs, continue with the next line
Set fcell = regRange.Find(x)
On Error GoTo 0 'disable the error handler
If fcell Is Nothing Then 'If Find failed
MsgBox "That number does not exist"
Else
'do your stuff with fcell here
End If
编辑新问题:无需使用
Select
和ActiveCell
If fcell.Value = x Then
If fcell.Offset(0,-1).Value = 1 Then
fcell.Offset(0,-1).Value = 0
...
编辑2:进一步的建议:您也可以使用Range.Find
方法。如果未找到任何内容,这将引发错误,因此您必须捕获:
On Error Resume Next 'If an error occurs, continue with the next line
Set fcell = regRange.Find(x)
On Error GoTo 0 'disable the error handler
If fcell Is Nothing Then 'If Find failed
MsgBox "That number does not exist"
Else
'do your stuff with fcell here
End If
希望现在回答您的问题还不算太晚:
Sub Cancelled()
Dim x As Long
Dim regRange As Range
Dim fcell As Range
x = ThisWorkbook.Sheets("Welcome").Range("A7").Value
Set regRange = ThisWorkbook.Sheets("Registration").Range("C:C")
For Each fcell In regRange.Cells
If fcell.Value = x Then
If fcell.Offset(0, -1).Value = 1 Then
fcell.Offset(0, -1).Value = 0
MsgBox "Changed to zero"
Else
MsgBox "That registration number is already cancelled"
End If
Exit Sub
End If
Next fcell
MsgBox "That number does not exist"
End Sub
而不是
Set regRange = ThisWorkbook.Sheets("Registration").Range("C:C")
最好在列C
中获取最后一行,然后将范围设置为:
Dim lastRow As Long
lastRow = ThisWorkbook.Sheets("Registration").Cells(Rows.Count, "C").End(xlUp).Row
Set regRange = ThisWorkbook.Sheets("Registration").Range("C1:C" & lastRow)
希望现在回答您的问题还不算太晚:
Sub Cancelled()
Dim x As Long
Dim regRange As Range
Dim fcell As Range
x = ThisWorkbook.Sheets("Welcome").Range("A7").Value
Set regRange = ThisWorkbook.Sheets("Registration").Range("C:C")
For Each fcell In regRange.Cells
If fcell.Value = x Then
If fcell.Offset(0, -1).Value = 1 Then
fcell.Offset(0, -1).Value = 0
MsgBox "Changed to zero"
Else
MsgBox "That registration number is already cancelled"
End If
Exit Sub
End If
Next fcell
MsgBox "That number does not exist"
End Sub
而不是
Set regRange = ThisWorkbook.Sheets("Registration").Range("C:C")
最好在列C
中获取最后一行,然后将范围设置为:
Dim lastRow As Long
lastRow = ThisWorkbook.Sheets("Registration").Cells(Rows.Count, "C").End(xlUp).Row
Set regRange = ThisWorkbook.Sheets("Registration").Range("C1:C" & lastRow)
谢谢…我尝试了不同的东西,如果没有写进这个剧本的话,结局会怎样。我仍然收到一个错误1004:应用程序定义的或对象定义的错误。请用新的错误消息和它在屏幕上出现的行编辑您的问题…稍微有点过头了我尝试了不同的事情,如果没有进入此脚本,则结束。我仍然收到一个错误1004:应用程序定义的错误或对象定义的错误。请使用新的错误消息及其显示的行编辑您的问题