英文:
"Object not found", move two excel cells diagonally
问题
I have an excel sheet like so:
| A----| B-------| C----| D-------|
| Todd | Sentry |
| Gary | Alquist |
I want a VBA
script that essentially moves the second row diagonally like so:
| A----| B-------| C----| D-------|
| Todd | Sentry | Gary | Alquist |
And I want it to do this for every occurrence there are two rows stacked on top of each other with content, moving the second row diagonally beside the first.
But I keep getting an Object not found
error in the For
loop.
Sub Macro1()
' Macro1 Macro
ColOne = "A"
ColTwo = "B"
ColThree = "C"
For Index = 1 To 1000
Below = Index + 1
If IsEmpty(ColOne + Index.ToString + ":" + ColTwo + Index.ToString) Then
Else
Range(ColOne + Below.ToString + ":" + ColTwo + Below.ToString).Select
Selection.Cut
Range(ColThree + Index.ToString).Select
ActiveSheet.Paste
Index = Index + 2
End If
Next
End Sub
英文:
I have an excel sheet like so:
| A----| B-------| C----| D-------|
| Todd | Sentry |
| Gary | Alquist |
I want a VBA
script that essentially moves the second row diagonally like so:
| A----| B-------| C----| D-------|
| Todd | Sentry | Gary | Alquist |
And I want it to do this for every occurrence there are two rows stacked on top of each other with content, moving the second row diagonally beside the first.
But I keep getting an Object not found
error in the For
loop.
Sub Macro1()
' Macro1 Macro
ColOne = "A"
ColTwo = "B"
ColThree = "C"
For Index = 1 To 1000
Below = Index + 1
If IsEmpty(ColOne + Index.ToString + ":" + ColTwo + Index.ToString) Then
Else
Range(ColOne + Below.ToString + ":" + ColTwo + Below.ToString).Select
Selection.Cut
Range(ColThree + Index.ToString).Select
ActiveSheet.Paste
Index = Index + 2
End If
Next
End Sub
答案1
得分: 1
你在for循环中一直收到“对象未找到”错误是因为这行代码:
If IsEmpty(ColOne + Index.ToString + ":" + ColTwo + Index.ToString)
我认为你想要检查范围是否为空?
还有几点需要注意:
Index
是只读属性,你应该使用另一个变量。ToString
是在 VB.Net 中使用的,而不是 VBA。- 字符串连接时应该使用
&
而不是+
。 - 避免使用
.Select/.ActiveSheet
,而是直接操作对象。
以下是你的代码示例:
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim i As Long
Dim aCell As Range
' 更改为相关的工作表
Set ws = Sheet1
For i = 1 To 1000
' 这是你想要处理的范围
Set aCell = ws.Range("A" & i & ":B" & i)
' 检查范围是否不为空
If Application.WorksheetFunction.CountA(aCell) <> 0 Then
' 剪切并粘贴同时进行
aCell.Offset(1).Cut Destination:=ws.Range("C" & i)
i = i + 1
End If
Next
End Sub
如果你不想在上述循环中硬编码1000
,可以查找最后一个已使用的单元格并将其用作循环的上限。详情请参考这里。
英文:
>I keep getting an "Object not found" error in the for loop:
You are getting an error because of the line
If IsEmpty(ColOne + Index.ToString + ":" + ColTwo + Index.ToString)
I believe you want to check if the range is empty?
Also few other things...
Index
is a ReadOnly property. Use another variable.ToString
is in VB.Net. Not in VBA.- For string concatenation use
&
and not+
. - Avoid the use of .Select/.ActiveSheet etc.. Work with Objects.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim i As Long
Dim aCell As Range
'~~> Change this to the relevant worksheet
Set ws = Sheet1
For i = 1 To 1000
'~~> This the range that you want to work with
Set aCell = ws.Range("A" & i & ":B" & i)
'~~> Check if the range is not empty
If Application.WorksheetFunction.CountA(aCell) <> 0 Then
'~~> Cut and paste at the same time
aCell.Offset(1).Cut Destination:=ws.Range("C" & i)
i = i + 1
End If
Next
End Sub
Screenshot
Before
After
PS: If you do not want to hardcode 1000
in the above loop then you can find the last row and use that as show HERE
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论