如何选择列范围内的最后一个单元格?

时间:2018-11-02 18:08:15

标签: excel vba excel-vba

我正在尝试选择范围的最终值并将该值放入单元格Q4中。该范围是动态的,从第四行开始。

这是我的代码:

Sub test()
    Dim PSpark As Worksheet
    Dim lc As Long

    Set PSpark = Worksheets("ws1")
    lc = PSpark.Cells(4, Columns.Count).End(xlToLeft).Column

    With PSpark            
        .Range("Q4") = lc.Value
    End With
End Sub

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

非常接近,我想你想要这个:

因此,我们使用找到的列号,然后在最后一行的第4行中选择值。将值复制到单元格Q4中。 Cells(row, column)

Sub test()

Dim PSpark As Worksheet
Dim lc As Long

Set PSpark = Worksheets("ws1")
lc = PSpark.Cells(4, Columns.Count).End(xlToLeft).Column 'Gives us the last column number. If the value is in Column C, then we get lc = 3.

    With PSpark

        .Range("Q4") = .Cells(4, lc).Value 'Which row from column lc (which is the last column) we want to copy from

    End With


End Sub