PDA

View Full Version : How to inser Row using VB



naikadit
08-25-2008, 03:28 PM
Hi I am trying to write a code in VB in excel.

For j = 2 To row_low
For i = 1 To 52
If Cells(j, 14) <> i Then
main_file.Sheets("Sheet6").Rows("j").Select
Selection.EntireRow.Insert
Cells(j, 14) = i
Cells(j, 7) = 0
End If


if the condition cells(j,14)<> i is satisfied then i want to add an entire row and want to write same number as i in the cell(j,14) and put 0 in another cell. I am getting error in the insertion part that subcript out of range please help.

Bob Phillips
08-25-2008, 03:39 PM
I am not sure that I get it, but how about



For j = 2 To row_low
For i = 1 To 52
If Cells(j, 14) <> i Then
main_file.Sheets("Sheet6").Rows(j).Insert
main_file.Sheets("Sheet6").Cells(j, 14) = i
main_file.Sheets("Sheet6").Cells(j, 7) = 0
End If
Next i
Next j

naikadit
08-25-2008, 03:57 PM
I am getting an error Subscript out of range after
main_file.Sheets("Sheet6").Rows(j).Insert

naikadit
08-25-2008, 03:59 PM
also if the condition doesnt satisfy I want to inser a row and shift the entire data by one

naikadit
08-25-2008, 04:02 PM
This is the entire code


Sub outage_2008()

Dim how_low As Integer, dur As Double, out_dur As Double, node As String
Dim i As Integer, month_num As Integer, node_num As Integer, j As Integer
Dim m As Integer, n As Integer
Dim row_low As Long, row_start As Long, collumn_start As Integer
Dim main_file As Workbook
Set main_file = Workbooks("Event Recovery TimesV1.xls")
Sheet6.Activate
row_start = 2
collumn_start = 1
row_low = find_low(row_start, collumn_start)
For j = 2 To row_low
For i = 1 To 52
If Cells(j, 14) <> i Then
Sheets("Sheet6").Rows(j).Insert
Cells(j, 14) = i
Cells(j, 7) = 0
End If
Next i
Next j



End Sub

I have written a function find_low which calculates teh minimum number of rows and its working fine

mdmackillop
08-26-2008, 12:02 AM
Is there a Sheet called "Sheet6"? This may not be the same sheet as the codename Sheet6 used earlier in your code.