Excel cycle through column - for loop problem

pecsenye

New Member
Joined
Jan 17, 2023
Messages
7
Hello everyone,
I have a problem with the following: excel cycle through column.

This code lists the data in 15 columns:

VBA Code:
Set divs = WPage.FindElementsByClass("xxx")
For i = 1 To 15
Worksheets("test").Range("C2").Columns(i).Value = divs(i).Text
Next

I would like to open new rows after 5 datas.
Without "for" loop the code is:

Code:
Set divs = WPage.FindElementsByClass("xxx")
For i = 1 To 15
    Worksheets("test").Range("C2").Value = divs(1).Text
    Worksheets("test").Range("D2").Value = divs(2).Text
    Worksheets("test").Range("E2").Value = divs(3).Text
    Worksheets("test").Range("F2").Value = divs(4).Text
    Worksheets("test").Range("G2").Value = divs(5).Text
    Worksheets("test").Range("H2").Value = divs(6).Text
    
    Worksheets("test").Range("C3").Value = divs(7).Text
    Worksheets("test").Range("D3").Value = divs(8).Text
    Worksheets("test").Range("E3").Value = divs(9).Text
    Worksheets("test").Range("F3").Value = divs(10).Text
    Worksheets("test").Range("G3").Value = divs(11).Text
    Worksheets("test").Range("H3").Value = divs(12).Text
    
    Worksheets("test").Range("C4").Value = divs(13).Text
    Worksheets("test").Range("D4").Value = divs(14).Text
    Worksheets("test").Range("E4").Value = divs(15).Text
    Worksheets("test").Range("F4").Value = divs(16).Text
    Worksheets("test").Range("G4").Value = divs(17).Text
    Worksheets("test").Range("H4").Value = divs(18).Text

I would like to do with "for loop".
Thanks for helping!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
You could test following:
VBA Code:
Sub Test()
Dim i As Long, lrow As Long, lcol As Long
    For i = 1 To 18
        lrow = Int(Int(i - 1) / 6) + 2
        lcol = ((i - 1) Mod 6) + 3
        Worksheets("test").Cells(lrow, lcol).Value = divs(i).Text
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,529
Messages
6,125,345
Members
449,220
Latest member
Edwin_SVRZ

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