VBA - AUTO FIT, Can we remove make the sum formula in below code

rajeshnam

New Member
Joined
Aug 19, 2024
Messages
1
Office Version
  1. 365
VBA Code:
Sub ExtractTableFromEmailToExcel()
    On Error GoTo ErrorHandler ' Start error handling

    Dim OutlookApp As Object
    Dim OutlookNamespace As Object
    Dim Inbox As Object
    Dim Subfolder As Object
    Dim Item As Object
    Dim HTMLDoc As Object
    Dim Table As Object
    Dim Row As Object
    Dim Cell As Object
    Dim ws As Worksheet
    Dim i As Integer
    Dim j As Integer
    Dim headersCopied As Boolean

    ' Define Outlook constants
    Const olFolderInbox As Integer = 6
    Const olMail As Integer = 43

    ' Initialize Outlook objects
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Inbox = OutlookNamespace.GetDefaultFolder(olFolderInbox)

    ' Replace "test" with the name of your specific subfolder
    Set Subfolder = Inbox.Folders("test")

    ' Loop through each item in the subfolder
    For Each Item In Subfolder.Items
        If Item.Class = olMail Then

            ' Check for specific email subjects and select the corresponding worksheet
            Select Case True
                Case InStr(Item.Subject, "CB_Production Summary_") > 0
                    Set ws = ThisWorkbook.Sheets("Sheet1")
                Case InStr(Item.Subject, "FS Production") > 0
                    Set ws = ThisWorkbook.Sheets("Sheet2")
                Case InStr(Item.Subject, "Level 1") > 0
                    Set ws = ThisWorkbook.Sheets("Sheet3")
                Case Else
                    ' Skip the item if the subject does not match any of the specified patterns
                    GoTo skipitem
            End Select

            ' Activate and clear the worksheet
            ws.Activate
            i = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1 ' Start at the next empty row

            ' Initialize HTML document
            Set HTMLDoc = CreateObject("HTMLFile")
            HTMLDoc.body.innerHTML = Item.HTMLBody

            ' Initialize headersCopied flag
            headersCopied = False

            ' Loop through tables in the email body
            For Each Table In HTMLDoc.getElementsByTagName("table")

                ' Loop through rows in the table
                For Each Row In Table.Rows

                    ' If headers have already been copied, skip the first row (assumed to be the header)
                    If headersCopied And Row.RowIndex = 0 Then
                        GoTo SkipHeader
                    End If

                    ' Loop through cells in the row
                    j = 1
                    For Each Cell In Row.Cells
                        ws.Cells(i, j).Value = Cell.innerText
                        ws.Cells(i, j).WrapText = True
                        j = j + 1
                    Next Cell

                    i = i + 1

SkipHeader:
                Next Row

                headersCopied = True ' Mark that headers have been copied
            Next Table

            ' Apply borders to the data range
            With ws.Range(ws.Cells(1, 1), ws.Cells(i - 1, j - 1))
                .Borders(xlEdgeLeft).LineStyle = xlContinuous
                .Borders(xlEdgeTop).LineStyle = xlContinuous
                .Borders(xlEdgeBottom).LineStyle = xlContinuous
                .Borders(xlEdgeRight).LineStyle = xlContinuous
                .Borders(xlInsideVertical).LineStyle = xlContinuous
                .Borders(xlInsideHorizontal).LineStyle = xlContinuous
                .EntireColumn.AutoFit
            End With
        End If

skipitem:
    Next Item

    MsgBox "Table extraction complete!"
    Exit Sub

ErrorHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Why did you start a new account and post the same code as your other question here?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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