WBURKERT
Technical User
- May 28, 2010
- 73
Please help (I have a hunch I am missing just one instruction)
-This routine first deletes all worksheets named SEC1 through SEC200 - this works fine
-then recreates 200 new worksheets named SEC1 through SEC200 - this works fine. I always write new data to all worksheets SEC1 through SEC200 and found that trying to clear 200 worksheets took a long time so I just delete them them and re-add them. - Again this works fine
-then the macro goes through a list looking for records with 1.01 and writes all records associated to 1.01 to Sec1 - this works
-then the macro increments to 2.01 and then looks for all records associated to 2.01 and writes them to Sec2 - THIS DOES NOT WORK. The macro increments to 2.01, searches through my file and finds records associated to 2.01 but will not write the data to SEC2. If I tell the macro that the first section is 2.01 then it writes fine to SEC2 but then increments but won't write to 3.01. It seems that the macro writes to the first section but no others and I sense I might have to release control of Sec1 before moving to Sec2. Please take a look through my code and see if you can help me out. I really don't want to re-write too much so please be kind with my coding. Thanks in advance.
Sub PARTS_To_Sections()
Dim Count As Long
For Count = 1 To 200
Application.DisplayAlerts = False
Sheets("Sec" & Count).Delete
Application.DisplayAlerts = True
Next Count
For Count = 1 To 200
Worksheets.Add().Name = "Sec" & Count
Next Count
Dim iRow As Integer
Dim y As Integer
Dim i As Integer
Dim s As Long
Dim c As String
Dim SecNo As String
For s = 2 To 3
SecNo = s + c
For iRow = 1 To 303 'SKIP first row when Reading and Writing - ALWAYS COLUMN HEADINGS
Application.StatusBar = "SecNo = " & SecNo & " iRow = " & iRow & " iRow, 6 = " & Worksheets("PARTS").Cells(iRow, 6)
If Worksheets("PARTS").Cells(iRow, 6).Text = SecNo And Worksheets("PARTS").Cells(iRow, 9).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
If Worksheets("PARTS").Cells(iRow, 7).Text = SecNo And Worksheets("PARTS").Cells(iRow, 9).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
If Worksheets("PARTS").Cells(iRow, 8).Text = SecNo And Worksheets("PARTS").Cells(iRow, 8).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
Next iRow
Next s
Worksheets("Sec2").Select
Worksheets("Sec2").Range("Print_area").Sort _
Key1:=Worksheets("PARTS - By Section and DESC").Range("D1"), _
Key2:=Worksheets("PARTS - By Section and DESC").Range("A1")
End Sub
-This routine first deletes all worksheets named SEC1 through SEC200 - this works fine
-then recreates 200 new worksheets named SEC1 through SEC200 - this works fine. I always write new data to all worksheets SEC1 through SEC200 and found that trying to clear 200 worksheets took a long time so I just delete them them and re-add them. - Again this works fine
-then the macro goes through a list looking for records with 1.01 and writes all records associated to 1.01 to Sec1 - this works
-then the macro increments to 2.01 and then looks for all records associated to 2.01 and writes them to Sec2 - THIS DOES NOT WORK. The macro increments to 2.01, searches through my file and finds records associated to 2.01 but will not write the data to SEC2. If I tell the macro that the first section is 2.01 then it writes fine to SEC2 but then increments but won't write to 3.01. It seems that the macro writes to the first section but no others and I sense I might have to release control of Sec1 before moving to Sec2. Please take a look through my code and see if you can help me out. I really don't want to re-write too much so please be kind with my coding. Thanks in advance.
Sub PARTS_To_Sections()
Dim Count As Long
For Count = 1 To 200
Application.DisplayAlerts = False
Sheets("Sec" & Count).Delete
Application.DisplayAlerts = True
Next Count
For Count = 1 To 200
Worksheets.Add().Name = "Sec" & Count
Next Count
Dim iRow As Integer
Dim y As Integer
Dim i As Integer
Dim s As Long
Dim c As String
Dim SecNo As String
For s = 2 To 3
SecNo = s + c
For iRow = 1 To 303 'SKIP first row when Reading and Writing - ALWAYS COLUMN HEADINGS
Application.StatusBar = "SecNo = " & SecNo & " iRow = " & iRow & " iRow, 6 = " & Worksheets("PARTS").Cells(iRow, 6)
If Worksheets("PARTS").Cells(iRow, 6).Text = SecNo And Worksheets("PARTS").Cells(iRow, 9).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
If Worksheets("PARTS").Cells(iRow, 7).Text = SecNo And Worksheets("PARTS").Cells(iRow, 9).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
If Worksheets("PARTS").Cells(iRow, 8).Text = SecNo And Worksheets("PARTS").Cells(iRow, 8).Text = "Y" _
Then
With Worksheets("Sec" & s)
.Cells(i, 1) = Worksheets("PARTS").Cells(iRow, 2)
.Cells(i, 2) = Worksheets("PARTS").Cells(iRow, 3)
.Cells(i, 3) = Worksheets("PARTS").Cells(iRow, 4)
End With
i = i + 1
End If
Next iRow
Next s
Worksheets("Sec2").Select
Worksheets("Sec2").Range("Print_area").Sort _
Key1:=Worksheets("PARTS - By Section and DESC").Range("D1"), _
Key2:=Worksheets("PARTS - By Section and DESC").Range("A1")
End Sub