VBA Paste Loop Question

JXI115

New Member
Joined
Aug 11, 2016
Messages
5
Hello, I am currently using the following code to pull in data from various websites.

Sub Macro3()
'
' Macro3 Macro
'
'

Dim Erw, Frw, Lrw
Frw = 1
Lrw = Range("A" & Rows.Count).End(xlUp).Row
For Erw = Frw To Lrw


With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & Range("A" & Erw).Value, Destination:=Range("B" & Erw))
.Name = ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = x1InsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Next Erw


End Sub

I have all web address' on column A, and the code looks at each link then pulls the 20 rows of data I need from the websites.
ex.
Stock Screener - Custom
Stock Screener - Custom

The problem I have is that the code will paste into cell B1, then paste into cell B2 etc. Considering the data I am importing is 20 rows long, I need the code to paste to every 20 rows in column B.

I can't figure out how to do that.

Thanks for anyone that can help.
 

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.
Can you not just do
Code:
For Erw = Frw To Lrw Step 20
?
 
Upvote 0
Thanks for your help. I just tried it but by adding step 20; now it only pulls from the first website link.
 
Upvote 0
According to research I have done Step 20 does sound like it would be the correct thing to add. Is there anywhere else I could add it where it might work?Thanks
 
Upvote 0
Why would you want to add it anywhere else :confused: where you have the For statement is the correct place.

Btw

Code:
Dim Erw, Frw, Lrw
should be
Code:
Dim Erw As Long, Frw As Long, Lrw As Long
 
Last edited:
Upvote 0
Oh! Sorry about that. I was just guessing if it would work anywhere else. I corrected the first part of the code and added "As Long". I honestly cannot figure out what to do to get this code to work.
 
Upvote 0
figure out what to do to get this code to work.

Post the code in the thread with the amendments you have made (please put it in code tags as well. Just paste the code in the thread then select all the code and click the # icon).
 
Upvote 0
I haven't made any amendments. I am completely stuck. I am very new to VBA and I cannot even think of what else I could do to solve the problem.
 
Upvote 0
I haven't made any amendments.
You obviously have made amendments because you wrote...
I corrected the first part of the code and added "As Long".

Your code should look like
Rich (BB code):
Sub Macro3()

    Dim Erw As Long, Frw As Long, Lrw As Long
    Frw = 1
    Lrw = Range("A" & Rows.Count).End(xlUp).Row

    For Erw = Frw To Lrw Step 20

        With ActiveSheet.QueryTables.Add(Connection:= _
                                         "URL;" & Range("A" & Erw).Value, Destination:=Range("B" & Erw))
            .Name = ""
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = x1InsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "10"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With

    Next Erw

End Sub

or more likely...

Rich (BB code):
Sub Macro3b()

    Dim Erw As Long, Frw As Long, Lrw As Long, Erw1 As Long
    Frw = 1
    Erw1 = 1
    Lrw = Range("A" & Rows.Count).End(xlUp).Row

    For Erw = Frw To Lrw

        With ActiveSheet.QueryTables.Add(Connection:= _
                                         "URL;" & Range("A" & Erw).Value, Destination:=Range("B" & Erw1))
            .Name = ""
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = x1InsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "10"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
        Erw1 = Erw1 + 20
    Next Erw

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,774
Messages
6,132,649
Members
449,740
Latest member
Stevejhonsy

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