如何从数组写入excel vba

时间:2017-03-04 07:35:57

标签: arrays excel vba excel-vba

我试图创建一个脚本,当我按下一个按钮时,它会在表格上抓取,从表格中获取第一个值,在单元格中设置该值。采用在更改上一个值后自动更改的另一个值(总计),然后将该值打印到新工作表中的新数组中。所以我想到的流程是这样的。

  1. 获取百分比
  2. 将正确的单元格设置为该百分比
  3. 检索总数
  4. 将该总额保存到新表格
  5. 只需通过整个表格进行整理。

    我现在的代码如下:

    Dim listObj As ListObject, r%, c%
    Dim answerList As ListObject
    Dim thePercent As ListObject
    Dim theTot As ListObject
    Dim percentage As Single
    Dim totalen As Integer
    
    Set listObj = Sheets("Kostnadskalkylering").ListObjects("Tabell")
    Set answerList = Sheets("KostnadsTabell").ListObjects("answerTable")
    Set thePercent = Sheets("Artikeldata").ListObjects("thePercentage")
    Set theTot = Sheets("Artikeldata").ListObjects("theTotal")
    
    For c = 1 To listObj.ListColumns.Count
        For r = 1 To listObj.ListRows.Count
            percentage = listObj.DataBodyRange.Cells(r, c).Value
            thePercent.Value = percentage
            totalen = theTot.Value
            answerList.DataBodyRange.Cells(r, c).Value = totalen
        Next
    Next
    

    首先我想answerList也是Listobject,r%,c%然后我得到重复值的错误。使用我目前使用的代码,我得到一个越界错误。有人可以帮我看看我做错了什么。提前致谢

    编辑:

    名为listObj和answerList的表是8行9列。第一个表保存百分比,第二个表保持为空。我经常在itteration 9上遇到错误,所以可能在行与列中出错?

    thePercentage只是我希望将listObj中的百分比放入单元格的持有者。

    theTot是计算后具有总值的单元格的持有者

    百分比是百分比的临时变量,totalen是计算值的占位符。

0 个答案:

没有答案