2017-09-10 158 views
0

我想迭代一個表並獲取所有行的位置A(列A)的單元格(單元格)等於某個日期,我從另一個單元獲得的日期讓我說H1單元格。點擊時,我有一個附帶宏的按鈕。 該表有超過10000行,並有7列(A,B,C,D,E,F,G) A列表示日期。在JavaScript中它會是這樣的東西。VBA excel宏迭代一個表,並選擇列A行中的行單元格的所有行

A列內的所有單元格都有日/月/年的值。整個桌子只有一年。所以表格從01/01/2017開始,並將於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 

回答

1

嘗試AutoFilter;我用Sheet 1上的細胞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 
一個例子
相關問題