VBA 迭代表

J. *_*Doe 2 excel vba

我正在尝试迭代过滤表,并打印 3 列的值。我希望他们将每一行作为一组(供应商、工厂、价格)进行打印。这是我到目前为止的代码。

For Each Row In Union(Range("TblRawData[Supplier]"), Range("TblRawData[plant]"), Range("TblRawData[price]")).Rows
    If Row.EntireRow.Hidden = False Then
          Debug.Print Row.Value
    End If
Next Row
Run Code Online (Sandbox Code Playgroud)

该代码打印所有供应商,然后打印所有工厂,然后打印所有价格。而不是每一行都是一组。

Code Results:        Desired Result:
supplier1            supplier1, plant1, $1.50
supplier2            supplier2, plant2, $2.00
supplier3            supplier3, plant3, $3.00
plant1
plant2
plant3
$1.50
$2.00
$3.00
Run Code Online (Sandbox Code Playgroud)

Sco*_*man 5

ListRows这种方法使用简单的变量构造对我有用。

(在这种情况下请原谅a, b,c变量名称)

Option Explicit

Sub printRows()

    Dim rawData As ListObject
    Set rawData = Worksheets("Sheet1").ListObjects("TblRawData") 'change sheet name as needed

    Dim lr As ListRow
    For Each lr In rawData.ListRows

        If lr.Range(1, 1).EntireRow.Hidden = False Then

            Dim a As String, b As String, c As String
            a = lr.Range(1, rawData.ListColumns("Supplier").Index).Value
            b = lr.Range(1, rawData.ListColumns("plant").Index).Value
            c = Format(lr.Range(1, rawData.ListColumns("price").Index).Value, "$#,###.00")

            Dim output As String
            output = a & ", " & b & ", " & c

            Debug.Print output

        End If

    Next

End Sub
Run Code Online (Sandbox Code Playgroud)