使用表作为VBA中的常量值与listobjects

时间:2017-01-17 18:11:07

标签: excel vba excel-vba

根据上一个问题,我有代码根据标题行和第一列的内容更新动态表中的特定单元格。常量值是代码中的固定值,但我现在需要能够将其引用到映射表,以便可以一次更新多个单元格。

现有代码和我需要使用的表格示例如下。

sub ChangeTable()

Dim wsMapping As Worksheet
Dim wsData As Worksheet
Dim tbl As ListObject
Dim x As Long
Dim i As Long
Dim hdrCount As Long

Const ACCT_NO = "ABCD1234"
Const HEADING = "Analysis/*"
Const NEW_VAL = "80321"


Set wsData = Worksheets("Test Sheet")
Set wsMapping = Worksheets("Mapping")


Set tbl = wsData.ListObjects("Data")
hdrCount = tbl.HeaderRowRange.Columns.Count

For x = 1 To tbl.ListRows.Count
    With tbl.ListRows(x)
        If .Range(1, 1).Value2 = ACCT_NO Then
            For i = 2 To hdrCount
                If (tbl.HeaderRowRange(i).Value2 Like HEADING) Then
                    If (Not IsEmpty(.Range(1, i).Value)) Then
                    .Range(1, i).Value = NEW_VAL
                    End If
                End If
            Next
        End If
    End With
Next

End Sub

在Excel的单独工作表中,将有一个表格,其中包含帐号和新值的列。最多可以有10个不同的帐号进行更改,每次生成文件时都可以变量,因此每次使用时代码都需要能够处理可变数量的常量。

ABCD1234    80310
ABCD1235    80311
ABCD1236    80312
ABCD1237    80313
ABCD1238    80314
ABCD1239    80315
ABCD1240    80316

我想我需要定义表格范围并说出类似的内容:

如果不是IsEmpty(Cells(x,1))那么(Const ACCT_NO = Cells(x,1)AND Const NEW_VAL = Cells(x,2))

并遍历表格中的每一行。这会有用吗?

1 个答案:

答案 0 :(得分:0)

除了你不能改变存储在常数中的值(根据定义,它们是常数,即不变)这听起来好像你在正确的轨道上。

将当前常量声明为普通字符串变量(例如Dim ACCT_NO As String),然后在循环中使用If not IsEmpty(Cells(x, 1)) Then ACCT_NO = Cells(x, 1): NEW_VAL = Cells(x, 2)

可能你的代码看起来像这样:

Sub ChangeTable()

    Dim wsMapping As Worksheet
    Dim wsData As Worksheet
    Dim tbl As ListObject
    Dim x As Long
    Dim i As Long
    Dim rowAcctNo As Long
    Dim lastrowAcctNo As Long
    Dim wsAcctNo As Worksheet
    Dim hdrCount As Long

    Dim ACCT_NO As String
    CONST HEADING = "Analysis/*"
    Dim NEW_VAL As String

    Set wsData = Worksheets("Test Sheet")
    Set wsMapping = Worksheets("Mapping")
    Set wsAcctNo = Worksheets("AcctNos") 'or whatever the sheet is called
    'Determine how many account number rows to process
    With wsAcctNo
        lastrowAcctNo = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Set tbl = wsData.ListObjects("Data")
    hdrCount = tbl.HeaderRowRange.Columns.Count

    For rowAcctNo = 1 to lastRowAcctNo
        If Not IsEmpty(wsAcctNo.Cells(rowAcctNo, 1)) Then
            ACCT_NO = wsAcctNo.Cells(rowAcctNo, 1).Value
            NEW_VAL = wsAcctNo.Cells(rowAcctNo, 2).Value
            For x = 1 To tbl.ListRows.Count
                With tbl.ListRows(x)
                    If .Range(1, 1).Value2 = ACCT_NO Then
                        For i = 2 To hdrCount
                            If (tbl.HeaderRowRange(i).Value2 Like HEADING) Then
                                If (Not IsEmpty(.Range(1, i).Value)) Then
                                    .Range(1, i).Value = NEW_VAL
                                End If
                            End If
                        Next
                    End If
                End With
            Next
        End If
    Next
End Sub