Excel 在vba中删除单元格时向右移动单元格
删除内容后,我需要将单元格向右移动。 excel没有提供此选项,我只有4个选项: -向左移动单元格 -上移单元格 -整排 -整列 最后,我希望在我的VBA代码中包含以下内容:Excel 在vba中删除单元格时向右移动单元格,excel,vba,shift,Excel,Vba,Shift,删除内容后,我需要将单元格向右移动。 excel没有提供此选项,我只有4个选项: -向左移动单元格 -上移单元格 -整排 -整列 最后,我希望在我的VBA代码中包含以下内容: Selection.Delete Shift:=xlToRight 我从 Selection.Delete Shift:=xlToLeft 提前感谢您在这方面的帮助 Brgds 帕特里克 我最终得到了这个,它运行得非常好: Sub ShiftRight() Selection.End(xlToRight).Selec
Selection.Delete Shift:=xlToRight
我从
Selection.Delete Shift:=xlToLeft
提前感谢您在这方面的帮助
Brgds
帕特里克
我最终得到了这个,它运行得非常好:
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
我需要使用在顶层定义的变量,因为我需要向右移动的范围永远不会有相同数量的列
我希望这也能帮助其他人。
谢谢大家的回复我同意这些评论,它不应该太复杂,但我认为值得一个回答。这将保留已移动单元格(范围左侧的单元格)的任何格式,但会清除已删除单元格的格式和内容
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
我同意这些评论,它不应该太复杂,但我认为它值得一个答案。这将保留已移动单元格(范围左侧的单元格)的任何格式,但会清除已删除单元格的格式和内容
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
我更喜欢这种简单的方式:
Sub DELETE_MOVE_TO_RIGHT()
Dim firstcolumn As Integer
Dim lastcolumn As Integer
Dim firstrow As Integer
Dim lastrow As Long
Dim i As Integer
Dim j As Integer
Dim nrows As Long
Dim ncols As Integer
ncols = Selection.Columns.Count
nrows = Selection.Rows.Count
firstcolumn = Selection.Column
lastcolumn = firstcolumn + ncols - 1
firstrow = Selection.Row
lastrow = firstrow + nrows - 1
Range(Cells(firstrow, firstcolumn), Cells(lastrow, lastcolumn)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlToLeft
For j = lastcolumn To firstcolumn + 1 Step -1
Range(Cells(firstrow, firstcolumn), Cells(lastrow, firstcolumn)).Cut
Range(Cells(firstrow, j + 1), Cells(lastrow, j + 1)).Insert Shift:=xlToRight
Next j
End Sub
我更喜欢这种简单的方式:
Sub DELETE_MOVE_TO_RIGHT()
Dim firstcolumn As Integer
Dim lastcolumn As Integer
Dim firstrow As Integer
Dim lastrow As Long
Dim i As Integer
Dim j As Integer
Dim nrows As Long
Dim ncols As Integer
ncols = Selection.Columns.Count
nrows = Selection.Rows.Count
firstcolumn = Selection.Column
lastcolumn = firstcolumn + ncols - 1
firstrow = Selection.Row
lastrow = firstrow + nrows - 1
Range(Cells(firstrow, firstcolumn), Cells(lastrow, lastcolumn)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlToLeft
For j = lastcolumn To firstcolumn + 1 Step -1
Range(Cells(firstrow, firstcolumn), Cells(lastrow, firstcolumn)).Cut
Range(Cells(firstrow, j + 1), Cells(lastrow, j + 1)).Insert Shift:=xlToRight
Next j
End Sub
Excel不能“右移”的原因是删除会留下一个需要填补的“漏洞”
xlToRight
会留下一个更大的“洞”。只需将所有内容复制到右边,并清除单元格内容即可。好的,谢谢您的澄清。我即将编写代码来实现这一点。Excel之所以不能“右移”,是因为删除会留下一个需要填补的“漏洞”xlToRight
会留下一个更大的“洞”。只需将所有内容复制到右边,并清除单元格内容即可。好的,谢谢您的澄清。我即将编写代码来实现这一点。