حلقه فور در حلقه فور ایچ

Collapse
X
 
  • زمان
  • نمایش
حذف همه
new posts
  • Amirabassmoradi
    • 2018/07/18
    • 2

    حلقه فور در حلقه فور ایچ

    سلام بر اساتید محترم
    لطفا راهنمایی فرمایید که ایراد کد زیر کجاست و چرا جواب نمیدهد
    ممنون از وقتی که میزارید

    کد:
    For each c in sheet1.range("a1:a10")
    
    For i = 1 To 
        If c.Value <> "" Then
            ListBox2.AddItem c.Offset(0, 3).Value
            ListBox2.List(ListBox2.ListCount - 1, 1) = c.Offset(0, 4).Value
            ListBox2.List(ListBox2.ListCount - 1, 2) = c.Offset(0, 5).Value
            ListBox2.List(ListBox2.ListCount - 1, 3) = i
            ListBox2.ColumnCount = 4
            ;90 ListBox2.ColumnWidths = "200;180;90"
     Exit For
        End If
        Next i
        Next
    Last edited by Amir Ghasemiyan; 2018/07/18, 15:39. دلیل: قرار دادن كدها داخل تگ كد
  • Amir Ghasemiyan

    • 2013/09/20
    • 4598
    • 100.00

    #2
    سلام دوست عزيز

    رديف هايي كه قرمز ميكنم اشتباه نوشته شده. طبق نياز اصلاح كنيد

    کد:
    For each c in sheet1.range("a1:a10")
    
    [COLOR="#FF0000"]For i = 1 To 
        If c.Value <> "" Then
            ListBox2.AddItem c.Offset(0, 3).Value
            ListBox2.List(ListBox2.ListCount - 1, 1) = c.Offset(0, 4).Value
            ListBox2.List(ListBox2.ListCount - 1, 2) = c.Offset(0, 5).Value
            ListBox2.List(ListBox2.ListCount - 1, 3) = i
            ListBox2.ColumnCount = 4
            [COLOR="#FF0000"];90 ListBox2.ColumnWidths = "200;180;90"[/COLOR]
     Exit For
        End If
        Next i
        Next
    مورد اول گفته اين حلقه رو از 1 تا ... ادامه بده. شما عدد دوم رو وارد نكرديد
    مورد دوم هم ;90 اضافي هست. بايد حذف بشه

    اگر باز هم موردي باشه بايد فايل نمونه بذاريد تا علتشو بتونم بگم

    [/COLOR]

    کامنت

    • Amirabassmoradi
      • 2018/07/18
      • 2

      #3
      فایل نمونه
      فایل های پیوست شده

      کامنت

      • Amir Ghasemiyan

        • 2013/09/20
        • 4598
        • 100.00

        #4
        من كه متوجه نشدم چرا از حلقه for داخل يك حلقه for each استفاده كرديد. به هر حال من اين كد رو به اين صورت اصلاح كردم. اميدوارم مطابق جواب مورد نظرتون باشه

        کد:
        Private Sub CommandButton1_Click()
        ListBox1.ColumnCount = 4
        ListBox1.ColumnWidths = "50;150;150;50"
        i = 1
        For Each c In Sheet1.Range("d2:d" & Application.WorksheetFunction.CountA(Sheet1.Range("a:a")))
            If c.Value <> "" Then
                ListBox1.AddItem c.Offset(0, -1).Value
                ListBox1.List(ListBox1.ListCount - 1, 1) = c.Offset(0, -2).Value
                ListBox1.List(ListBox1.ListCount - 1, 2) = c.Offset(0, -3).Value
                ListBox1.List(ListBox1.ListCount - 1, 3) = i
                i = i + 1
            End If
        Next c
        End Sub

        کامنت

        چند لحظه..