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
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