简体   繁体   中英

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

I'm making this macro that generates a raw data sheet from a template of information. The last part of the code goes back through the created sheet, inserts rows, and fills those rows with additional information. However, I want it to also add the same information at the very end of all the data, but can't figure out how. Here is the code I have so far

' 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

So, I need to get the information from A - K and put it at the end of all the created data. I made several attempts, that I can post with their subsequent errors if anyone is interested.

What I read from the problem
It seems like you are only adding rows in section A. Afterwards, you are looping through each column cell-by-cell.

Questions
1) Do you want to insert rows, leaving the last row untouched, or would it be better to add rows to the end? 2) Are you appending the data from AK into column L or summarizing the data somewhere else?

Suggestion
If you just need to add a variable number of rows, you could:

  • go to the end of the A column and read the value
  • Loop through the number of new rows, incrementing the row number variable
  • Fill in the values in their respective column (AK)
  • Summarize by concatenating the values in each of the columns
  • Move on to the next row

If you need the original final row to be the final row:

  • Insert the rows like you do in section A
  • Follow the same loop that I suggested for BK (Make sure you just touch the new rows)
  • Stop when you reach the last row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM