簡體   English   中英

Excel VBA \\:如何使此代碼擴展到所有當前數據的末尾

[英]Excel VBA\: How to make this code extend to the end all present data

我正在制作此宏,該宏從信息模板生成原始數據表。 代碼的最后一部分返回到創建的工作表,插入行,並用其他信息填充這些行。 但是,我希望它還在所有數據的末尾添加相同的信息,但無法弄清楚該怎么做。 這是我到目前為止的代碼

' Declarations
Dim maxRows As Integer
Dim countRow As Integer
Dim addRowOffset As Integer
Dim innerLoop As Integer

innerLoop = 0

' Variable value assignment

'MsgBox ("periodQ = " & periodQ & "    periodP = " & periodP & "     periodS = " & periodS)
maxRows = (periodQ * periodP * periodS + periodR * periodS)

If (numRowsForRatings1 = 0 Or numRowsForRatings1 = 1 Or numRowsForRatings1 = numRowsForProducts1) Then
    addRowOffset = numRowsForRatings1
    'MsgBox ("addRowOffset = " & addRowOffset)
Else
    MsgBox ("There was an error in the number of Rating Questons")
End If

If addRowOffset > 0 Then

       For countRow = 2 To maxRows

        If Range("A" & countRow).Value < (Range("A" & (countRow + 1)).Value) Then

            ' code to insert rows for no. of rating equal to no. of products
            ElseIf (addRowOffset > 1) Then

                Range("A" & countRow + 1 & ":" & "A" & (countRow + addRowOffset)).EntireRow.Insert
            ' label "A" column cell as countRow
                Range("A" & countRow + 1 & ":" & "A" & (countRow + addRowOffset)).Value = Range("A" & countRow).Value
                Range("A" & countRow + 1 & ":" & "A" & (countRow + addRowOffset)).Value = Range("A" & countRow).Valu


            ' label "B" column cell as countRow
                For innerLoop = 0 To addRowOffset - 1
                        Dim iCounter9 As Long
                             For iCounter9 = 1 To addRowOffset
                             Range("B" & countRow + iCounter9).Select
                                 ActiveCell.FormulaR1C1 = iCounter9
                        Next iCounter9
                Next innerLoop

            ' label "C" column cell as countRow
                For innerLoop = 0 To addRowOffset - 1
                    Range("C" & countRow + innerLoop + 1).Value = _
                      Sheets("Template").Range("B" & (4 + innerLoop)).Value
                Next innerLoop


                countRow = countRow + addRowOffset

                ' label "D" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                    Range("D" & countRow - innerLoop).Value = _
                      Sheets("Template").Range("B" & (4 + periodP + innerLoop)).Value
                Next innerLoop

                ' label "E" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                        Dim iCounter10 As Long
                            For iCounter10 = 1 To addRowOffset
                             Range("E" & countRow - periodR + iCounter10).Select
                                 ActiveCell.FormulaR1C1 = iCounter10
                             Next iCounter10
                Next innerLoop

                ' label "F" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                      Range("F" & countRow - innerLoop).Value = _
                      Sheets("Template").Range("B" & (6 + periodP * 2 + periodQ * 3 + innerLoop)).Value
                Next innerLoop

                ' label "G" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                      Range("G" & countRow - innerLoop).Value = "Ranking"
                Next innerLoop

                ' label "I" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                      Range("I" & countRow - innerLoop).Value = "Standard"
                Next innerLoop

                ' label "J" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                      Range("J" & countRow - innerLoop).Value = "=IF((RC[+1])=""Yes"",IF((RC[-1])=""Invert"",10-(RC[-2]),(RC[-2])),"""")"
                Next innerLoop

                ' label "K" column cell as countRow

                For innerLoop = 0 To addRowOffset - 1
                      Range("K" & countRow - innerLoop).Value = "=IF(ISBLANK(RC[-3]), ""No"", ""Yes"")"
                Next innerLoop

                ' label remaining row cells
            Else
                MsgBox ("We got an error in adding rows based on no. of ratings")
            End If

        End If

    Next
End If

因此,我需要從A-K獲取信息,並將其放在所有創建的數據的末尾。 我做了幾次嘗試,如果有興趣的話,可以發布他們隨后的錯誤。

我從問題中讀到什么
似乎您僅在A節中添加行。然后,您逐單元循環遍歷每一列。

問題
1)您是否要插入行,而保持最后一行不變,還是在行尾添加行會更好? 2)您是將AK中的數據附加到L列中還是將數據匯總到其他位置?

建議
如果只需要添加可變數量的行,則可以:

  • 轉到A列的末尾並讀取值
  • 遍歷新行數,增加行數變量
  • 在各自的欄中填寫值(AK)
  • 通過串聯每個列中的值進行匯總
  • 移至下一行

如果您需要原始的最后一行成為最后一行:

  • 像在A節中一樣插入行
  • 遵循我為BK建議的相同循環(確保您僅觸摸了新行)
  • 到達最后一行時停止

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM