将值从表中的循环复制到另一列-仅粘贴前12个值(和标头)

问题描述 投票:0回答:1

所以我目前正在一段代码上使用列表对象表-我已将其定义为InTbl。我要编写的代码将遍历该表的第四列(仅遍历数据主体行),并将该行上的所有值复制到表的第9列。

由于某种原因,代码本身适用于前12行(出于某种原因,还有标题),将值复制得很好然后停止吗?

代码在这里,然后我解释我认为哪里出了问题?

Sub AmendMVCCY
Dim InTbl As ListObject 'Dim the input table as 'InTbl'
Dim i As Long
Dim rng As Range, AmendedCCY As Range

Set InTbl = Sheets("Input").ListObjects("INPUT")

Set AmendedCCY = InTbl.ListColumns(4).DataBodyRange


    For i = 1 To AmendedCCY.Rows.Count 'For row starting as 1 to Last row within the data body
        If Not IsEmpty(AmendedCCY.Cells(i, 1)) Then 'The the cell isn't empty
            Cells(i, 4).Copy 'Copy the figure in amended Column
            Cells(i, 9).PasteSpecial xlValues 'Paste Values in 9th Column
        End If
    Next i


End Sub

我认为定义了ListColumn(4)后,它会误读数据。该表的行长为200ish,并且只会增长,但是当我声明时For i = 1 To AmendedCCY.Rows.Count它只计数12行,而不是全部。

这可能措辞不佳,但如果需要,我会尽力弄清楚。

本质上,我需要遍历整个Listobject表的循环,但不确定为什么不会在atm发生这种情况

编辑:表的图片(尽管有20行示例)enter image description here

基本上,最后,我需要修改的任何数字才能覆盖第9栏(该栏的前面有货币值)

excel vba
1个回答
0
投票

类似于FanDuru的建议:

代替:

For i = 1 To AmendedCCY.Rows.Count 'For row 
    If Not IsEmpty(AmendedCCY.Cells(i, 1)) Then 'The the cell isn't empty
        Cells(i, 4).Copy 'Copy the figure in amended Column
        Cells(i, 9).PasteSpecial xlValues 'Paste Values in 9th Column
    End If
Next i

尝试:

Dim lastRow as long
lastRow = AmendedCCY.Rows.Count
For i = 1 To lastRow 'For row starting as 1 to Last row within the data body
    If Not IsEmpty(AmendedCCY.Cells(i, 1)) Then 'The the cell isn't empty
        Cells(i, 9) = Cells(i, 4).value 
    End If
Next i

无需使用复制/粘贴。确保表的AmendedCCY.Rows.Count实际上大于十二。

© www.soinside.com 2019 - 2024. All rights reserved.