【问题标题】:Get Cell at Row n and Column (by name)获取第 n 行和第 1 列的单元格(按名称)
【发布时间】:2017-11-02 00:18:06
【问题描述】:

系统说明:我有一个电子表格,其中有一个名为 RentLogs 的表格(Sheet2,Renting Logs)。当您单击一个按钮时,会出现一个用户表单,用户选择返回项目的日期并点击“确定”。然后表单在表格中搜索项目的 ID,并在 签入日期 列中记录签入日期。

问题:

1) 我的 VBA 代码(如下)只能在整个工作表上找到项目 ID,我如何在表格中找到它的位置,以便可以移动表格而不用担心静态列值?
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,也不知道对象的所有属性。用户表单完美运行,所以没有问题。

【问题讨论】:

  • ws.Cells(FoundCell.Row, ws.Range("RentLog").Column).Value = Me.checkin_datepicker.Value 看起来您的列使用了错误的索引,不应该是Check In Date 吗?也许我不明白这里的问题......

标签: excel userform vba


【解决方案1】:

如果您真的想问如何引用表格中的单元格,您可以使用类似这样的内容。这里有一个有用的语法指南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

【讨论】:

  • 这成功了!我的特定版本有两个错误:表是“RentLog”,字段是“项目 ID”。非常感谢!
【解决方案2】:

要直接回答您的问题,要使用 VBA 引用表中的特定列,请使用 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"
)

【讨论】:

  • 注意:如果我正在寻找部分匹配,我只会使用Find 而不是索引匹配。例如,如果您有诸如20012002001 之类的值,并且您将Find 用于200,它会找到其中包含200 的第一个单元格,即使那是@ 987654333@ 或 2001.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多