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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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