Invalid Next control variable in macro

Nidhi24

New Member
Joined
Nov 1, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
Hi. I keep getting an error as Invalid Next control variable reference for my below code. Any help would be greatly appreciated.

VBA Code:
Option Explicit

Sub ImportTable()

Cells.Clear
Dim OLApp As Outlook.Application
Set OLApp = New Outlook.Application

Dim ONS As Outlook.Namespace
Set ONS = OLApp.GetNamespace("MAPI")
Dim oMapi As Outlook.MAPIFolder
Dim myFolder As Outlook.Folder
Set myFolder = ONS.Folders("emailaddress").Folders("Inbox")
Set myFolder = myFolder.Folders("Custody mails")
Dim OLMAIL As Outlook.MailItem
Set OLMAIL = OLApp.CreateItem(olMailItem)
Dim intRow As Integer
Dim oItem As object

For Each OLMAIL In myFolder.Items
    Dim oHTML As MSHTML.HTMLDocument
    Set oHTML = New MSHTML.HTMLDocument
    Dim oElColl As MSHTML.IHTMLElementCollection
With oHTML
    .Body.innerHTML = OLMAIL.HTMLBody
    Set oElColl = .getElementsByTagName("table")
End With


For Each oItem In oMapi.Items
    If oItem.Subject = "Volume data" Then
   
            Exit For
    End If
'Next oItem
   
Dim t As Long, r As Long, c As Long
Dim eRow As Long

    For t = 0 To oElColl.Length - 1
        eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        For r = 0 To (oElColl(t).Rows.Length - 1)
            For c = 0 To (oElColl(t).Rows(r).Cells.Length - 1)
                Range("A" & eRow).Offset(r, c).Value = oElColl(t).Rows(r).Cells(c).innerText
            Next c
        Next r
        eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Next t
       
        'Cells(eRow, 1) = "Sender's Name:" & " " & OLMAIL.Sender
        'Cells(eRow, 1).Interior.Color = vbRed
        'Cells(eRow, 1).Font.Color = vbWhite
        Cells(eRow, 1) = "Date & Time of Receipt:" & " " & OLMAIL.ReceivedTime
        Cells(eRow, 1).Interior.Color = vbRed
        Cells(eRow, 1).Font.Color = vbWhite
        Cells(eRow, 1).Columns.AutoFit
       
Next OLMAIL

Range("A1").Select

Set OLApp = Nothing
Set OLMAIL = Nothing
Set oHTML = Nothing
Set oElColl = Nothing

On Error Resume Next
Range("A1:A" & Worksheets(1).UsedRange.Rows.Count).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

'ThisWorkbook.VBProject.VBE.MainWindow.Visible = False

End Sub
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
When posting code please use the available code tags (see my signature below for details). I have fixed them for you this time.

With a quick look, it appears the problem could be that you have commented out the Next oItem line

VBA Code:
For Each oItem In oMapi.Items
    If oItem.Subject = "Volume data" Then
   
            Exit For
    End If
'Next oItem
 
Upvote 0
Solution
When posting code please use the available code tags (see my signature below for details). I have fixed them for you this time.

With a quick look, it appears the problem could be that you have commented out the Next oItem line

VBA Code:
For Each oItem In oMapi.Items
    If oItem.Subject = "Volume data" Then
  
            Exit For
    End If
'Next oItem
Thankyou!!
 
Upvote 0

Forum statistics

Threads
1,215,561
Messages
6,125,538
Members
449,236
Latest member
Afua

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top