如何查找行中第一个空单元格的列名?

问题描述 投票:0回答:1

这是我的示例数据。 my data looks like this

我想选择第1行中的第一个空单元格,即D列,并使用此列名来使用range函数填充数据。简单来说,我想在D2:D3中填充数据。我写了这样的代码-但这会将数据写到C2:C3而不是D2:D3。

    Dim row As Long
    Dim lastrow As Long
    Dim lastcolumn As Variant

    lastcolumn = Split(Workbooks("Release.xlsm").Worksheets("Sheet4").Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Cells.Address(1, 0), "$")(0)
lastrow = Workbooks("Release3.xlsm").Worksheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).row
    For row = 2 To lastrow

        Range(lastcolumn & row).Value = "success"

    Next
excel vba excel-vba
1个回答
0
投票

这是方法:-

Dim R As Long                       ' "row" is reserved for Excel's use
Dim lastRow As Long
Dim lastColumn As Variant

With Workbooks("Release.xlsm").Worksheets("Sheet4")
    ' observe the leading period in ".Rows.Count"
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
    lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1

    For R = 2 To lastRow

        .Cells(R, lastColumn).Value = "success"

    Next
End With

lastRowlastColumn在您的设置中的区别是您想知道最后使用的行,但想知道下一个空闲列。 [下一个免费] = [最后使用] +1。我认为您的装置确实有效-未测试-但以上内容更为主流。

© www.soinside.com 2019 - 2024. All rights reserved.