Hi Guys.
I've been creating a macro for my (small) investment team, but I'm struggeling to get the final touch to work. The macro just updates certain cells which contains a function that get (almost) real-time quotes from Yahoo finance. When the cells is updated the macro prints the total portfolio value to another sheet along with date and time. Simple as that. The problem is that some of my stocks is bought in another stock exchange which closes at another time of the day. In other words, I have to create a macro that automatically activates the above-mentioned macro at a certain time of the day (23.59). I've tried alot and my macro ONLY works if i press F5 before i leave work, but I want this update to happend every night without me interfering each day. Below you can se all the codes (ps. if you have any tips to make it better I would appriciate it).
Sub knappen()
Workbooks("Valuation (test)").Activate
Sheets("MLC").Select
Range("CADNOK").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""CADNOK=x"")"
Range("USDNO").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""USDNOK=x"")"
Range("SEKNOK").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""SEKNOK=x"")"
Range("Axactor").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""axa.ol"")"
Range("Bakkafrost").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""bakka.ol"")"
Range("DNO").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""dno.ol"")"
Range("Golden").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""gogl.ol"")"
Range("GoldenReg").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""gogl-r.ol"")"
Range("Kinross").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""k.to"")"
Range("Nel").Select
ActiveCell.FormulaR1C1 = "=StockQuote(""nel.ol"")"
End Sub
Dim req_value As Variant
Private Sub Worksheet_Activate()
req_value = [Markedsverdi]
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Integer
If [Markedsverdi] <> req_value Then
With Worksheets("Avkastning")
lastRow = .Range("A1048576").End(xlUp).Row
For X = 1 To lastRow
If IsDate(.Range("A" & X).Value) Then
If CDate(.Range("A" & X).Value) = Date Then Exit For
End If
Next
.Range("B" & X).Value = Range("Markedsverdi").Value
.Range("A" & X).Value = Date
.Range("D" & X).Value = Time
End With
End If
End Sub
Private Sub Workbook_Open()
If Weekday(Now, vbMonday) < 6 Then
Application.OnTime VBA.TimeValue("23:59:00"), "knappen"
End If
End Sub
I have never written a macro before, so the macro above is creatied through googling and trying/failing.
Best regards
Thomas Mangor-Jensen
Bookmarks