需要一个循环来在excel表列中输入公式/数据

时间:2016-06-01 22:10:42

标签: excel vba excel-vba for-loop listobject

我需要一个嵌套循环来将公式添加到我的表中的4个特定列(“Table1”)。我希望我的For循环模仿前面关于命名这些相同的4个附加列(“colNames”)的For循环。

此代码的底部部分工作得很好,但我想知道如何将其用于循环。

"chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
      users
       uid_1: true
       uid_3: true
    },
    "two": { ... },

这是我到目前为止的目标,但我遇到了错误,原因可能是显而易见的原因:

chatsRef.queryOrderedByChild("users/uid_3").queryEqualToValue(true)
     .observeSingleEventOfType(.Value, withBlock: { snapshot in

     //.Value can return multiple nodes within the snapshot so iterate over them
     for child in snapshot.children {
          let lastmsg = child.value["lastMessage"] as! String
          let timestamp = child.value["timestamp"] as! String
          print(lastmsg)
          print(timestamp)
     }     
})

1 个答案:

答案 0 :(得分:1)

构造一个包含要使用。

填充新表列的公式/值的变量数组
Sub insertTableColumn()
    Dim lst As ListObject
    Dim currentSht As Worksheet
    Dim h As Long, hdrs As Variant, r1c1s As Variant

    hdrs = Array("AHT", "Target AHT", "Transfers", "Target Transfers")
    r1c1s = Array("=([@[Inbound Talk Time (Seconds)]]+[@[Inbound Hold Time (Seconds)]]+[@[Inbound Wrap Time (Seconds)]])/[@[Calls Handled]]", _
                    350, _
                    "=[@[Call Transfers and/or Conferences]]/[@[Calls Handled]]", _
                    0.15)

    Set currentSht = ActiveWorkbook.Sheets("Sheet1")
    Set lst = ActiveSheet.ListObjects("Table1")

    With lst 'ActiveSheet.ListObjects("Table1")
        For h = LBound(hdrs) To UBound(hdrs)
            .ListColumns.Add
            .ListColumns(.ListColumns.Count).Name = hdrs(h)
            .ListColumns(.ListColumns.Count).DataBodyRange.FormulaR1C1 = r1c1s(h)
        Next h
    End With

End Sub

我还在单个数组中使用了对,h = LBound(hdrs) To UBound(hdrs) Step 2hdrs(h+1)用于公式/值。