vba代码,仅选择除标题之外的特定列中的可见单元格

时间:2015-09-06 16:32:42

标签: excel-vba vba excel

我需要代码只选择除标题之外的特定列中的可见单元格。

这就是我的尝试:

myfbgraph = facebook.GraphAPI(token)
mylikes = myfbgraph.get_connections(id="me", connection_name="likes")['data']
for like in mylikes:
  print like['name'], like['category']
  ...

但是上面的代码将选择除标题之外的整个列。我需要它只选择一个特定的列(使用D列作为答案)。

2 个答案:

答案 0 :(得分:1)

这将选择列D中包含数据的所有可见单元格,但标题和最后一个带有数据的可见单元格除外:

Option Explicit

Sub SelectVisibleInColD()
    Dim lRow As Long

    With ActiveSheet

        lRow = .Cells(.Rows.Count, 4).End(xlUp).Row

        If lRow < 3 Then Exit Sub

        .Cells(1, 4).Offset(1, 0).Resize(lRow - 2).SpecialCells(xlCellTypeVisible).Select

    End With
End Sub

答案 1 :(得分:1)

这是你在尝试什么?这将选择除A标题之外的所有可见单元格直到最后一行

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~>  I also want to exclude the last cell which is visible and contains data
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row - 1 

        If lRow < 3 Then Exit Sub

        Debug.Print Intersect( _
                              .Range("A2:A" & lRow), _
                              .Range("A1").Offset(1, 0).SpecialCells(xlCellTypeVisible) _
                              ).Address
    End With
End Sub