vba 如何根据我是否在 Row3 中选择了一个单元格来跳过一些代码行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27868409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I skip some lines of code based on whether I have selected a cell in Row3 or not?
提问by James
I am trying to have a spreadsheet where the user selects any cell and then runs this macro. If the cell is not in Row 3 then select the entire row that that cell is in, cut and insert that row in Row 3. (this part works fine) If the selected cell is in Row 3 then I want to skip the cut and insert part of the code. This seems like it should be simple to do but I get the error: End if without block if, when I run this macro. Any help would be greatly appreciated. Thank you.
我正在尝试制作一个电子表格,用户可以在其中选择任何单元格,然后运行此宏。如果单元格不在第 3 行,则选择该单元格所在的整行,剪切并将该行插入第 3 行。(这部分工作正常)如果所选单元格在第 3 行,那么我想跳过剪切并插入部分代码。这看起来应该很简单,但我收到错误消息:当我运行此宏时,如果没有阻止,则结束。任何帮助将不胜感激。谢谢你。
Dim Row As String
ActiveCell.EntireRow.Select
strRow = Selection
If Selection = Rows("3:3") Then GoTo SkipToHere
End If
Selection.Cut
ActiveWindow.SmallScroll Down:=-84
Rows("3:3").Select
Selection.Insert Shift:=xlDown
Range("A3").Select
SkipToHere:
回答by Rusan Kax
In VBA, you can write IF
statements in multiple ways: 1. All on one line, or 2. With an Else
component, or 3. using ElseIf
... etc.
在 VBA 中,您可以IF
通过多种方式编写语句:1. 全部在一行上,或 2. 使用一个Else
组件,或 3. 使用ElseIf
...等。
When you write an IF
statement like this:
当你写这样的IF
语句时:
If A=True Then B
If A=True Then B
with no Else
component, then VBA does not require an End IF
statement. It is needed when you use IF
like this:
如果没有Else
组件,则 VBA 不需要End IF
语句。IF
像这样使用时需要它:
IF A=True Then
Do this
Else
Do Something else
End If
回答by D_Zab
Using GoTo in this context is probably not the best way to go about this. Instead I would just encase your first part in an If:
在这种情况下使用 GoTo 可能不是解决此问题的最佳方法。相反,我只是将您的第一部分包含在 If 中:
Dim strRow As String
ActiveCell.EntireRow.Select
strRow = Selection
If Selection.Row <> 3 Then
Selection.Cut
Rows("3:3").Insert Shift:=xlDown
Range("A3").Select
End If
You should also as a matter of principle avoid .Select where you do not need it.
原则上你也应该避免 .Select 你不需要的地方。
回答by Dirk Horsten
Sub Horsten()
' Do the selection and the third row have nothing in commen?
' 选区和第三行有什么共同点吗?
If Intersect(Selection, Rows(3)) Is Nothing Then
' Then cut and paste (but without changing the selection)
' 然后剪切和粘贴(但不改变选择)
Selection.EntireRow.Cut
Rows(3).Insert Shift:=xlDown
' Then adjust the positioning if you like
' 然后根据需要调整位置
ActiveWindow.SmallScroll Up:=84
Cells(3, 1).Select
End If
End Sub
回答by bilbo_strikes_back
Dim Row As String
If Activecell.Row = 3 Then GoTo SkipToHere
End If
ActiveCell.EntireRow.Select
Selection.Cut
ActiveWindow.SmallScroll Down:=-84
Rows("3:3").Select
Selection.Insert Shift:=xlDown
Range("A3").Select
SkipToHere: