我的代码在根据输入的内容移动到下一列时效果很好,只有当它到达第13列时我需要它从最后一行开始并继续工作。有人能帮助我吗?
Dim lastrow As Long
If Target.Column > 2 Then
lastrow = Range("A17").Value
'lastrow = Me.Cells(Me.Rows.Count, "B").End(xlDown).Row
If Target.Column Mod 2 = 1 Then
If Target.Row >= lastrow Then
Me.Cells(Target.Row, Target.Column + 1).Select
Else
Me.Cells(Target.Row + 1, Target.Column).Select
End If
Else
If Target.Row = 3 Then
Me.Cells(Target.Row, Target.Column + 1).Select
Else
Me.Cells(Target.Row - 1, Target.Column).Select
End If
End If
End If
答案 0 :(得分:1)
似乎问题出在你的陈述中:
If Target.Column Mod 2 = 1 Then
我相信你正在为excel vba“\”寻找模数运算符。试试这个:
If (Target.Column\2) = 1 Then
根据您的评论,请尝试为第13列添加此代码:
If Target.Column = 13 Then
'<your code here'
这应该可以解决问题。
此外,您可能想要使用偏移功能。语法是.Range(“”)。(RowOffset,ColumnOffset)。这允许您从固定位置偏移。
请参阅以下完整编辑的代码:
If Target.Column > 2 Then
lastrow = Range("A17").Value
'lastrow = Me.Cells(Me.Rows.Count, "B").End(xlDown).Row
If Target.Column Mod 2 = 1 Then
If Target.Row >= lastrow Then
Me.Cells(Target.Row, Target.Column + 1).Select
Else
Me.Cells(Target.Row + 1, Target.Column).Select
End If
ElseIf Target.Column = 13 Then
' I am assuming the code below is what you need
' this would start at your last row and work it's way up
Me.Cells(lastrow, Target.Column).Select
For i = lastrow to 1 Step - 1 ' replace 1 with however far up you want it to go
Me.Cells(i, Target.Column).Select
Next
Else
If Target.Row = 3 Then
Me.Cells(Target.Row, Target.Column + 1).Select
Else
Me.Cells(Target.Row - 1, Target.Column).Select
End If
End If
End If