VBA excel宏,用于迭代表格并选择列a中的行单元格等于某个值的所有行
我试图迭代一个表,并从所有行中获取索引,位置a(列a)的单元格(日期单元格)等于某个日期,我从另一个单元格中获取的日期,比如说VBA excel宏,用于迭代表格并选择列a中的行单元格等于某个值的所有行,vba,excel,Vba,Excel,我试图迭代一个表,并从所有行中获取索引,位置a(列a)的单元格(日期单元格)等于某个日期,我从另一个单元格中获取的日期,比如说H1Cell。单击时,我有一个带有附加宏的按钮。 该表有10000多行和7列(A、B、C、D、E、F、G) A列表示日期。在javascript中,这将是类似这样的事情 列A中的所有单元格都具有日/月/年值。整张桌子只放一年。因此,该表从2017年1月1日开始,到2017年12月31日结束 const myDate = worksheet.getCell('H1').Va
H1
Cell。单击时,我有一个带有附加宏的按钮。
该表有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单元格的所有行
编辑:
到目前为止我已经尝试过了,我正在存储和索引一天的开始和结束位置。所以我有这个范围。现在,我如何选择第一行和最后一行之间的所有单元格并将其打印出来
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上的单元格H4和H5来显示日期范围标准的示例
选项显式
公共子标记日期()
将ws作为工作表,colA作为范围,lc作为长度,hdr作为长度
设置ws=Sheet1
Set colA=ws.UsedRange.Columns(1)
lc=ws.UsedRange.Columns.Count+1'最后一列中今天的日期
Application.ScreenUpdating=False
可乐
.自动筛选字段:=1_
标准1:=“>=”&CDbl(ws.Range(“H4”))_
运算符:=xlAnd_
准则2:“
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