-
Solved: Compare date before event execution?
Hi Guys. I hope someone can help me with this problem. i have been trying to figure it out for the last 2 days.
I have a macro that will pull a table from a website and paste it on a worksheet. This website updates the table weekly, dropping the earliest week's data with each update. I need to maintain the dropped data, so the macro finds the last column,moves back and places the data where I need it to go, then just deletes the first row of the table. This all works fine.
The problem I have is that if I run the macro several times before the new update (which will be done because other people use the file and the macro actually pulls several tables), I will continue to pile on data that I already have. I've been trying to do an If Then statement that will compare the date to a date cell on the spreadsheet and won't pull the data if the data is aleready there. I can't get that to work and wonder if there's a better way to do the comparison?
Here's the code I have that works. Can someone help solve the date comparison?
[vba]Sub PullWeb()
'Pull & post energy data from website
Dim sTS6 As String
sTS6 = Range("a1").Value
With ActiveSheet.QueryTables.Add(Connection:= _
"URL:HAD TO REMOVE DUE TO FORUM POSTING RULES" _
& sTS6, Destination:=Range("A8").End(xlToRight).Offset(-3, -2))
.Name = "Gasoline"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "6"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
'Delete 1st column of new data
Dim MyCell As Long
Dim i As Long
For i = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
If Cells(1, i).Value = "Data" Then Cells(1, i).EntireColumn.Delete
Next i
For MyCell = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
If Application.WorksheetFunction.CountIf(Columns(MyCell), "U.S.") >= 1 _
Then Columns(MyCell).EntireColumn.Delete
Next MyCell
End Sub
[/vba]
The URL above has been removed because I had too few posts. The sample workbook I have attached has the code so you can see what I'm talking about.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules