簡體   English   中英

如何使用 VBA 更新 Excel 表中的單元格值並匹配 function?

[英]How to update cell value in Excel table using VBA and Match function?

這里不是 VBA 專業人士,但盡我所能......

目標是創建一個宏,它根據 Application.Match function 中的表行變量更新表中的單元格值,我也在努力解決這個問題。 這是我到目前為止所擁有的以及我迷路的地方(也在代碼中進行了注釋)。

  1. 我似乎無法獲得匹配 function 將我的 TargetRw 變量設置為表中匹配的行。 因為目前我得到“類型不匹配”,但我嘗試了幾種不同的配置並收到了各種不同的錯誤。

  2. 如果我能讓匹配工作,我希望能夠將 TargetRw 和表列“Reviewed Rate”= 的單元格值設置為“Rate”變量中保存的值。 我無法在網上找到很多關於如何引用這樣的表格范圍以更新單元格值的信息。

     Sub ReviewTracker() Dim Acell As Variant Dim TargetRw As Long Dim Rate As Variant Dim MACMtable, RCtable, TargetTable As ListObject Dim LUTables As Worksheet Set LUTables = ThisWorkbook.Sheets("LookupTables") Set MACMtable = LUTables.ListObjects("MACM_Lookup") Set RCtable = LUTables.ListObjects("RC_Lookup") Asht = ActiveSheet.Name Acell = ActiveCell.Value Rate = ActiveCell.Offset(0, -3).Value If Asht = "Rate Codes" Then Set TargetTable = RCtable Else If Asht = "MACMs" Then Set TargetTable = MACMtable End If End If ***''' Can't get the TargetRw variable below to work... Type Missmatch'''*** TargetRw = Application.Match(Acell, TargetTable.ListColumns(1), 0) With TargetTable ******'''I am trying to figure out how to set the cell corresponding to the row: TargetRw & Column 6 (name: "Reviewed Rate") to the value of the variable 'Rate'******.DataBodyRange.Cells(TargetRw, 6) = Rate.Value '''This doesn't seem to work, but hopefully illustrates the goal''' End With

    結束子

一個工作表上有 2 個表(變量:'LUTables')。 一個或另一個將根據宏啟動時的活動表進行更新。 兩者都有一個名為“Reviewed Rate”的列,這也是每個表中的第 6 列。

任何幫助將不勝感激!

TargetTable.ListColumns(1)

應該

TargetTable.ListColumns(1).DataBodyRange

ListColumn 與 Range 不同

未經測試:

Sub ReviewTracker()

    Dim Acell As Variant, Asht As String
    Dim TargetRw As Variant '***
    Dim Rate As Variant
    Dim TargetTable As ListObject
    Dim LUTables As Worksheet

    Set LUTables = ThisWorkbook.Sheets("LookupTables")

    Asht = ActiveSheet.Name
    Acell = ActiveCell.Value
    Rate = ActiveCell.Offset(0, -3).Value

    If Asht = "Rate Codes" Then
        Set TargetTable = LUTables.ListObjects("RC_Lookup")
    ElseIf Asht = "MACMs" Then
        Set TargetTable = LUTables.ListObjects("MACM_Lookup")
    End If

    TargetRw = Application.Match(Acell, TargetTable.ListColumns(1).DataBodyRange, 0)

    If Not IsError(TargetRw) Then
        TargetTable.DataBodyRange.Cells(TargetRw, 6) = Rate '### no .Value
    End If

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM