将活动单元格从选定范围复制到第一个空单元格

时间:2018-10-09 10:34:17

标签: vba excel-vba

情况是: 查找声明范围内的第一个空单元格。如果找到,则将值从活动单元格复制到“第一个找到的空单元格”并退出循环。

看不到应该改进的地方。退出循环时可能会出现一些错误。

Sub Copie()
Dim myNamedRange As Range
Set myNamedRange = myWorksheet.Range("L2:L11")
For Each cell In myNamedRange
    If IsEmpty(cell) = True Then
        ActiveCell.Value = cell.Value
        Exit For
    End If
Next cell
End Sub

2 个答案:

答案 0 :(得分:1)

要执行此操作而不循环,可以使用NextSlide = () => { const { currentIndex, data } = this.state; const slideWidth = this.slideWidth(); // Exiting the method early if we are at the end of the images array. // We also want to reset currentIndex and translateValue, so we return // to the first image in the array. if(currentIndex === data.length - 1) { return this.setState({ currentIndex: 0, translateValue: 0 }) } // This will not run if we met the if condition above this.setState(NextState => ({ currentIndex: NextState.currentIndex + 1, translateValue: NextState.translateValue + -(slideWidth) })); } slideWidth = () => { const { slideClass } = this.state; return document.querySelector(slideClass).clientWidth } ,但是使用小范围不会带来任何明显的改进。

FIND

答案 1 :(得分:0)

ActiveCell.Value = cell.Value应该是cell.Value = ActiveCell.Value

左侧填充右侧。看这个小例子:

Sub TestMe()        
    Dim a As Long
    Dim b As Long
    a = 10
    b = 20
    a = b
    MsgBox a 'shows 20, because it accepted "b"
End Sub

因此,这应该可行:

Sub TestMe()

    Dim myNamedRange As Range
    Set myNamedRange = Worksheets(1).Range("L2:L11")
    Dim myCell As Range

    For Each myCell In myNamedRange
        If IsEmpty(myCell) Then
            myCell.Value = ActiveCell.Value
            Exit For
        End If
    Next myCell

End Sub