【问题标题】:Loop with the row range - How can I get a specific cell?使用行范围循环 - 如何获取特定单元格?
【发布时间】:2018-10-02 08:26:42
【问题描述】:
With Worksheets("Tripdata")
    For filtercount = 1 To 1
        Worksheets("Tripdata").Range("A1").AutoFilter Field:=4, Criteria1:=stationidentify(filtercount, 1)
        For Each rowIndex In .UsedRange.Rows
            If (rowIndex.Hidden) Then
            'nothing
            Else
                If (firstpass) Then
                    firstpass = False
                Else
                    MsgBox rowIndex(2, 2)
                    test = test + 1
                    Dim currentstat As Double, finalstat As Double
                    currentstat = Worksheets("Tripdata").Cells(rowIndex, 3).Value
                    finalstat = Worksheets("Tripdata").Cells(rowIndex, 7).Value

The tripdata visual look - I wanna somehow get the names on the fourth and seventh cells

您好,我正在尝试使用上述循环获取特定单元格。我认为 rowIndex 是 .Cells(row,column); 上的行号但似乎不是。我正在尝试使用过滤器遍历数据。
谁能帮我解决这个问题?
提前致谢。

【问题讨论】:

  • 我不明白MsgBox rowIndex(2, 2)。为什么要从当前 rowIndex 向下一排和 B 列中的单元格?
  • @Jeeped 我目前正在尝试通过编写一堆东西来了解 rowIndex 实际得到的是什么。那是错误的。我的问题是不知道如何使用每个循环访问该行上的单元格。
  • For filtercount = 1 To 1?此外,(firstpass) 应该只是 filterpass

标签: vba excel loops excel-2007


【解决方案1】:

您正在处理单行,因此如果直接处理 rowIndex,则任何 Cells(row, column) 引用都应为 1,如果引用工作表上的单元格,则 rowIndex.row 应为 1。

'put these outside the loop
Dim currentstat As Double, finalstat As Double
For Each rowIndex In .UsedRange.Rows
...
Else
    MsgBox rowIndex(1, 2)  'column B value within rowIndex
    MsgBox rowIndex.cells(1, 2)  'column B value within rowIndex
    test = test + 1
    currentstat = Worksheets("Tripdata").Cells(rowIndex.row, 3).Value
    finalstat = Worksheets("Tripdata").Cells(rowIndex.row, 7).Value
    'you're inside a With ... End With block so these are the same
    currentstat = .Cells(rowIndex.row, 3).Value  'column C value within rowIndex
    finalstat = .Cells(rowIndex.row, 7).Value  'column G value within rowIndex
    debug.print currentstat
    debug.print finalstat 

【讨论】:

  • 我想我现在明白了。 with 的使用让我很困惑。谢谢你。我希望我不明白 rowIndex(1,2) 和 rowIndex.Cells(1,2) 之间的区别。
  • 没有区别。前者是后者的简写,省略了默认属性 .cells。
  • 效果很好。尝试为调度优化编写代码。我是 VBA 语法的新手。非常感谢您的帮助。
  • 顺便说一句,Worksheets("Tripdata").Range("A1").AutoFilter 可能只是 .Range("A1").AutoFilter,因为它也在 With ... End With 块内。
猜你喜欢
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多