![](/img/trans.png)
[英]How do I read and loop through an excel file in python in the format [Row1, Col1] [Row2, Col2] [Row3, Col3]
[英]How do I skip some lines of code based on whether I have selected a cell in Row3 or not?
我正在尝试创建一个电子表格,用户可以在其中选择任何单元格,然后运行此宏。 如果该单元格不在第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:
在VBA中,您可以通过多种方式编写IF
语句:1.全部写在一行上,或者2.使用Else
组件,或者3.使用ElseIf
...等。
当您编写这样的IF
语句时:
If A=True Then B
如果没有Else
组件,则VBA不需要End IF
语句。 像这样使用IF
时需要它:
IF A=True Then
Do this
Else
Do Something else
End 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
从原则上讲,您还应该避免。选择不需要的位置。
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:
Sub Horsten()
'选择和第三行没有什么意义吗?
If Intersect(Selection, Rows(3)) Is Nothing Then
'然后剪切并粘贴(但不更改选择)
Selection.EntireRow.Cut
Rows(3).Insert Shift:=xlDown
'然后根据需要调整位置
ActiveWindow.SmallScroll Up:=84
Cells(3, 1).Select
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.