Problem: Challenge in Iterating and Assigning Variables to HTML Table Elements

I am currently working on a project that involves tracking the order statuses of individual shipments from a list of URLs stored in an Excel sheet. The code I have written is capable of looping through the URLs and extracting information from them successfully. However, I encountered a type mismatch error when attempting to add exceptions and variables to the loop for different URLs. To solve this issue, I need three specific variables to extract HTML information from the HTMLCollection/Table. Here's what I'm trying to accomplish:

  • Iterate through each URL without opening new tabs in Internet Explorer.
  • Retrieve the status of each item from the corresponding HTML element.
  • FedEx: Delivered (td class="status")
  • UPS: Delivered (id="tt_spStatus")
  • USPS: Arrived at USPS Facility (class= "info-text first)

This is my current code:

Sub TrackingDeliveryStatusUpdate()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = True

Dim rngURL As Range
Dim wb1 As Workbook, ws1 As Worksheet, ws2 As Worksheet
Dim MyURL As String
Dim Rows As Long, links As Variant, IE As InternetExplorer, link As Variant
Dim i As Long
Dim sID As String
Dim rngLinks As Range, rngLink As Range


Dim filterRange As Range
Dim copyRange As Range

Set wb1 = Application.Workbooks.Open("\\S51\Store51\Employee Folders\Jason\TrackingDeliveryStatus.xls")
Set ws1 = wb1.Worksheets("TrackingDeliveryStatusResults")

Rows = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Set rngLinks = ws1.Range("C2:C" & Rows)

With IE
    .Visible = True
    i = 2
    For Each rngLink In rngLinks
        .Navigate (rngLink)
        While .Busy Or .ReadyState <> 4: DoEvents: Wend

        Dim doc As Object
        Set doc = IE.Document

        If InStr(1, rngLink.Value2, "ups") Then
            sID = "tt_spStatus"
            ws1.Range("D" & i).Value = doc.getElementById(sID).Items(0).Value
        End If

    Next rngLink
    i = i + 1
End With


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True



End Sub

I initially thought this approach would work, but it seems that referencing URLs from an Excel spreadsheet causes issues. Here's a screenshot of the error message I encountered: https://i.stack.imgur.com/qC2cB.png

Answer №1

To handle the situation, modify Link and Links as range and update the code to:

links = ws1.Range("C2:C" & Rows)

Replace it with:

Set Links = ws1.Range("C2:C" & Rows)

You are attempting to assign a Variant to a Range, which is not possible. However, once you set Links to the cell range, you can access each cell's URL by referencing link.Value2 in your loop.

Answer №2

The issue with your code is that there are multiple intertwined concepts in your statements. I have redesigned the relevant sections of the code and proposed changes to variable types and names.

Dim rangeLinks as Range, rangeLink as Range

Set rangeLinks = ws1.Range("C2:C" & Rows)

For each rangeLink in rangeLinks

Here, you're iterating through cells within a specific range of a worksheet.

Further down, in the For each rangeLink in rangeLinks statement, after waiting for IE to load:

Dim documentObj as Object
Set documentObj = .document

If InStr(1, rangeLink.Value2, "ups") Then 
     statusID = "tt_spStatus"
     ws1.Range("D" & I).Value = documentObj.getElementById(statusID).Items(0).Value
End If

You can also remove the For Each TDelement In statusID block since it isn't necessary.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with Background Image Display in Internet Explorer 8

I have been attempting to set a background image for the . element, but it is not displaying correctly. The image shows up in Firefox and Chrome, but not in Internet Explorer. I have included a link to my website and relevant CSS code below. Can anyone pro ...

Achieving equal height for the englobing/parent div and the inner divs

To achieve a standard left, middle, and right column layout, it is necessary for me to enclose various inner divs of different heights within a surrounding/parent div. It is crucial that the parent div adjusts its height to match the tallest inner div, whi ...

How can I set a value using document.getElementById(idPopUPImage).innerHTML to create a static popup in Leaflet?

I added a leaflet map to my project which you can find on Codpen In the map, I've included a button key that displays an image in a popup when clicked. However, after closing the popup and reopening it, the image doesn't show unless I click the ...

Discovering the values within a separate JSON object

I have a collection of json objects that include information about different languages and user details. Languages User Details The user details contain a field for languages, which can have multiple values. Below is a sample json: $scope.languages = ...

Proper HTML style linking with CSS

Describing the file structure within my project: app html index.html css style.css The problem arises when trying to link style.css as follows: <link rel="stylesheet" type="text/css" href="css/style.css"/> S ...

Is there a way to identify the ID of a button using Javascript specifically in Internet Explorer and Safari browsers?

Within my code, there lies a directive that contains the attribute on-blur = blurFunc($event). Imagine this scenario: I interact with a button bearing the id "myButton" located outside of the directive. How can I determine which specific button was clicke ...

Unexpected Error: Null value prevents accessing 'getElementsByClassName' property in HTML, along with Troubleshooting Inactive Button Behavior in HTML

Can someone identify the error in my HTML code? I keep getting an "Uncaught TypeError: Cannot read property 'getElementsByClassName' of null" error on the second line of the script tag. Additionally, my implemented active header code is not funct ...

The coloring feature in Excel Add-in's React component fails to populate cells after the "await" statement

Currently, I am developing a Microsoft Excel Add-in using React. My goal is to assign colors to specific cells based on the value in each cell, indicated by a color code (refer to the Board Color column in the image below). To achieve this, I retrieve the ...

Integrating a fresh element into the carousel structure will automatically generate a new row within Angular

I'm currently working on an Angular4 application that features a carousel displaying products, their names, and prices. At the moment, there are 6 products organized into two rows of 3 each. The carousel includes buttons to navigate left or right to d ...

Ways to ensure that v-model does not become "true" or "false" in an input checkbox using Vue

I am currently working on a filter popup that includes a list of checkboxes. By default, some items should be selected and others not selected. I have connected these checkboxes to an object array using v-model. My issue is that when I deselect and select ...

Arrange the menu items in a column layout based on a given condition

Can someone assist me with displaying the menu subitems? I have created a plunker. Take a look at it to understand what I need (open plunker in full screen) https://plnkr.co/edit/IMEJFPfl5kavKvnUYaRy?p=preview In the plunker above, there are two dropdown ...

The PHP script is exhausting memory resources and triggering a fatal error

I encountered the following error message: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 528384 bytes) in /home/u234536193/domains/monetizingonline.com/public_html/wp-includes/wp-db.php on line 2024 The problematic cod ...

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

How can I add information into a nested div element?

I encountered an issue when attempting to insert data into my block. The div structure is as follows: <div class="1"> <div class="2"> <div class="3"> <div class="4"></div> </div> ...

An element featuring a background color is vertically aligned in the middle of its parent container

Struggling to achieve a seemingly simple task, but coming up short on finding a solution. The goal is to have a background-color that aligns vertically in the middle of the first and last images in a stack of images. It may sound more complicated than it a ...

Production environment experiencing issues with jQuery tabs functionality

Currently, I have implemented jQuery tabs on a simple HTML page. The tabs are functioning correctly and smoothly transitioning between different content sections. However, upon integrating this setup into my actual project environment, I encountered an is ...

By increasing the background-color to 100%, the list item obstructs the background image of its parent div

In cases where an element is set to display:block, the background-color of that element typically extends to 100% of the width of its parent element. However, I am seeking a solution where the background-color only reaches the edge of the text, allowing th ...

Trouble with triggering HTML5 FileReader() functions

I'm having trouble determining why neither readSuccess() nor readFailure() are being executed in the code snippet below: function readMyFile(){ var reader = new FileReader(); reader.onload = readSuccess; reader.onerror = readFailure; ...

Is your Ajax jQuery live search not functioning properly with JSON data?

My programming code is not functioning properly. Here is the file I am working on. When it does work, it does not display the list and gives an error in the Json file. I am unsure of the reason behind this issue. You will be able to view the error in the C ...

Maintain the alignment of content in the center while expanding background images

I am looking to create a website with a split background effect, similar to the design on this site . I want to maintain a content container width of 980px while achieving this look. I attempted to accomplish this by adding height and background color pro ...