Hi protonLeah,
I tried this more or less, but got the same error. The modification I had to make was because the the Next Z statement actually needs to be outside the With statement. Perhaps I should have done this earlier, but here is the code in it's entirety:
Sub GetFinvizData()
Dim Z As Integer
Dim strFin(1 To 8) As String
strFin(1) = "basicmaterials"
strFin(2) = "consumergoods"
strFin(3) = "financial"
strFin(4) = "healthcare"
strFin(5) = "industrialgoods"
strFin(6) = "services"
strFin(7) = "technology"
strFin(8) = "utilities"
Dim RowCt As Integer
Dim RowTot As Variant
For Z = 1 To 8
'Delete existing data
Sheets("Raw").Activate
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'Download Sector Stock Data
strFin(Z) = "http://finviz.com/export.ashx?v=151&f=sec_" & strFin(Z) & ",sh_avgvol_o500,sh_opt_optionshort,sh_price_o10&ft=4&o=industry&c=1,2,4,42,43,44,45,46,47,65,68"
QueryQuote:
With Sheets("Raw").QueryTables.Add(Connection:="URL;" & strFin(Z), Destination:=Sheets("Raw").Range("a1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Sheets("Raw").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("Raw").Range("a1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, other:=True, OtherChar:=",", FieldInfo:=Array(1, 2)
Sheets("Raw").Columns("A:B").ColumnWidth = 12
Range("A1").Select
'Filters bullish potential trades based on:
'-Week and month performance positive
'-Half performance negative
With Sheets("Raw")
RowTot = .UsedRange.Rows.Count
For RowCt = 2 To RowTot
If (.Cells(RowCt, 7).Value < 0 And .Cells(RowCt, 5).Value > 0 And .Cells(RowCt, 4).Value > 0) Then
.Range("A" & RowCt & ":" & "K" & RowCt).Copy _
Destination:=Sheets(strFin(Z)).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next RowCt
End With
Next Z
End Sub
I think the With statement is the way to go, but it resulted in the same error when the Next Z was outside of the last With. Again the query works fine. The idea is that "Raw" is the dump sheet for the data before filtering. Once the filter is complete, then "Raw" is cleared and the next set of data is brought in and will run through the same filter and placed on the sheet with the sector name. Ultimately there will be approximately 6 different filters run on the data before I get to the Next Z. I have not written the other filters yet, since I think if I can get one to work the others will follow suit.
Thanks.
Bookmarks