I am using the sub below to copy stock/commodity prices from one
worksheet to another:
Sub CopyPrices()
Dim strdate As String
Dim rCell As Range
Worksheets("P1").Select
strdate = Range("AL5").Value
strdate = Format(strdate, "Short Date")
Range("AL4").Select
Range("AL4:AQ9").Find(What:="Last", After:=ActiveCell,
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate
Range(ActiveCell.Offset(1, -3), ActiveCell.Offset(5, 0)).Select
Selection.Copy
Sheets("Db1").Select
Columns("FN:FN").Select
Set rCell = Selection.Find(What:=CDate(strdate), After:=Range("FN5"),
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False)
rCell.Select
Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 1)).Select
End Sub
where worksheet P1 imports the prices via web query and worksheet Db1
is a database that needs to be updated. The format of the date in Db1
is 21/01/2008 (21st Jan 2008). A problem arises, however, because the
date in the web query table is sometimes of the above format, and
sometimes in the format 01/21/2008. When in the latter format, the sub
fails with "Run-time error '91', Object variable or With Block
variable not set" or selects the wrong date in those cases where the
latter transposed format is also a valid date e.g. 06/07/2008 (6th
July 2008) being confused with 07/06/2008 (7th June 2008). How can I
modify the sub to cope with this problem of variable date formats?