数组未填充第一个标题

时间:2017-12-19 00:01:07

标签: vba excel-vba excel

下面的代码,要添加一系列列标题,不会将第一个标题“帐户ID”添加到第一列。而是,单元格A1填充“#Pmts”。我找不到为什么会这样。我目前的解决方法是添加第二个“帐户ID”。

Sub Create_Transaction_Notes()

Dim DSA As Worksheet
Set DSA = Worksheets("DEBT_SALE_ACTIVITY")
Dim LF As Worksheet
Set LF = Worksheets("LOAD_FILE")
Dim myArray As Variant
Dim myCount As Integer

'Add in the column titles
DSA.Activate
myArray = Array("Account ID", "# Pmts.", "Total value Pmts.", _
    "Avg. Pmt. value", "# Purchases", "Total purch. value", _
    "Avg. purch. value", "# Cash adv.", "Total cash adv. value")

With DSA
    For myCount = 1 To UBound(myArray)
        .Cells(1, myCount).Value = myArray(myCount)
        Next myCount
    End With

2 个答案:

答案 0 :(得分:2)

无需循环:

[DEBT_SALE_ACTIVITY!A1:I1] = Array("Account ID", "# Pmts.", "Total value Pmts.", _
                              "Avg. Pmt. value", "# Purchases", "Total purch. value", _
                            "Avg. purch. value", "# Cash adv.", "Total cash adv. value")

答案 1 :(得分:1)

VBA中的数组默认以0开头。将您的for语句更改为

For myCount = 0 To UBound(myArray)