“Object not found”, 移动两个 Excel 单元格对角线。

huangapple go评论99阅读模式
英文:

"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)

我认为你想要检查范围是否为空?

还有几点需要注意:

  1. Index 是只读属性,你应该使用另一个变量。
  2. ToString 是在 VB.Net 中使用的,而不是 VBA。
  3. 字符串连接时应该使用 & 而不是 +
  4. 避免使用 .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 + &quot;:&quot; + ColTwo + Index.ToString) 

I believe you want to check if the range is empty?

Also few other things...

  1. Index is a ReadOnly property. Use another variable.
  2. ToString is in VB.Net. Not in VBA.
  3. For string concatenation use &amp; and not +.
  4. 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
    
    &#39;~~&gt; Change this to the relevant worksheet
    Set ws = Sheet1

    For i = 1 To 1000
        &#39;~~&gt; This the range that you want to work with
        Set aCell = ws.Range(&quot;A&quot; &amp; i &amp; &quot;:B&quot; &amp; i)
        
        &#39;~~&gt; Check if the range is not empty
        If Application.WorksheetFunction.CountA(aCell) &lt;&gt; 0 Then
            &#39;~~&gt; Cut and paste at the same time
            aCell.Offset(1).Cut Destination:=ws.Range(&quot;C&quot; &amp; i)
            i = i + 1
        End If
    Next
End Sub

Screenshot

Before

“Object not found”, 移动两个 Excel 单元格对角线。

After

“Object not found”, 移动两个 Excel 单元格对角线。

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

huangapple
  • 本文由 发表于 2023年6月9日 13:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定