Paste on next blank row

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,066
Office Version
  1. 2016
Platform
  1. Windows
Hi

How do I get this code to paste data on the next blank row, in sheet1 column A

Sheets("Sheet1").Range("A" & i).Value = IE.Document.getElementsByTagName("a").Item(i).innerText
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Code:
Dim i as long
i = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Sheet1").Range("A" & i).Value = IE.Document.getElementsByTagName("a").Item(i).innerText
 
Upvote 0
Hi Paul

Im getting this error message Duplicate declaration in current scope, the error is shown in red below, I thought it was the bit in Blue causing the error, I took it out still having the problem. This is the last bit I am stuck on could you please tweak this for me. Your code is below in green and red.

For info, some rows may not have data in them, e.g row 1,2,3 has data, 4 is empty, 5,6, has data, its just the way my code pastes the data in excel, this will change every time. So not sure if your code is looking for last row withot data or FIRST BLANK ROW.


Full Code
Private Sub CommandButton2_Click()

Dim i, j, k, l As Integer

i = 2
k = 2
l = 2

'SHEET2 as sheet with URL
Dim wsSheet As Worksheet, Rows As Long, links As Variant, IE As Object, link As Variant
Set wb = ThisWorkbook
Set wsSheet = wb.Sheets("Sheet2")

'Set IE = InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
Rows = wsSheet.Cells(wsSheet.Rows.Count, "A").End(xlUp).Row
links = wsSheet.Range("A1:A" & Rows)

'IE Open Time per page 5sec and check links on Sheet2 Column A
With IE
.Visible = True
Application.Wait (Now + TimeValue("00:00:5"))

For Each link In links
.navigate (link)
While .Busy Or .ReadyState <> 4: DoEvents: Wend

For i = 0 To 500
On Error Resume Next

'Paste in sheet and column

Dim i As Long
i = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets("Sheet1").Range("A" & i).Value = IE.Document.getElementsByTagName("a").Item(i).innerText


Next i

'Deletes duplicates in column A Sheet1
Columns(1).RemoveDuplicates Columns:=Array(1)

'navigate links
Next link

End With

'Close IE Browser
IE.Quit
Set IE = Nothing

End Sub
 
Upvote 0
You didn't post all the code before so I didn't know you'd already dimmed it, or that it was already in a loop!!

Code:
    For i = 2 To 500
        If Sheets("Sheet1").Range("A" & i) <> "" Then GoTo Miss
        Sheets("Sheet1").Range("A" & i).Value = ie.Document.getElementsByTagName("a").Item(i).innerText
Miss:
    Next i
 
Last edited:
Upvote 0
If you are adding rows to this list every time then you should start your loop from the 1st empty row and use Do-Loop Until rather than a For-Next (otherwise you will run out of rows after 500!).
 
Upvote 0
Paul this is really good, the only problem i'm having is that its not pulling all the URL from the webpage that it opens, it skips some not sure why

if only this is run
Sheets("Sheet1").Range("A" & i).Value = IE.Document.getElementsByTagName("a").Item(i).innerText

then it get all the url,

put if i run your code it does not pull all the url
 
Last edited:
Upvote 0
Try this:

Code:
Private Sub CommandButton2_Click()
    Dim i, j, k, l As Integer
    i = 2
    k = 2
    l = 2
'SHEET2 as sheet with URL
    Dim wsSheet As Worksheet, Rows As Long, links As Variant, IE As Object, link As Variant
    Set wb = ThisWorkbook
    Set wsSheet = wb.Sheets("Sheet2")


'Set IE = InternetExplorer
    Set IE = CreateObject("InternetExplorer.Application")
    Rows = wsSheet.Cells(wsSheet.Rows.Count, "A").End(xlUp).Row
    links = wsSheet.Range("A1:A" & Rows)


'IE Open Time per page 5sec and check links on Sheet2 Column A
    With IE
        .Visible = True
        Application.Wait (Now + TimeValue("00:00:5"))


        For Each link In links
            .navigate (link)
            While .Busy Or .ReadyState <> 4: DoEvents: Wend


            For i = 0 To 500
                On Error Resume Next


'Paste in sheet and column


                Dim rw As Long
                rw = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1
                Sheets("Sheet1").Range("A" & rw).Value = IE.Document.getElementsByTagName("a").Item(i).innerText




            Next i


'Deletes duplicates in column A Sheet1
            Columns(1).RemoveDuplicates Columns:=Array(1)


'navigate links
        Next link


    End With


'Close IE Browser
    IE.Quit
    Set IE = Nothing


End Sub
 
Upvote 0
Paul

Its giving me an invalid qualifier error message rw = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1

The first code seem fine if the URL are different, however if the web page is the same, just the data is diffrent then it does not copy alll the urls, is this because of
IE.Document.getElementsByTagNames

e.g if url are

https://www.bbc.co.uk/
https://www.aljazeera.com/

it seems to work fine as far as i can tell

if the url are
https://www.bbc.co.uk/1234/john

https://www.bbc.co.uk/3456/jane

Only the data is different then it does not seem to pull all the url, I though the issue might be due to getElementsByTagName

The website may be the same page, just different data

<tbody>
</tbody>
 
Last edited:
Upvote 0
It shouldn't do unless you have changed the code name of Sheet1, try this instead:

Code:
        rw = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1

The url's I don't think I can help with sorry.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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