gpt4 book ai didi

vba - 将每第 3 个单元格中的值向右移动 2 个单元格

转载 作者:行者123 更新时间:2023-12-01 05:23:10 25 4
gpt4 key购买 nike

从 C7 开始,我想将 C7 中的值向右移动两个单元格,C7 -> E7 和 F7 -> H7,等等。我必须这样做直到列结束

我的代码:

Sub MoveCells2ToTheRight(specifiedWorksheet)

Dim lastCol As Long
With specifiedWorksheet
lastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
' from column 3 to end of used columns in worksheet
For i = 3 To lastCol
If Not IsEmpty(.Cells(7, i)) Then
.Cells(7, i).Cut
Sheets(specifiedWorksheet).Range(.Cells(7, i + 2)).Select
ActiveSheet.Paste
i = i - 1
lastCol = lastCol - 1
Else
Exit For
End If
Next i
End With

End Sub

最佳答案

除了剪切和粘贴,您还可以插入空白单元格并向右移动,如:Range("C7").Resize(ColumnSize:=2).Insert Shift:=xlToRight 如果有中间没有数据。

Sub MoveCells2ToTheRight(specifiedWorksheet As Worksheet)

Dim lastCol As Long
With specifiedWorksheet
lastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
' from column 3 to end of used columns in worksheet
For i = 3 To lastCol
If Not IsEmpty(.Cells(7, i)) Then
.Cells(7, i).Resize(ColumnSize:=2).Insert Shift:=xlToRight
i = i - 1
lastCol = lastCol - 1
Else
Exit For
End If
Next i
End With

End Sub

注意我建议指定 pecifiedWorksheet As Worksheet

关于vba - 将每第 3 个单元格中的值向右移动 2 个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50683125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com