如何利用VBA和Match函数更新Excel表格中的单元格数值? [英] How to update cell value in Excel table using VBA and Match function?

查看:64
本文介绍了如何利用VBA和Match函数更新Excel表格中的单元格数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是这里的VBA专家,但我尽了最大努力...

我们的目标是创建一个宏来根据Application.Match函数中的表行变量更新表中的单元格值,这也是我正在努力解决的问题。以下是我到目前为止所拥有的以及我迷路的地方(还注释到代码中)。

  1. 我似乎无法获得Match函数来将TargetRw变量设置为表中匹配的行。目前,我遇到了"类型不匹配",但我尝试了几种不同的配置,并收到了各种不同的错误。

  2. 如果我可以进行匹配,我希望能够将TargetRw和表列"revised 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
    

    结束订阅

单个工作表上有两个表(变量:‘LUTables’)。一个或另一个将根据启动宏时的活动表进行更新。这两个表都有一个名为"已审阅比率"的列,这也是每个表中的第6列。

如有任何帮助,我们将不胜感激!

推荐答案

TargetTable.ListColumns(1)

TargetTable.ListColumns(1).DataBodyRange

ListColumn与范围不是一回事

未测试:

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

这篇关于如何利用VBA和Match函数更新Excel表格中的单元格数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆