简体   繁体   English

Excel VBA插入复制的单元格并向下移动单元格ERROR

[英]Excel VBA Insert copied cells and shift cells down ERROR

Complete amateur here. 在这里完成业余。 I've been puzzling over this for hours, and I can't find anything to help me in any other thread. 数小时来,我一直对此感到困惑,在其他任何线程中,我找不到任何可以帮助我的东西。 At my wit's end so sorry if this has been asked elsewhere. 我的机智很抱歉,是否有人在其他地方问过这个问题。

I'm trying to create a ridiculously simple macro to do the following: 我正在尝试创建一个荒谬的简单宏来执行以下操作:

Go to Sheet2,
Select C6:C10
Copy
Go to Sheet3
Insert copied cells in B2 and shift the other cells down.

I did this just by recording the macro, but each time I do it, I get different errors. 我只是通过记录宏来做到这一点,但是每次这样做,都会遇到不同的错误。 The error I currently have is 'Insert Method of Range Class Failed', but sometimes the error pops up at 'Selection.Copy'. 我目前遇到的错误是“ Insert Method of Range Class Failed”,但有时错误会在“ Selection.Copy”中弹出。 This is the code I have: 这是我的代码:

Sub InsertCellsShitDown()
'
' InsertCellsShitDown Macro
'

'
Sheets("Booking Sheet").Select
Range("C6:C10").Select
Selection.Copy
Sheets("Sheet1").Select
Range("B2").Select
Selection.Insert Shift:=xlDown
End Sub

Any help would be hugely appreciated. 任何帮助将不胜感激。

Sub InsertCellsShiftDown()
'
' InsertCellsShitDown Macro
Dim bookingWS As Worksheet, mainWS As Worksheet
Dim copyRng As Range

Set bookingWS = Sheets("Booking Sheet")
Set mainWS = Sheets("Sheet1")
Set copyRng = bookingWS.Range("C6:C10")

mainWS.Range("B2:B" & copyRng.Rows.Count + 1).Insert Shift:=xlDown
copyRng.Copy mainWS.Range("B2")
End Sub

How does this work? 这是如何运作的? I assume you wanted to insert 5 rows, so from B2:B7 , then put the data. 我假设您想插入5行,因此从B2:B7 ,然后放入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM