Skipping the line when sheet is changed in a for-loop

aoktaykoc

New Member
Joined
Mar 8, 2018
Messages
1
Hello all,
I have a multisheet-workbook. In a for loop including if statement and an increment, I visit all the sheets starting from the third one, the code works for the first sheet but for the second sheet, the counter is not incremented.
Could you please help me to solve this problem?

Private Sub CommandButton18_Click()
Dim LastRow As Long
Dim satir_start As Long
Dim sheet As Worksheet
Dim mycell As Range
Dim r As Integer
Dim counter As Integer
Dim k As Integer
Dim i As Integer
Dim p As Integer




Dim haftalık_ders_saati As Integer


For prg = 3 To Sheets.Count
Sheets(prg).Select


Range("a1").Select


'sınıfın toplam ders sayısını hesaplıyor
For strp = 2 To 35
If Cells(strp, 5) <> "" Then
satir = satir + 1
End If
Next strp


kacinci_satir = satir




'öğretmenin programını sınıfın programı ile örtüştürüyor














'***********************************************
For pn = 2 To kacinci_satir + 1


For clr = 1 To 40
Cells(clr, 3) = " "
Next clr


haftalık_ders_saati = Cells(pn, 7)


ogretmen = Cells(pn, 8)
Set mycell = Sayfa1.Range("all_teachers").Find(what:=ogretmen, LookIn:=xlValues)
ogretmen_r = mycell.Row
ogretmen_c = mycell.Column






Sheets(prg).Select


Range("a1").Select


If haftalık_ders_saati >= 5 Then 'DERS SAATİ = 5


For p = 1 To 40
Cells(p, 2).Copy Cells(p, 3)
If IsEmpty(Cells(p, 2)) Then
Cells(p, pn + 20).Copy Cells(p, 3)
End If
Next p

For yh = 1 To 5
counter = 0
For i = 8 * yh - 7 To 8 * yh
If Cells(i, 3).Value = "" Then
Cells(i, 18).Value = ""
End If
Next i
'j= boş olan derslerin sayısı
toplam = Application.WorksheetFunction.CountIf(Range("r1:r" & i), 1)

k = 0

For t = 8 * yh - 7 To 8 * yh
If IsEmpty(Sheets(prg).Cells(t, 3)) Then
k = k + 1
For p = k To counter
Cells(p, 4).Value = Sheets(prg).Cells(t, 1).Value
Exit For

Next p
End If
Next t




randomnumber = Int((toplam - 1 + 1) * Rnd + 1)
r = Cells(randomnumber, 4).Value
Cells(r, 2).Value = Cells(pn, 13).Value
Cells(r, 20 + pn).Value = ActiveSheet.Name
Sayfa1.Cells(ogretmen_r, r + 47) = Cells(r, 20 + pn).Value

Next yh
Cells(pn, 14) = Application.WorksheetFunction.CountIf(Range("b1:b40"), Cells(pn, 13))



'6. veya 7. dersleri ekliyor.


For p = 1 To 40
Cells(p, 2).Copy Cells(p, 3)
If IsEmpty(Cells(p, 2)) Then
Cells(p, pn + 20).Copy Cells(p, 3)
End If
Next p






For ph = 1 To haftalık_ders_saati - 5
counter = 0

'j= boş olan derslerin sayısı
For cr = 1 To 40
If IsEmpty(ActiveSheet.Cells(cr, 3)) Then
counter = counter + 1
End If
Next cr

k = 0
For t = 1 To 40
If IsEmpty(Cells(t, 3)) Then
k = k + 1
For p = k To counter
Cells(p, 4).Value = Cells(t, 1).Value
Exit For

Next p
End If
Next t

randomnumber = Int((counter - 1 + 1) * Rnd + 1)

r = Cells(randomnumber, 4).Value
Cells(r, 2).Value = Cells(pn, 13)
Cells(r, 20 + pn).Value = ActiveSheet.Name
Sayfa1.Cells(ogretmen_r, r + 47) = Cells(r, 20 + pn).Value

Next ph


Cells(pn, 14) = Application.WorksheetFunction.CountIf(Range("b1:b40"), Cells(pn, 13))


'*************************************** YERLEŞMEMİŞ DERSİ EKLİYOR


If Cells(pn, 14) <> Cells(pn, 7) Then


yeniden_ata = Cells(pn, 7) - Cells(pn, 14)


For p = 1 To 40
Cells(p, 2).Copy Cells(p, 3)
If IsEmpty(Cells(p, 2)) Then
Cells(p, pn + 20).Copy Cells(p, 3)
End If
Next p


For ph = 1 To yeniden_ata
counter = 0

'j= boş olan derslerin sayısı
For cr = 1 To 40
If IsEmpty(ActiveSheet.Cells(cr, 3)) Then
counter = counter + 1
End If
Next cr

k = 0
For t = 1 To 40
If IsEmpty(Cells(t, 3)) Then
k = k + 1
For p = k To counter
Cells(p, 4).Value = Cells(t, 1).Value
Exit For

Next p
End If
Next t

randomnumber = Int((counter - 1 + 1) * Rnd + 1)
r = Cells(randomnumber, 4).Value
Cells(r, 2).Value = Cells(pn, 13)
Cells(r, 20 + pn).Value = ActiveSheet.Name
Sayfa1.Cells(ogretmen_r, r + 47) = Cells(r, 20 + pn).Value

Next ph
End If
Cells(pn, 14) = Application.WorksheetFunction.CountIf(Range("b1:b40"), Cells(pn, 13))
'***************************************


End If
Next pn


Next prg






End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top