需要循环在 excel table 列中输入 formulas/data
Need a loop to enter formulas/data in excel table columns
我需要一个嵌套循环来将公式添加到我的 table ("Table1") 中的 4 个特定列。我希望我的 For 循环模仿之前关于这些相同的 4 个附加列 ("colNames") 的命名的 For 循环。
这段代码的底部工作得很好,但我想知道如何将它变成一个循环。
Sub attStatPivInsertTableColumns_2()
Dim lst As ListObject
Dim currentSht As Worksheet
Dim colNames As Variant, formNames As Variant '<~~ NOTE: As Varient, this is your go to when working with arrays
Dim oLC As ListColumn, oLData As Variant
Dim i As Integer, d As Integer
Set currentSht = ActiveWorkbook.Sheets("Sheet1")
Set lst = ActiveSheet.ListObjects("Table1")
colNames = Array("AHT", "Target AHT", "Transfers", "Target Transfers")
For i = 0 To UBound(colNames)
Set oLC = lst.ListColumns.Add
oLC.Name = colNames(i)
Next i
***Below is the code that needs to be looped***
'lst.ListColumns("Target AHT").DataBodyRange.FormulaR1C1 = "=350"
'lst.ListColumns("Target Transfers").DataBodyRange.FormulaR1C1 = "=0.15"
'lst.ListColumns("AHT").DataBodyRange.FormulaR1C1 = "=([@[Inbound Talk Time (Seconds)]]+[@[Inbound Hold Time (Seconds)]]+[@[Inbound Wrap Time (Seconds)]])/[@[Calls Handled]]"
'lst.ListColumns("Transfers").DataBodyRange.FormulaR1C1 = "=[@[Call Transfers and/or Conferences]]/[@[Calls Handled]]"
End Sub
这是我目前的目标,但我 运行 犯了错误,原因可能很明显:
formNames = 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")
For d = 0 To UBound(formNames)
For i = 0 To UBound(colNames)
Set oLData = lst.ListColumns(i).DataBodyRange.FormulaR1C1 = "d"
Next i
Next d
再构造一个变体数组,其中包含您要填充新的 table 列的 formulas/values。
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 2
和 hdrs(h+1)
对 formulas/values。
我需要一个嵌套循环来将公式添加到我的 table ("Table1") 中的 4 个特定列。我希望我的 For 循环模仿之前关于这些相同的 4 个附加列 ("colNames") 的命名的 For 循环。
这段代码的底部工作得很好,但我想知道如何将它变成一个循环。
Sub attStatPivInsertTableColumns_2()
Dim lst As ListObject
Dim currentSht As Worksheet
Dim colNames As Variant, formNames As Variant '<~~ NOTE: As Varient, this is your go to when working with arrays
Dim oLC As ListColumn, oLData As Variant
Dim i As Integer, d As Integer
Set currentSht = ActiveWorkbook.Sheets("Sheet1")
Set lst = ActiveSheet.ListObjects("Table1")
colNames = Array("AHT", "Target AHT", "Transfers", "Target Transfers")
For i = 0 To UBound(colNames)
Set oLC = lst.ListColumns.Add
oLC.Name = colNames(i)
Next i
***Below is the code that needs to be looped***
'lst.ListColumns("Target AHT").DataBodyRange.FormulaR1C1 = "=350"
'lst.ListColumns("Target Transfers").DataBodyRange.FormulaR1C1 = "=0.15"
'lst.ListColumns("AHT").DataBodyRange.FormulaR1C1 = "=([@[Inbound Talk Time (Seconds)]]+[@[Inbound Hold Time (Seconds)]]+[@[Inbound Wrap Time (Seconds)]])/[@[Calls Handled]]"
'lst.ListColumns("Transfers").DataBodyRange.FormulaR1C1 = "=[@[Call Transfers and/or Conferences]]/[@[Calls Handled]]"
End Sub
这是我目前的目标,但我 运行 犯了错误,原因可能很明显:
formNames = 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")
For d = 0 To UBound(formNames)
For i = 0 To UBound(colNames)
Set oLData = lst.ListColumns(i).DataBodyRange.FormulaR1C1 = "d"
Next i
Next d
再构造一个变体数组,其中包含您要填充新的 table 列的 formulas/values。
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 2
和 hdrs(h+1)
对 formulas/values。