我有一列有很多空白和条目。我要输入条目(忽略空格),然后将它们向右移一次并向下移两次,以替换内容。我觉得您会使用偏移功能,但是我不知道如何在VBA中编写此功能。我只使用offset作为公式。任何帮助,将不胜感激...
首先,您需要创建一个循环,循环遍历范围的所有值。创建循环的方法很多,但是这里有一个例子:
'find last row of range
lastrow = ActiveSheet.UsedRange.Rows.Count
'Loops through the values from 2 to the last row of range
For x=2 to lastrow
Next x
然后,我建议遍历该范围并使用IF函数检查每个单元格值是否符合您的条件:
'Checks for blank value in column A. If not blank
If Cells(x, 1).Value <> "" then
'Do Something
End IF
现在,为了复制新范围内的所有值,只需将旧单元格和新单元格的值设置为相等即可:
'Moves value from column A to column B and two cells down
Cells(x+2, 2).Value = Cells(x, 1).Value
总之,您的代码如下所示:
Sub MoveValue ()
lastrow = ActiveSheet.UsedRange.Rows.Count
For x=2 to lastrow
If Cells(x, 1).Value <> "" then
Cells(x+2, 2).Value = Cells(x, 1).Value
End IF
Next x
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句