获取第 n 行和第 n 列的单元格(按名称)

Get Cell at Row n and Column (by name)

系统描述:我有一个价差sheet,其中一个table叫做RentLogs =38=](表 2,出租日志)。当您单击一个按钮时,会出现一个用户表单,用户可以选择退回商品的日期并点击 "OK"。然后,表单会在 table 中搜索项目 ID,并在 登记日期 列中记录登记日期。

问题:

1) 我的 VBA 代码(下面)只能找到整个 sheet 的项目 ID,我如何找到它在 table 中的位置所以 table 可以移动而不用担心静态列值?
2) 如何让 VBA 将其放入(项目 ID 行,签入日期 列)?

Private Sub checkout_cmdbutton_Click()
    Dim ws As Worksheet
    Set ws = Worksheets("Renting Logs")

    Const WHAT_TO_FIND As Integer = 200

    Set FoundCell = ws.Range("RentLog").Find(What:=WHAT_TO_FIND, SearchOrder:=xlRows, SearchDirection:=xlNext, LookIn:=xlValues)
    If Not FoundCell Is Nothing Then
        MsgBox (WHAT_TO_FIND & " found in (" & FoundCell.Row & "," & FoundCell.Column & ")")
    Else
        MsgBox (WHAT_TO_FIND & " not found in RentLog")
        Exit Sub
    End If

'copy the check in date to the table
    ws.Cells(FoundCell.Row, ws.Range("RentLog").Column).Value = Me.checkin_datepicker.Value

'clear userform
    Me.checkin_datepicker.Value = Date

'close userform
Unload Me End Sub 

我的尝试: 我知道问题出在我的搜索算法上,但我不熟悉 VBA 并且不知道对象的所有属性。用户窗体工作完美,所以没有问题。

要直接回答您的问题,要使用 VBA 引用 table 中的特定列,您可以使用 Range("TableName[Column Name]")(实际上,这就是您引用它的方式在 Excel 公式中也是如此:TableName[Column Name]).

至于实现,我个人总是去索引匹配。

Private Sub checkout_cmdbutton_Click()

    Dim ws As Worksheet
    Set ws = Worksheets("Renting Logs")

    Const WHAT_TO_FIND As Integer = 200

    Dim FoundRow As Variant
    Dim FoundCell As Range, TargetCell As Range

    FoundRow = Application.Match(WHAT_TO_FIND, ws.Range("RentLogs[Item ID]"), 0) ' if there is a match of WHAT_TO_FIND in the "Item ID" column of "RentLogs" table, returns a number corresponding to the row, otherwise returns an error
    If Not IsError(FoundRow) Then ' if there is a match
        Set FoundCell = Application.WorksheetFunction.Index(ws.Range("RentLogs[Item ID]"), FoundRow) ' get the corresponding cell in the "Item ID" column of "RentLogs" table at the same row as the match,
        MsgBox (WHAT_TO_FIND & " found in (" & FoundCell.Row & "," & FoundCell.Column & ")") ' you may wish to look at FoundCell.Row and FoundCell.Column, as this is currently relative to the worksheet, not the table
    Else ' if there is no match
        MsgBox (WHAT_TO_FIND & " not found in RentLog")
        Exit Sub
    End If

    'copy the check in date to the table
    Set TargetCell = Application.WorksheetFunction.Index(ws.Range("RentLogs[Check In Date]"), FoundRow) ' get the corresponding cell in the "Check In Date" column of "RentLogs" table at the same row as the match
    TargetCell.Value = Me.checkin_datepicker.Value

    'clear userform
    Me.checkin_datepicker.Value = Date

    'close userform
    Unload Me

End Sub

得到TargetCell,以上等同于有一个excel公式,如:

=IF(
    NOT(
        ISERROR(
            MATCH(200,RentLogs[Item ID],0)
        )
    ),
    INDEX(RentLogs[Check In Date],
        MATCH(200,RentLogs[Item ID],0)
    ),
    200 & " not found in RentLog"
)

如果你真的想问如何引用 table 中的单元格,你可以使用类似这样的方法。这里有一个有用的语法指南 https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables

Private Sub checkout_cmdbutton_Click()

Dim ws As Worksheet, r As ListObject, foundcell As Range

Set ws = Worksheets("Renting Logs")
Set r = ws.ListObjects("RentLogs")
Const WHAT_TO_FIND As Integer = 200

Set foundcell = r.ListColumns("ID").DataBodyRange.Find(What:=WHAT_TO_FIND)

If Not foundcell Is Nothing Then
    MsgBox (WHAT_TO_FIND & " found in (" & foundcell.Row & "," & foundcell.Column & ")")
    r.ListColumns("Check in Date").Range(foundcell.Row - r.HeaderRowRange.Row + 1).Value = Me.checkin_datepicker.Value
Else
    MsgBox (WHAT_TO_FIND & " not found in RentLog")
    Exit Sub
End If

End Sub

'clear userform
Me.checkin_datepicker.Value = Date

'close userform
Unload Me

End Sub