在删除vba中的单元格时右移单元格

删除内容后,我需要将单元格移到右侧。 这个选项不是由excel给出的,我只能得到4个select: – 左移位单元格 – 移位单元格 – 整行 – 整列

最后,我希望在我的VBA代码中有这样的结果:

Selection.Delete Shift:=xlToRight 

我改变了

 Selection.Delete Shift:=xlToLeft 

提前谢谢您对Brgds Patrick的任何帮助

我终于结束了这个,它工作得很好:

 Sub ShiftRight() Selection.End(xlToRight).Select numcol = ActiveCell.Column numcol2 = numcol numcol = (numcol - lngColNumber) + 5 strcolletter = Split(Cells(1, numcol - 1).Address, "$")(1) strcolletter2 = Split(Cells(1, numcol2).Address, "$")(1) Range(Myrange).Select Selection.Cut Destination:=Columns(strcolletter & ":" & strcolletter2) End Sub 

我需要使用在顶层定义的variables,因为我需要移动到右侧的范围永远不会有相同数量的列。

我希望今后也能帮助别人。 Thx为所有您​​的答复

我同意这个意见不应该太复杂,但我认为这是值得回答的。 这将保留已移位单元格的任何格式(范围左侧的单元格),但会清除已删除单元格的格式和内容。

 Sub DelRight() Dim firstColumn, lastColumn, firstRow, lastRow, nRows, nCols, colsToShift As Long Dim sheet As Worksheet Dim rangeToCopy, rangeToReplace, rangeToDelete As Range Set sheet = ActiveSheet firstColumn = Selection.Column nCols = Selection.Columns.Count nRows = Selection.Rows.Count lastColumn = firstColumn + nCols - 1 colsToShift = firstColumn - 1 firstRow = Selection.Row lastRow = firstRow + nRows - 1 ' Shift cells to left of the range to the right hand end of the range With sheet If firstColumn > 1 Then Set rangeToCopy = .Range(.Cells(firstRow, 1), .Cells(lastRow, colsToShift)) Set rangeToReplace = .Range(.Cells(firstRow, lastColumn - colsToShift + 1), .Cells(lastRow, lastColumn)) rangeToCopy.Copy destination:=rangeToReplace End If ' Delete cells to the left of the shifted cells Set rangeToDelete = .Range(.Cells(firstRow, 1), .Cells(lastRow, lastColumn - colsToShift)) rangeToDelete.ClearContents rangeToDelete.ClearFormats End With End Sub