复制、插入和删除给定的单元格

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

Copy, insert and delete a given cell

问题

我想在这里做的是选定一个给定的单元格,复制其值并删除该单元格,然后将其值粘贴到新插入的单元格。用户提供关于删除哪个单元格和在哪个单元格插入新单元格的输入。

以下是我的代码:

Sub Test()
   Dim rngDeleteCell As Range
   Dim rngInsertCell As Range

   On Error Resume Next
   Set rngDeleteCell = Application.InputBox( _
          "选择要删除的单元格", _
          Type:=8)
   If rngDeleteCell Is Nothing Then Exit Sub
   Set rngInsertCell = Application.InputBox( _
          "选择要在哪个单元格上插入新单元格", _
          Type:=8)
   If rngInsertCell Is Nothing Then Exit Sub

   On Error GoTo 0

   With ThisWorkbook.Sheets("Sheet1")
      .Range(rngInsertCell).Insert xlShiftDown
      .Range(rngDeleteCell).Copy Destination:=Range(rngInsertCell)
      .Range(rngDeleteCell).Delete Shift:=xlShiftUp
   End With
End Sub

然而,当我运行代码时,我收到运行时错误:“1004”:应用程序定义或对象定义的错误。

英文:

What I want to do here is taking a given cell, copying its value and deleting the cell after pasting its value to the newly inserted cell. User gives input about which cell to delete and which cell to insert the new cell upon.

Here is my code:

Sub Test() 
   Dim rngDeleteCell As Range
   Dim rngInsertCell As Range
   
   On Error Resume Next
   Set rngDeleteCell = Application. InputBox( _
          "Select the cell to delete", _
          Type:=8)
   If rngDeleteCell Is Nothing Then Exit Sub
   Set rngInsertCell = Application. InputBox( _
          "Select the cell to insert the new cell upon", _
          Type:=8)
   If rngInsertCell Is Nothing Then Exit Sub 

On Error GoTo 0

With ThisWorkbook.Sheets("Sheet1") 
 .Range(rngInsertCell).Insert xlShiftDown
 .Range(rngDeleteCell).Copy Destination:=Range(rngInsertCell) 
 .Range(rngDeleteCell).Delete Shift:=xlShiftUp
End With
End Sub

However, when I run the code I receive Run-time error:'1004': Application-defined or object-defined error.

答案1

得分: 0

以下是翻译好的部分:

"不确定这是否是您考虑中的期望效果,似乎将所有值向上移动有点奇怪:"

"值被向上移动,并保留一个空白空间,将值保留在其下原来的位置。"

"您使用的代码如下:"

"如果您想要的是这种奇怪的结果,那么很好(?); 如果不是,请澄清它应该是什么样子的。如果 Ike 想要获得 .Range(xx) 的澄清归属权,我不介意 :)"

英文:

Not sure this is the desired effect you had in mind, seems a bit odd shifting all the values up like that:

复制、插入和删除给定的单元格

The values get shifted upwards with an empty space keeping the values below it where they were..

Your code adjusted used:

Sub TestInsertDelete()
   Dim rngDeleteCell As Range
   Dim rngInsertCell As Range
   
   On Error Resume Next
   Set rngDeleteCell = Application.InputBox( _
          "Select the cell to delete", _
          Type:=8)
   If rngDeleteCell Is Nothing Then Exit Sub
   Set rngInsertCell = Application.InputBox( _
          "Select the cell to insert the new cell upon", _
          Type:=8)
   If rngInsertCell Is Nothing Then Exit Sub

On Error GoTo 0

 rngInsertCell.Insert xlShiftDown
 rngDeleteCell.Copy Destination:=rngInsertCell
 rngDeleteCell.Delete Shift:=xlShiftUp
End Sub

If this weird result is what you were going for, then good(?), if not, please clarify how it was meant to be. If Ike wants credit for clarifying the .Range(xx) I don't mind 复制、插入和删除给定的单元格

答案2

得分: 0

我通过调整Notus_Panda的代码来实现我的目标。更明确地说,我想在一个迭代数据库中切换单元格的值。列号是可调整的。

Sub TestInsertDelete()
    Dim rngDeleteCell As Range
    Dim rngInsertCell As Range

    On Error Resume Next
    Set rngDeleteCell = Application.InputBox( _
           "选择要删除的单元格", _
           Type:=8)
    If rngDeleteCell Is Nothing Then Exit Sub
    Set rngInsertCell = Application.InputBox( _
           "选择要插入新单元格的位置", _
           Type:=8)
    If rngInsertCell Is Nothing Then Exit Sub

    On Error GoTo 0

    rngInsertCell.Insert xlShiftDown
    Cells(rngInsertCell.Row - 1, 2).Value = rngDeleteCell.Value
    rngDeleteCell.ClearContents
    rngDeleteCell.Value = Cells(rngInsertCell.Row, 2).Value
    rngInsertCell.Delete Shift:=xlShiftUp

End Sub

希望这对您有所帮助。

英文:

I achieved my goal by adapting Notus_Panda's code. To be more clear, I wanted to switch the cell values in an iterative database. Column number is adjustable.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Sub TestInsertDelete()
   Dim rngDeleteCell As Range
   Dim rngInsertCell As Range
   
   On Error Resume Next
   Set rngDeleteCell = Application.InputBox( _
          &quot;Select the cell to delete&quot;, _
          Type:=8)
   If rngDeleteCell Is Nothing Then Exit Sub
   Set rngInsertCell = Application.InputBox( _
          &quot;Select the cell to insert the new cell upon&quot;, _
          Type:=8)
   If rngInsertCell Is Nothing Then Exit Sub

On Error GoTo 0

 rngInsertCell.Insert xlShiftDown
 Cells(rngInsertCell.Row - 1, 2).Value = rngDeleteCell.Value
 rngDeleteCell.ClearContents
 rngDeleteCell.Value = Cells(rngInsertCell.Row, 2).Value
 rngInsertCell.Delete Shift:=xlShiftUp
 
End Sub

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 19:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661530.html
匿名

发表评论

匿名网友

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

确定