+ Reply to Thread
Results 1 to 7 of 7

Macro that AUTOMATICALLY activates another macro without human interference?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Macro that AUTOMATICALLY activates another macro without human interference?

    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

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Why don't you use an array?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Well, I should probably have mentioned that I am quite new to vba, so that might be one of the reasons. If you have a suggestion (and preferably a detailed example) I would love to see it and try to implement it.

    Thank you I'm advance.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Do you know how many stocks are in your portfolio?

    Does each stock have a value/price in a table on the page?

  5. #5
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Yes, that's correct. The stocks are ranked in "column A" by alphabetic order, while the price, number of stocks, total value etc is in the according row. So, I guess you can say it's in a table.

    At the moment I have 4 stocks!

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Are the values you want always in the HTML elements with the names tr1, tr2, tr3 etc?

    Do you always want to put the values in Q5, Q6, Q7 etc?

    If that's the case and you know the no of stocks you have you could use a loop.
    
    NoStocks = 4
    
    For I = 1 To NoStocks
    
        Range("Q" & 4 + I).Value = dd = IE.Document.getElementById("tr" & I).Cells(6).innerText
    
    Next I
    If you don't always know the no of stocks something might be able to be done using the no of rows in the table.

    Something like this perhaps, though I'm kind of taking a bit of a guess with the code to get the no of rows in the table
    NoStocks = IE.Document.getElementById("tr1").table.rows.length + 1
    
    For I = 1 To NoStocks
    
        Range("Q" & 4 + I).Value = dd = IE.Document.getElementById("tr" & I).Cells(6).innerText
    
    Next I

  7. #7
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Macro that AUTOMATICALLY activates another macro without human interference?

    Yes, its the latter option. I want the macro to detect how many stocks I have every time I run the macro, so the number of stocks is "unknown".

    I tried to implement you solution, but I keep getting the errormessage: "Run-time error '438': Object doesn't support this property or method".

    Maybe I'm doing it wrong?

    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 = "mlcinvest"
            .Document.all.Item("pContent").Value = "1MillionDollaz$"
            .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
            
            NoStocks = IE.Document.getElementById("tr1").Table.Rows.Length + 1
    
        For I = 1 To NoStocks
    
        Range("Q" & 4 + I).Value = dd = IE.Document.getElementById("tr" & I).Cells(6).innerText
    
        Next I
            
            '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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro that AUTOMATICALLY activates another macro without human interference? HELP
    By Mangorni in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-24-2016, 09:23 AM
  2. Replies: 1
    Last Post: 10-10-2015, 01:00 AM
  3. Help with running a Macro that activates a different workbook whose name changes
    By jjcarter1988 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2013, 05:45 PM
  4. Button who activates a macro
    By RoMarius1981 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-20-2012, 07:27 AM
  5. Cell select activates macro
    By pauluk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-28-2005, 09:55 AM
  6. [SOLVED] How can I stop a macro for Human Interaction
    By Henry Stock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-19-2005, 07:06 PM
  7. Rectangle that activates a macro
    By in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1