VBA:如何使用大于零的第一个单元格的地址

时间:2017-09-26 07:48:28

标签: excel-vba vba excel

我在这个问题上找到了多个答案,但我不知道如何将它应用到我自己的工作表中。 代码的第一步是在C列上进行升序排序。 第二步是在C列中找到第一个单元格,其值大于零(应该是C7)。 我无法弄清楚如何在第二步中使用地址来剪切第7,8和9行并将它们粘贴到Sheet2上

到目前为止,这是代码:

Sub findAdres()

' First Step Sort Values

Range("A2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range( _
    "C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
With ActiveSheet.Sort
    .SetRange Range("A:C")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

' Second Step find address of first cell in column C greater than zero

Dim lcell As Range

For Each lcell In Range("C2:C100")
  If CLng(lcell.Value) > 0 Then
     not_zero = lcell.Address
     Exit For
  End If
Next lcell

' Third step is cut all rows from row 7 down to end an paste in sheet2
' how do you use the lcell.Address to indicate what you want to cut?

End Sub

1 个答案:

答案 0 :(得分:0)

您已从上一步中获得lcell(定义为Range),现在您可以将其与Resize一起使用。

' Third step is cut all rows from row 7 down to end an paste in sheet2    
' Copy >> Paste in 1 line, to "Sheet2" at "A2"
lcell.Resize(100 - lcell.Row + 1, 1).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A2")

注意:我使用100作为最后一行(与上面的代码相同)。

注意2 :如果需要,您可以使用For Each lcell In Range("C2:C100")替换步骤2中的Find循环。