Hi,
I am trying to run a simple macro that will insert x number of rows depending upon the value in a cell. Here is the code:
Sub InsertRows()
Dim c As Range
Dim RowCount As Long
For Each c In ActiveSheet.Range("A2", Range("A" & Rows.Count).End(xlUp))
RowCount = c.Offset(0, 8)
c.Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlToRight)).Select
Rows.Insert.Resize(RowCount).xlShiftDown
Next c
End Sub
When it gets to the line "Rows.Insert.Resize(RowCount).xlShiftDown", I receive a Run-Time error 1004: Possible loss of data because Excel can't move non-blank cells of the worksheet. Find the last non-blank cell and delete everything in between that and your data set. Select cell A1 and save your worksheet to reset the last cell.
I have done this a few times, and have even closed out of Excel completely after clearing out the cells around my data set. I've tried copying the data into a new sheet, new workbook, etc. I can't see anything wrong with the code. I am able to successfully insert rows manually, just not through VBA. There are only 236 rows of data in my sheet....also, the RowCount value maxes out at 9.
Help...!
I am trying to run a simple macro that will insert x number of rows depending upon the value in a cell. Here is the code:
Sub InsertRows()
Dim c As Range
Dim RowCount As Long
For Each c In ActiveSheet.Range("A2", Range("A" & Rows.Count).End(xlUp))
RowCount = c.Offset(0, 8)
c.Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlToRight)).Select
Rows.Insert.Resize(RowCount).xlShiftDown
Next c
End Sub
Code:
When it gets to the line "Rows.Insert.Resize(RowCount).xlShiftDown", I receive a Run-Time error 1004: Possible loss of data because Excel can't move non-blank cells of the worksheet. Find the last non-blank cell and delete everything in between that and your data set. Select cell A1 and save your worksheet to reset the last cell.
I have done this a few times, and have even closed out of Excel completely after clearing out the cells around my data set. I've tried copying the data into a new sheet, new workbook, etc. I can't see anything wrong with the code. I am able to successfully insert rows manually, just not through VBA. There are only 236 rows of data in my sheet....also, the RowCount value maxes out at 9.
Help...!