+ Reply to Thread
Results 1 to 20 of 20

Excel 2010 to 2016 broke VB code?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Excel 2010 to 2016 broke VB code?

    I have a spreadsheet which works fine and was created in Excel 2010. We will be upgrading to Excel 2016 soon, but for some strange reason my script will not work on newer versions of Excel. I don't even know where to begin trouble shooting this, as it seems to me it should work fine across all versions. My code is below. I also attached my file.

    Sub Test()
        ActiveSheet.Unprotect Password:=""
        
           
        
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;https://www.wsj.com/market-data/bonds", Destination:=Range("$B$2"))
            .Name = "myQuery"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            
            .RefreshPeriod = 0
            
            .WebSelectionType = xlSpecifiedTables
            .WebTables = "1"
            
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    
    
     Dim L&
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open "GET", "https://www.wsj.com/market-data/bonds", False
            .setRequestHeader "DNT", "1"
            .send
         If .Status = 200 Then
             L = InStr(.responseText, "Rates shown are effective")
             If L Then ActiveSheet.Range("A12") = Mid(.responseText, L, InStr(L, .responseText, "</") - L)
         End If
        End With
    
    
    ActiveSheet.Protect Password:=""
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Excel 2010 to 2016 broke VB code?

    Rick, in order to help you I've to buy Excel 2016 to see what the error message and at which line it occurs.

    But, maybe you can tell us this info to make a suggestion to you.

    .

  3. #3
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Good idea. Here is the error:

    Error Code: System Error &H80072F7D (-2147012739). An error occurred in the secure channel support.

  4. #4
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Excel 2010 to 2016 broke VB code?

    Well, on which line do you get this error ?

    After the line "Dim L&" ?

    .

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,204

    Re: Excel 2010 to 2016 broke VB code?

    Bad news. It works for me unedited/unchanged.

    I have 64 bit Windows and 32 bit Office/Excel. It's a 365 subscription ... so, 2016 or whatever. Which versions(s) of Excel don't work for you? Are you missing any references in your VBA Project.

    It may be that the newer versions are 64 bit and the code may be slightly different.

    As Haluk has said, we would need to know where and how it fails for you.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Just had a chance to look at this again. I don't know how to know which line of code is the problem. I just tried the code again with two different users who have Excel 2016 and it throws the same error... We are going to upgrade soon and I need to have this work.

    I open my code, click run script, and I see a different error: Run-time error '-2147012739 (80072f7d) Automation error

    Any help would be appreciated, as the upgrade will break this workbook for many users...

  7. #7
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    I am using Microsoft Office Professional Plus 2016, which breaks the code, prior to that Excel 2010 worked fine. Makes no logical sense. Also, I double checked, both are the 32-bit versions.

    If I remove this portion of the code, it works but I need that portion of the script to pull in the effective date of the rates:

    Dim L&
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open "GET", "https://www.wsj.com/market-data/bonds", False
            .setRequestHeader "DNT", "1"
            .send
         If .Status = 200 Then
             L = InStr(.responseText, "Rates shown are effective")
             If L Then ActiveSheet.Range("A12") = Mid(.responseText, L, InStr(L, .responseText, "</") - L)
         End If
        End With

  8. #8
    Registered User
    Join Date
    06-26-2019
    Location
    California, US
    MS-Off Ver
    Office 365
    Posts
    36

    Re: Excel 2010 to 2016 broke VB code?

    Quote Originally Posted by Rick-O-Shay View Post
    Just had a chance to look at this again. I don't know how to know which line of code is the problem. I just tried the code again with two different users who have Excel 2016 and it throws the same error... We are going to upgrade soon and I need to have this work.

    I open my code, click run script, and I see a different error: Run-time error '-2147012739 (80072f7d) Automation error

    Any help would be appreciated, as the upgrade will break this workbook for many users...
    Use F8 to step into the code (go through it line-by-line) instead of just pressing run.

  9. #9
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Ah, thanks!

    It errors here: If. Status = 200 Then

  10. #10
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    From further research, I am wondering if we can use a readystate command here, but I just don't know enough yet to know what I am doing. I think this line is just checking to see if the website loaded, right? Would readystate be a good replacement? If so, how would I write that?

  11. #11
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Excel 2010 to 2016 broke VB code?

    The problem could be caused by Microsoft Office not correctly handling the TLS 1.2 certificate installed on the server.
    The fix to this problem is available in a Microsoft article 3140245.

  12. #12
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Mohsen, if that is the issue, that is a problem because I can't be doing updates like this company wide to fix the issue... The fact that it worked in prior versions would indicate there would be some kind of work around...

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,518

    Re: Excel 2010 to 2016 broke VB code?

    Which version of Windows are you running?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  14. #14
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    I get the error on Windows 7, I think some of the other users in the company have Windows 10 that are getting the error.

  15. #15
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Excel 2010 to 2016 broke VB code?

    The codes on all sheets work fine under Windows 10 (using Excel 2019 under Office Professional Plus 2019). No errors generated.

  16. #16
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Bizarre, I have had multiple different people show me that it will not work on Office 2016, including me on my home PC... It does not make any sense.

  17. #17
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Excel 2010 to 2016 broke VB code?

    Rick;

    I've encountered with the same error. I am using Excel 2010 (32 Bit) on Win7-Home (64 bit).

    I changed the 2nd part of the code and instead of using "WinHttp.WinHttpRequest" I used "MSXML2.XMLHTTP" object.

    Here is the revised code that works fine for me;

    Sub Test()
        'Haluk - 18/07/2019
        'sa4truss@gmail.com
        
        Dim xmlHTTPReq As Object
        Dim postURL As String
        Dim L As Long
        
        postURL = "https://www.wsj.com/market-data/bonds"
        
        With ActiveSheet.QueryTables.Add(Connection:="URL;" & postURL, Destination:=Range("$B$2"))
            .Name = "myQuery"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            
            .RefreshPeriod = 0
            
            .WebSelectionType = xlSpecifiedTables
            .WebTables = 1
            
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
        
        Set xmlHTTPReq = CreateObject("MSXML2.XMLHTTP")
        
        With xmlHTTPReq
            .Open "GET", postURL, False
            .send
        End With
        
        L = InStr(xmlHTTPReq.responseText, "Rates shown are effective")
        If L Then ActiveSheet.Range("A12") = Mid(xmlHTTPReq.responseText, L, InStr(L, xmlHTTPReq.responseText, "</") - L)
        
        Set xmlHTTPReq = Nothing
    End Sub
    .
    Last edited by Haluk; 07-18-2019 at 01:48 PM.

  18. #18
    Forum Contributor
    Join Date
    12-30-2009
    Location
    US
    MS-Off Ver
    Excel 2003, 2010, 2016, 2019, & Office 365
    Posts
    194

    Re: Excel 2010 to 2016 broke VB code?

    Haluk, you are my hero dude. This worked!

  19. #19
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,204

    Re: Excel 2010 to 2016 broke VB code?

    I'm using an Office 365 subscription (32 bit). The code works both from the buttons on each sheet and stepping through using F8 (although I wouldn't have expected that to fail if the button worked).

    I've tried running the code, clearing the table, and running it again. All with no issues.

    If you comment out the If and End If lines of code, does it run through?

    Do the other subroutines work ok?

  20. #20
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Excel 2010 to 2016 broke VB code?

    Rick;

    Another alternative is; to use the XMLHTTP object also for the 1st part of the original code, which involves with a query to get data from a table.

    By adding 2 references to the project via Tools >> References menu in VBA editor the following code will work faster, I hope...

    Reference-1: Microsoft XML V6.0
    Reference-2: Microsoft HTML Object Library

    I also attached a workbook including the below alternative code.

    Sub Test2()
        'Haluk - 18/07/2019
        'sa4truss@gmail.com
        'References added to the project:
        '1) Microsoft XML V6.0
        '2) Microsoft HTML Object Library
        
        Dim xmlHTTPReq As XMLHTTP
        Dim HTMLdoc As HTMLDocument
        Dim postURL As String
        Dim noRows As Integer, i As Integer, j As Integer
        
        Set xmlHTTPReq = New XMLHTTP
        Set HTMLdoc = New HTMLDocument
        
        postURL = "https://www.wsj.com/market-data/bonds"
        
        xmlHTTPReq.Open "GET", postURL, False
        xmlHTTPReq.send
       
        If xmlHTTPReq.Status = 200 Then
            HTMLdoc.body.innerHTML = xmlHTTPReq.responseText
    
            Set Tables = HTMLdoc.getElementsByTagName("Table")
            Set myTable = Tables(0)
    
            noRows = myTable.Rows.Length
    
            For i = 0 To noRows - 1
                For j = 0 To myTable.Rows(i).Cells.Length - 1
                    Cells(i + 1, j + 1) = myTable.Rows(i).Cells(j).innerText
                Next
            Next
            Range("A12") = HTMLdoc.getElementsByClassName("WSJTheme--intro-text--2pqtJQIp ")(0).innerText
        End If
    
        Columns.AutoFit
        
        Set Tables = Nothing
        Set myTable = Nothing
        Set HTMLdoc = Nothing
        Set xmlHTTPReq = Nothing
    End Sub
    Attached Files Attached Files
    Last edited by Haluk; 07-18-2019 at 03:22 PM.

+ 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. file from Excel 2016 opened with 2010
    By thoandros in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-22-2019, 03:44 AM
  2. Excel 2010 VS 2016
    By lostsoul65 in forum Excel General
    Replies: 3
    Last Post: 05-25-2018, 09:52 AM
  3. VBA Code works in 2010, but not in 2016
    By duffynmu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-05-2017, 04:11 AM
  4. 2010 Macro not working in 2016 Excel
    By porepiga in forum Excel General
    Replies: 7
    Last Post: 04-18-2017, 12:11 PM
  5. Opening Excel 2010 in 2016 causes double images
    By tlshook in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2016, 12:48 PM
  6. [SOLVED] Issues with code sent from 2010 to 2016
    By dwr2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-10-2016, 03:57 PM
  7. Replies: 2
    Last Post: 05-17-2016, 12:23 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