【问题标题】:VBA excel macro that iterates a table and select all rows which row cell at column A equals somethingVBA excel宏,它迭代一个表并选择A列的行单元格等于某事的所有行
【发布时间】:2017-09-10 18:16:10
【问题描述】:

我正在尝试迭代一个表并从位置 A(A 列)处的单元格(日期单元格)等于某个日期的所有行获取索引,我从另一个单元格获取的日期可以说是 H1 单元格。单击时,我有一个带有附加宏的按钮。 该表有 10000 多行,有 7 列(A、B、C、D、E、F、G) A 列代表日期。在javascript中它会是这样的。

A 列内的所有单元格都有日/月/年值。整张桌子只有一年。所以表格从 2017 年 1 月 1 日开始,到 2017 年 12 月 31 日结束。

const myDate = worksheet.getCell('H1').Value;
const columnA = worksheet.getColumns('A') // here i use pseudocode 
columnA.forEach((cell, index) => {
    if (cell.Value == myDate)
      console.log(index); // instead i can push each index in array so later i could forEach the rows with these indexes and then do some manipulation.
});

我的任务基本上是将日期字符串放入 H1 单元格。并且当单击按钮时,必须打印第一个单元格(列 A )等于 H1 单元格的所有行。

已编辑:

到目前为止已经尝试过了,我正在存储和索引一天的开始和结束的位置。所以我有范围。现在如何选择所有行在 firstRow 和 lastRow 之间的单元格并将它们打印出来。

Sub FindMyNubmer()
    Dim a As Range, b As Range
    Dim firstRow As Long
    Dim lastRow As Long
    Set a = Range("A1:A65000")

    For Each b In a.Rows
        If b.Value = Range("H4").Value Then
            If firstRow = "0" Then
                firstRow = b.Row
            End If
            lastRow = b.Row
        End If
    Next
    MsgBox firstRow & " - " & lastRow
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    尝试自动过滤;我使用 Sheet1 上的单元格 H4 和 H5 来显示日期范围标准的示例


    Option Explicit
    
    Public Sub MarkDates()
        Dim ws As Worksheet, colA As Range, lc As Long, hdr As Long
    
        Set ws = Sheet1
        Set colA = ws.UsedRange.Columns(1)
        lc = ws.UsedRange.Columns.Count + 1     'Today's date in last col
    
        Application.ScreenUpdating = False
    
        With colA
    
            .AutoFilter Field:=1, _
                        Criteria1:=">=" & CDbl(ws.Range("H4")), _
                        Operator:=xlAnd, _
                        Criteria2:="<=" & CDbl(ws.Range("H5"))
    
            If .SpecialCells(xlCellTypeVisible).CountLarge > 1 Then
                hdr = Abs(Not IsDate(.Cells(1)))
                With ws.UsedRange.Columns(lc)
                    .Offset(hdr).Resize(.Rows.Count - hdr, 1) = Date    'Last used column
                    .NumberFormat = colA.NumberFormat
                End With
            End If
    
            .AutoFilter
    
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      相关资源
      最近更新 更多