简体   繁体   中英

insert value to next cell using vba

I am trying to fetch data from a webpage. My VBA code is as below

m = 0
For Each htmlele1 In doc.getElementsByClassName("resultsbranch")
    m = m + 1
    companyname = htmlele1.getElementsByTagName("h2")
    Address = htmlele1.getElementsByTagName("span")
    If Address.getAttribute("itemprop") = "myaddress" Then
        Range("D" & i).Value = companyname.innerText + "," + Address.innerText
    End If
    Teliphone = htmlele1.getElementsByClassName("teldata")
    If Teliphone.getAttribute("itemprop") = "tel" Then
        Range("E" & i).Value = Teliphone.innerText
    End If
    'i = i + 1
    'Debug.Print i
Next

On the first iteration, values are get inserted to columns D,E
on second iteration I want to insert data To F,H .
On 3 rd iteration I,J
On 4th iteration K,L So on up to nth iteration

How can i do this ?

Instead of:

Range("D" & i).Value
Range("E" & i).Value

Use:

Cells(i, (m*2 + 3)).Value
Cells(i, (m*2 + 4)).Value

Or use another counter... As you like... Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM