This is my code I made that extracts data from a .csv file stored on the web. My sheet called 'New Data' stores this data for 2 days. I store 2 days worth of data so that when I run it next time, it pulls the data from the web, compares to the data I pulled yesterday, and then places any of the data that doesn't match (wasn't there yesterday but is now there today) into a new column.
I essentially only actually need the new data, but am stuck storing 2 day's worth of about 4000 lines of data each. My file is extremely large and takes way too long to run (this has alot to do with my poor vba code). If someone could please help me cleanup my code (i.e. remove any unneccessary lines I know I have in there) I would be very greatful!
Option Explicit
Sub FindNewIssues()
'for efficiency
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
'first checks to see if the data is already up-to-date
'continues with further commands only if data NOT up-to-date
If Worksheets("New Data").Range("A10") <> Worksheets("New Data").Range("B2") Then
'copy the old content to a new location
'this will allow us to compare the data from last night (new data) to two days ago (old data)
Sheets("New Data").Range("A9:I10000").Cut Destination:=Sheets("New Data").Range("K9")
'then import the new data from external database
With Worksheets("New Data").QueryTables.Add(Connection:= _
"TEXT;https://(the URL that I can't show).csv", Destination:=Range( _
"$A$9"))
.Name = "Closes"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(3, 2, 2, 2, 2, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Application.Calculate
'next need to check to see if there are any new issues
'this is done by looking for errors (non-matches) from a vlookup between the new and old data
Range("T10:W10000").ClearContents
Range("T10").Formula = "=IF(IF(ISERROR(VLOOKUP('New Data'!E10,'New Data'! $O$10:$S$10000,1,FALSE))=TRUE,'New Data'!E10,"""")=0,"""",IF(ISERROR (VLOOKUP('New Data'!E10,'New Data'!$O$10:$S$10000,1,FALSE))=TRUE,'New Data'! E10,""""))"
Range("U10").Formula = "=IF(IF(ISERROR(VLOOKUP('New Data'!B10,'New Data'! $L$10:$S$10000,1,FALSE))=TRUE,'New Data'!B10,"""")=0,"""",IF(ISERROR (VLOOKUP('New Data'!B10,'New Data'!$L$10:$S$10000,1,FALSE))=TRUE,'New Data'! B10,""""))"
Range("T10:U10").AutoFill Destination:=Range("T10:U10000"), Type:=xlFillDefault
Application.Calculate
'these will get placed in a new column and considered our new issues (because they didn't exist before)
Range("T9:U10000").Select
Selection.AutoFilter
ActiveSheet.Range("$T$9:$U$10000").AutoFilter Field:=1, Criteria1:="<>"
Worksheets("New Data").Range("T10:U10", Sheets("New Data").Range ("T10").End(xlDown)).Copy
Range("V10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Selection.AutoFilter
Application.Calculate
'this if statement is just so I can store the new issues as text or else they'd clear every day
'this also takes into account the chance of there not being any new issues
If Worksheets("New Data").Range("V10").Value = "" Then
Range("V10").Value = ""
Else
If Worksheets("New Data").Range("X10") = "" Then
Worksheets("New Data").Range("V10:W10", Sheets("New Data").Range ("V10").End(xlDown)).Copy
Range("X10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Else
Worksheets("New Data").Range("V10:W10", Sheets("New Data").Range ("V10").End(xlDown)).Copy
Range("X9").End(xlDown).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End If
End If
Application.CutCopyMode = False
'need to send these new issues to their appropriate sheets
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End If
End Sub