简体   繁体   English

Excel VBA在当前单元格之前插入单元格

[英]Excel VBA insert cell before current cell

I'm trying to find the right way to insert a cell before the current cell. 我正在尝试找到在当前单元格之前插入单元格的正确方法。

I'm looping through a number of cells in a row - when it finds the right value I want it shifted to the right. 我在一行中遍历多个单元格-当它找到正确的值时,我希望它向右移动。

I've tried this code: (located post code has a value pointing to the offset relative to the starting active cell) 我尝试了以下代码:(定位的邮政编码具有指向相对于起始活动单元格的偏移量的值)

Dim shiftrng as Range

For ctr = locatedPostCodeIndex To 4

    Set shiftrng = Range(cll.Offset(0, locatedPostCodeIndex)).Select
    shiftrng.Insert.xlShiftToRight

Next

I get an error: Method range of object _global failed 我收到一个错误: Method range of object _global failed

What is the correct way to insert a cell before a cell? 在单元格之前插入单元格的正确方法是什么?

在将变量设置为范围对象时,无需添加.Select ,并且不需要将单元格偏移量目标包装在Range(...)

Set shiftrng = cll.Offset(0, locatedPostCodeIndex)

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

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