For Loop Not Performing Its Duty

Hmerman

Board Regular
Joined
Oct 2, 2016
Messages
102
Hello,
Hope you are well.

I have a loop that should place data from Sheet18 to Sheet11 from a Userform showing on Sheet1:
If a Column 2 value on Sheet18 is equal to a textbox value similar to FDS002/18 AND
a Column 6 value on Sheet18 is equal to a combobox value eg SF AND
a Column 10 value on Sheet18 equals a value in a combobox eg WP.

But for some reason which I cannot figure out in the last 15 hours, it does not run. I have tried placing the If values on the spreadsheet and run the logical If from a module with f8, but the loop runs as if the above values does not exist on Sheet18, but they do. At best it only places one line on Sheet11.

I removed the logic and ran the loop and only one line was placed on Sheet11 (not the same as above). I also replaced the forward slash with an underscore in the first logical If question; reversed the logic by asking whether the textboxes and comboboxes is equal to Sheet18 values; tried differing methods of referencing ranges and I rewrote the code using the Application.Match function, but again no joy.

Can someone please point me in the right direction or give advice?

My code as is now:

Code:
Dim lastCel As Long, x As Long
    Dim openRow As Long
    
    Application.ScreenUpdating = False
    
    With Sheet18
            openRow = Sheet11.Range("A" & Rows.Count).End(xlUp).row
               lastCel = .Range("B" & Rows.Count).End(xlUp).row
            For x = 2 To lastCel
                   If .Cells(x, 2).Value = txbVragVerwysing.Text And .Cells(x, 6) _
                 .Value = cbSundrVerdPlaas.Value And .Cells(x, 10).Value = cbSundrVerdDroog.Value Then

                    Sheet11.Range("A" & openRow).Value = .Cells(x, 6).Value
                    Sheet11.Range("A" & openRow).Offset(0, 2).Value = .Cells(x, 7).Value
                   Sheet11.Range("A" & openRow).Offset(0, 3).Value = .Cells(x, 10).Value
                    Sheet11.Range("A" & openRow).Offset(0, 4).Value = .Cells(x, 9).Value
                    Sheet11.Range("A" & openRow).Offset(0, 5).Value = .Cells(x, 21).Value
    
            End If
         
            Next x
        
    End With
    Application.ScreenUpdating = True

Regards
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Update:

I think I have found what is wrong with my code, I am not incrementing the openRow Range variable correctly. So it is overwriting the values until the last line. That why I only see one line of transferred data.:banghead: I hope.

Does someone have a suggestion?
 
Upvote 0
Try adding
Code:
openRow=openRow+1
Just before the End If
 
Upvote 0
:laugh::grin: Fluff, brevity is the soul of wit and you have tons of it.:pray:

Thank you. Your suggestion solved it and brought me a sense of relief.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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