I recently posted a thread with the same name. The reason why I'm using the same name again, is because I didn't know how to "UNSOLVE" the other thread. Well enough about that.
For you that remembers my last thread, I needed to create a macro that opened a internet site, inserted username and password and extracted the stockprices from my portefolio.
Further I needed the macro to automatically get the stock prices each night at 23.59 without doing anything. It does the job, but now I've encountered a new problem.
I use static references which means that the macro is "screwed up" everytime I buy or sell a stock, so I tried to use the loop formula, but I can't get it to work. Is there another way to solve this problem or am I using the loop formula in a wrong way? Thank You very much for all responses.
As you can see from the code below I dim 5 variables (which means I can only have 5 stocks in my portfolio before I have to write more code). I figured it should be a way to loop between the "tr's" and get the right values no matter how many stocks I buy or sell.
Sub LoginNordnet()
'Range("C3:F7").Clear
'Inn med navnet på aksjene
'Range("C2").Value = "Kurs"
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate "https://www.nordnet.no/mux/login/startNO.html?cmpi=start-loggain"
Do Until .ReadyState = 4
DoEvents
Loop
.Document.all.Item("input1").Value = "XXXX"
.Document.all.Item("pContent").Value = "XXXX"
.Document.all.Item("login_btn").Click
Application.Wait DateAdd("s", 2, Now)
.Navigate "https://www.nordnet.no/mux/web/depa/mindepa/depaoversikt.html"
'Vent til eksplorer har loadet ferdig
Do Until .ReadyState = 4
DoEvents
Loop
Dim dd As Single, de As Single, df As Single, dg As Single, dh As Single
'Hent og lagre kursen (celle 6 i tabbellen "tr1" -> "tr5")
dd = IE.Document.getElementById("tr1").Cells(6).innerText
de = IE.Document.getElementById("tr2").Cells(6).innerText
df = IE.Document.getElementById("tr3").Cells(6).innerText
dg = IE.Document.getElementById("tr4").Cells(6).innerText
'dh = IE.Document.getElementById("tr5").Cells(6).innerText
End With
'Skriv ut den lagrede verdien (kurs) i celle Q5 - Q9
Range("Q5").Value = dd
Range("Q6").Value = de
Range("Q7").Value = df
Range("Q8").Value = dg
'Range("Q9").Value = dh
'Formatterer cellene slik at de ikke er lagret som text, men tall (må da finnes bedre metoder?)
'Range("A3").CurrentRegion.EntireColumn.AutoFit
Range("Q5:Q9").NumberFormat = "0.00"
' Rydd opp og lukk explorer
Application.StatusBar = ""
IE.Quit
Set IE = Nothing
End Sub
Best regards
Thomas
Bookmarks