将值复制到右侧的空单元格

时间:2014-12-03 16:33:46

标签: vba excel-vba excel

我正在寻找类似于将值复制到ASAP Utilities下面的空单元格而是向右复制的内容。因此,当我选择一行时,它会将填充单元格中的值复制到右侧的空单元格,直到有一个填充的单元格,然后它将开始复制该单元格,依此类推。

这里有一个代码示例,除非我选择整行:

Sub CopyToRight()

On Error GoTo Err_Handler

Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=RC[-1]"

Exit_This_Sub:
Exit Sub
Err_Handler:
Resume Exit_This_Sub

End Sub

Example file

2 个答案:

答案 0 :(得分:0)

这将做你想要的:

Sub CopyToRight()

Dim CurCol As Long, CurRow As Long, LastCol As Long
Dim CopyRng As Range

CurRow = ActiveCell.Row

LastCol = Cells(CurRow, Columns.Count).End(xlToLeft).Column

For CurCol = 1 To LastCol
    If Not Cells(CurRow, CurCol).Value = "" Then
        Set CopyRng = Cells(CurRow, CurCol)
    ElseIf CopyRng is Nothing Then
        MsgBox "Error First Cell is Blank"
    Else
        Cells(CurRow, CurCol).Text = CopyRng.Text
    End If
Next CurCol

End Sub

答案 1 :(得分:-1)

你尝试过这样的事吗?没有任何代码示例,很难看到您正在尝试做什么。

Sub CopyRow()
    Rows("32:32").Select
    Selection.Copy
    Rows("33:33").Select
    Selection.Insert Shift:=xlDown
End Sub