runtime error 1004: application-defined or object-defined error

aspiringnerd

New Member
Joined
Apr 22, 2022
Messages
39
Office Version
  1. 365
Platform
  1. Windows
I get the error at the following line
VBA Code:
Set table1 = .Range(Cells(1, 1), Cells(count_row, count_col))
I declared table1 as a range and then set it equal to a dynamic range but I'm getting the error.

VBA Code:
Sub Email_range()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim count_row, count_col As Integer
    Dim table1 As Range
    Dim table2 As Range
    Dim str1, str2, str3 As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Sheets("Sheet1")
        count_row = WorksheetFunction.CountA(.Range("A1", .Range("A1").End(xlDown)))
        count_col = WorksheetFunction.CountA(.Range("A1", .Range("A1").End(xlToRight)))
        Set table1 = .Range(Cells(1, 1), Cells(count_row, count_col))
    End With

    With Sheets("Sheet2")
        count_row = WorksheetFunction.CountA(.Range("A1", .Range("A1").End(xlDown)))
        count_col = WorksheetFunction.CountA(.Range("A1", .Range("A1").End(xlToRight)))
        Set table2 = .Range(Cells(1, 1), Cells(count_row, count_col))
    End With

    str1 = "<BODY STYLE = font-size12pt/font-family:Calibri>" & "Good morning,<br>”"
    str2 = "<br>Please see below.<br>"
    str3 = "<br>Best regards."

    On Error Resume Next
    With OutMail
        .to = "thisistest@test.com"
        .CC = " "
        .Subject = "Daily Report"
        .Display
        .HTMLBody = str1 & RangetoHTML(table2) & str2 & RangetoHTML(table1) & .HTMLBody
    End With

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try qualifying your Cells references by preceding them with a dot ( . ), for example...

VBA Code:
Set table1 = .Range(.Cells(1, 1), .Cells(count_row, count_col))

The same thing for your other line of code.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,247
Members
448,879
Latest member
oksanana

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