+ Reply to Thread
Results 1 to 17 of 17

VBA Error '1004'- Userform use and emailing data

  1. #1
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Question VBA Error '1004'- Userform use and emailing data

    Hi all,
    I'm a new excelForum user and have a VBA related question.
    I've created a userform below and I have a code to help write the data entered in the form do the following:

    1. Save the data in a worksheet (Asset History) within the same workbook,
    2. Copy the newly entered data into a another worksheet (Asset Form).
    3. Take an image of the data from worksheet (Asset Form) and
    a) Create a HTML image on out look
    b) Send out an email (Get the email distribution from worksheet (EmailList)
    4. I also want to be able to select the data nad change if any of the information changes using the userform.

    Not sure why my code is getting stuck and getting the attached error "1004" when I get to saving and emailing. My guess is that the error is on the Checklist option explicit but I cants fix. Please help.

    Here is the full coding

    Private Sub cmdsave_Click()
    Dim cell As Range
    Dim rowoffset As Integer
    Dim ws As Worksheet
    Dim rng As Range
    Dim rFind As Range
    Dim RowCount As Long
    Dim ctl As Control
    Dim NewID As Integer


    If Not IsDate(Me.txtEntryDate.Value) Then
    MsgBox "You must fill in the request date.", vbCritical, "Date Missing"
    Me.txtEntryDate.SetFocus
    End If
    If Me.cboShopName.Value = "" Then
    MsgBox "Please choose a Shop Name.", vbExclamation, "Missing Shop Name"
    Me.cboShopName.SetFocus
    End If
    If Me.txtPartNo.Value = "" Then
    MsgBox "You must enter a Part No.", vbCritical, "Part No. Required"
    Me.txtPartNo.SetFocus
    End If
    If Me.txtSerialNo.Value = "" Then
    MsgBox "Please enter the Serial No.", vbExclamation, "Missing Serial No."
    Me.txtPartNo.SetFocus
    End If
    'Write Data to Rotable history worksheet'
    Set ws = Sheets("Asset History")
    Set rFind = ws.Range("A2")
    ws.Activate

    NewID = rFind + 1
    If MsgBox("Add New Asset as Entry ID " & Format(NewID, "") & "?", vbOKCancel, _
    "Add New Asset") = vbCancel Then
    Exit Sub
    End If

    Me.EnableEvents = False

    Me.txtEntryDate = Date
    Me.txtUpdateDate = ""
    Me.cboUniqueID.AddItem Format(NewID, ""), 0
    Me.cboUniqueID = Format(NewID, "")
    rFind.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    Set rFind = rFind.Offset(-1, 0)
    With rFind.EntireRow.Font
    .ColorIndex = 0
    .Bold = False
    End With
    rFind.Offset(0, 0) = Format(NewID, "")
    rFind.Offset(0, 1) = Format(Me.txtEntryDate, "m/dd/yy")
    rFind.Offset(0, 2) = Me.txtUpdateDate
    rFind.Offset(0, 3) = Me.txtPartNo.Value
    rFind.Offset(0, 4) = Me.txtSerialNo.Value
    rFind.Offset(0, 5) = Me.txtCost.Value
    rFind.Offset(0, 6) = Me.cboShopName.Value
    rFind.Offset(0, 7) = Me.txtPurchaseOrderNo.Value
    rFind.Offset(0, 8) = Me.txtBuyerName.Value
    rFind.Offset(0, 9) = Me.txtNotes.Value
    rFind.Offset(0, 10) = Me.cboProfitCenter.Value
    rFind.Offset(0, 11) = Me.txtAssetNo.Value
    rFind.Offset(0, 12) = Environ$("USERNAME")
    Me.EnableEvents = True
    Call CheckLists
    End Sub

    Private Sub cmdUpdate_Click()
    Dim rFind As Range

    If Me.EnableEvents = False Then Exit Sub

    Sheets("Asset History").Activate
    Set rFind = ActiveWorkbook.Names("ID").RefersToRange.Find(Format(Me.cboUniqueID.Value, "0"), lookat:=xlWhole)

    If rFind Is Nothing Then
    MsgBox "Select an Entry ID to update ", vbOKOnly, "No entry found"
    Exit Sub
    Else
    If MsgBox("Update history for ID " & Me.cboUniqueID.Value & " with new values? ", vbOKCancel, "Edit History") = vbCancel Then
    Exit Sub
    End If
    End If

    Me.EnableEvents = False

    Me.txtUpdateDate = Date
    rFind.Offset(0, 1) = Format(Me.txtEntryDate, "m/dd/yy")
    rFind.Offset(0, 2) = Format(Me.txtUpdateDate, "m/dd/yy")

    If Not rFind.Offset(0, 3) = Me.txtPartNo Then
    With rFind.Offset(0, 3)
    .Value = Me.txtPartNo
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 3).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If

    If Not rFind.Offset(0, 4) = Me.txtSerialNo Then
    With rFind.Offset(0, 4)
    .Value = Me.SerialNo
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 4).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If

    If Not rFind.Offset(0, 5) = Me.txtCost Then
    With rFind.Offset(0, 5)
    .Value = Me.txtCost
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 5).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If

    If Not rFind.Offset(0, 6) = Me.cboShopName Then
    With rFind.Offset(0, 6)
    .Value = Me.cboShopName
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 6).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If

    If Not rFind.Offset(0, 7) = Me.txtPurchaseOrderNo Then
    With rFind.Offset(0, 7)
    .Value = Me.txtPurchaseOrderNo
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 7).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If


    If Not rFind.Offset(0, 8) = Me.txtBuyerName Then
    With rFind.Offset(0, 8)
    .Value = Me.txtBuyerName
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 8).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If


    If Not rFind.Offset(0, 9) = Me.txtNotes Then
    With rFind.Offset(0, 9)
    .Value = Me.txtNotes
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 9).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If

    If Not rFind.Offset(0, 10).Text = Me.cboProfitCenter Then
    With rFind.Offset(0, 10)
    .Value = Me.cboProfitCenter
    .Font.ColorIndex = 3
    .Font.Bold = True
    End With
    Else
    With rFind.Offset(0, 10).Font
    .ColorIndex = 0
    .Bold = False
    End With
    End If


    rFind.Offset(0, 12) = Environ$("USERNAME")

    Me.EnableEvents = True

    Call CheckLists

    End Sub

    Private Sub cbouniqueID_Change()
    Dim rFind As Range

    If Me.EnableEvents = False Then Exit Sub

    Me.txtEntryDate.SetFocus

    Set rFind = ActiveWorkbook.Names("ID").RefersToRange.Find(Format(Me.cboUniqueID.Value, "0"), lookat:=xlWhole)

    If Not Me.txtPartNo.Value = vbNullString Then
    If MsgBox("Are you sure you want to load entry ID " & Me.cboUniqueID.Value & " from history?" & _
    vbCrLf & vbCrLf & "This will remove all changes on this form", vbOKCancel, "Mmmm...") = vbCancel Then
    Me.EnableEvents = False
    Me.cboUniqueID = LastID
    Me.EnableEvents = True
    Exit Sub
    End If
    End If

    LastID = Me.cboUniqueID
    Me.txtEntryDate = rFind.Offset(0, 1)
    Me.txtUpdateDate = rFind.Offset(0, 2)
    Me.txtPartNo = rFind.Offset(0, 3)
    Me.txtSerialNo = rFind.Offset(0, 4)
    Me.txtCost = rFind.Offset(0, 5)
    Me.cboShopName = rFind.Offset(0, 6)
    Me.txtPurchaseOrderNo = rFind.Offset(0, 7)
    Me.txtBuyerName = rFind.Offset(0, 8)
    Me.txtNotes = rFind.Offset(0, 9)
    Me.cboProfitCenter = rFind.Offset(0, 10)
    Me.txtAssetNo = rFind.Offset(0, 11)

    End Sub

    Private Sub FillEmailRange(rFind As Range)
    Dim ws As Worksheet
    Dim rng As Range
    Set ws = Sheets("Asset Form")
    Set rng = ws.Range("B1")

    rng.Offset(0, 0) = rFind.Offset(0, 3)
    rng.Offset(1, 0) = rFind.Offset(0, 4)
    rng.Offset(2, 0) = rFind.Offset(0, 5)
    rng.Offset(3, 0) = rFind.Offset(0, 6)
    rng.Offset(4, 0) = rFind.Offset(0, 7)
    rng.Offset(5, 0) = rFind.Offset(0, 8)
    rng.Offset(6, 0) = rFind.Offset(0, 9)
    rng.Offset(7, 0) = rFind.Offset(0, 10)
    rng.Offset(8, 0) = Format(Me.txtEntryDate, "m/dd/yy")
    rng.Offset(9, 0) = Format(Me.txtUpdateDate, "m/dd/yy")
    rng.Offset(10, 0) = rFind.Offset(0, 11)
    rng.Offset(11, 0) = rFind.Offset(0, 12)
    rng.Offset(12, 0) = "<" & Format(Me.cboUniqueID, "0000") & ">"

    rng.Offset(0, 0).Font.Color = rFind.Offset(0, 3).Font.Color
    rng.Offset(1, 0).Font.Color = rFind.Offset(0, 4).Font.Color
    rng.Offset(2, 0).Font.Color = rFind.Offset(0, 5).Font.Color
    rng.Offset(3, 0).Font.Color = rFind.Offset(0, 6).Font.Color
    rng.Offset(4, 0).Font.Color = rFind.Offset(0, 7).Font.Color
    rng.Offset(6, 0).Font.Color = rFind.Offset(0, 8).Font.Color
    rng.Offset(7, 0).Font.Color = rFind.Offset(0, 9).Font.Color
    rng.Offset(8, 0).Font.Color = rFind.Offset(0, 10).Font.Color
    rng.Offset(9, 0).Font.Color = rFind.Offset(0, 11).Font.Color
    rng.Offset(10, 0).Font.Color = rFind.Offset(0, 12).Font.Color


    rng.Offset(1, 0).Font.Bold = rFind.Offset(0, 4).Font.Bold
    rng.Offset(2, 0).Font.Bold = rFind.Offset(0, 5).Font.Bold
    rng.Offset(3, 0).Font.Bold = rFind.Offset(0, 6).Font.Bold
    rng.Offset(4, 0).Font.Bold = rFind.Offset(0, 7).Font.Bold
    rng.Offset(6, 0).Font.Bold = rFind.Offset(0, 8).Font.Bold
    rng.Offset(8, 0).Font.Bold = rFind.Offset(0, 9).Font.Bold
    rng.Offset(9, 0).Font.Bold = rFind.Offset(0, 10).Font.Bold
    rng.Offset(10, 0).Font.Bold = rFind.Offset(0, 11).Font.Bold


    End Sub
    Private Sub cmdSendEmail_Click()
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim rowoffset As Integer
    Dim EmailTo As String
    Dim Subject As String
    Dim EmailBody As Range
    Dim ws As Worksheet
    Dim rFind As Range
    Dim ScreenShotTo As String



    If MsgBox("Do you want to create email to the distribution List?", vbYesNo, "E-mail Rotable Form") = vbNo Then Exit Sub
    Application.ScreenUpdating = False

    'Gathers and assembles the distribution list into a single string

    Set rFind = ActiveWorkbook.Names("ID").RefersToRange.Find(Format(Me.cboUniqueID.Value, "0"), lookat:=xlWhole)
    If rFind Is Nothing Then
    MsgBox vbCrLf & "Please add the new Asset to history first. " & vbCrLf, vbOKOnly, "Meow"
    Exit Sub
    End If

    Call FillEmailRange(rFind)

    Set rFind = ActiveWorkbook.Names("EmailList").RefersToRange.Find(Me.EmailList, lookat:=xlWhole)
    If rFind Is Nothing Then
    MsgBox "Please choose a distribution list to E-mail ", vbOKOnly, "List Not Found"
    Exit Sub
    End If

    Set ws = Sheets("Asset Form")
    Set EmailBody = ws.Range("A1:B12")
    ws.Activate
    EmailBody.Select

    rowoffset = 1
    EmailTo = ""
    Do
    EmailTo = EmailTo & rFind.Offset(rowoffset, 0).Text & "; "
    rowoffset = rowoffset + 1
    Loop Until rFind.Offset(rowoffset, 0).Text = ""

    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)

    On Error Resume Next
    With OutlookMail
    .To = EmailTo
    .CC = ""
    .BCC = ""
    .Subject = "Asset: " & Me.txtPartNo & " " & Me.txtSerialNo & ""
    .HTMLBody = RangetoHTML(EmailBody)
    .Display
    End With
    On Error GoTo 0

    Set OutlookMail = Nothing
    Set OutlookApp = Nothing

    End Sub





    Private Sub SendEmail_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim EmailBody As Range
    Dim EmailTo As String
    Dim rowoffset As Integer
    Dim rFind As Range
    Dim ws As Worksheet

    Set rFind = ActiveWorkbook.Names("Asset ID").RefersToRange.Find(Format(Me.cboUniqueID.Value, "0"), lookat:=xlWhole)
    If rFind Is Nothing Then
    MsgBox vbCrLf & "Please add the new Rotable Asset to history first. " & vbCrLf, vbOKOnly, "Check"
    Exit Sub
    End If

    Call FillEmailRange(rFind)

    Set rFind = ActiveWorkbook.Names("EmailList").RefersToRange.Find(Me.cboEmailList, lookat:=xlWhole)
    If rFind Is Nothing Then
    MsgBox "Please choose a distribution list to E-mail ", vbOKOnly, "List Not Found"
    Exit Sub
    End If

    Set ws = Sheets("Asset Form")
    Set EmailBody = ws.Range("A1:B12")
    ws.Activate
    EmailBody.Select

    rowoffset = 1
    EmailTo = ""
    Do
    EmailTo = EmailTo & rFind.Offset(rowoffset, 0).Text & "; "
    rowoffset = rowoffset + 1
    Loop Until rFind.Offset(rowoffset, 0).Text = ""

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

    On Error Resume Next
    With OutMail
    .To = EmailTo
    .CC = ""
    .BCC = ""
    .Subject = "New Asset Creation: " & " <Part# " & Me.txtPartNo & " <Serial# " & Me.txtSerialNo & ">"
    .HTMLBody = RangetoHTML(EmailBody)
    .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    End Sub


    Private Sub CheckLists()
    Dim n As Name
    Dim iCounter As Integer
    Dim wb As Workbook


    For Each n In ActiveWorkbook.Names
    If InStr(n.Name, "FilterDatabase") = 0 And InStr(n.Name, "Print_Area") = 0 And InStr(n.Name, "ID") = 0 Then
    If n.RefersToRange.Find(Me.Controls.Item(n.Name), lookat:=xlWhole) Is Nothing Then
    If Not Me.Controls.Item(n.Name) = "" Then
    n.RefersToRange.End(xlDown).Offset(1, 0) = Me.Controls.Item(n.Name)
    n.RefersToRange.Sort key1:=n.RefersToRange, order1:=xlAscending, Header:=xlYes, DataOption1:=xlSortTextAsNumbers
    Me.Controls.Item(n.Name).AddItem Me.Controls.Item(n.Name), 0
    End If
    End If
    End If


    Next n

    End Sub


    Userform Image.PNG

    VBA error.PNG

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Yes. I'll upload the workbook.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Here is the code (with code tags) portion that I'm getting an error with. Im using Excel 2010 if that helps. Your helps is much appreciated.

    <Dim wb As Workbook>


    <For Each n In ActiveWorkbook.Names>
    <If InStr(n.Name, "FilterDatabase") = 0 And InStr(n.Name, "Print_Area") = 0 And InStr(n.Name, "ID") = 0 Then>
    <If n.RefersToRange.Find(Me.Controls.Item(n.Name), lookat:=xlWhole) Is Nothing Then>
    <If Not Me.Controls.Item(n.Name) = "" Then>
    <n.RefersToRange.End(xlDown).Offset(1, 0) = Me.Controls.Item(n.Name)>
    <n.RefersToRange.Sort key1:=n.RefersToRange, order1:=xlAscending, Header:=xlYes,> <DataOption1:=xlSortTextAsNumbers>
    <Me.Controls.Item(n.Name).AddItem Me.Controls.Item(n.Name), 0>
    <End If>
    <End If>
    <End If>


    <Next n>

    <End Sub>

  5. #5
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Full code with tags. Sorry I'm new and trying to do the posting right.

    HTML Code: 

  6. #6
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Hi all,
    Please help me out on this error. Let me know if you need more information.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    Sorry for not getting back to you.

    I've had a quick look and can't see anything obviously wrong.

    Can you explain what steps I an take to replicate the error?

    By the way, why is this in the userform module?
    Please Login or Register  to view this content.
    Pretty sure it should go in the ThisWorkbook module.

  8. #8
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    When I enter information using the "asset request create form" and click on the save button, I get the following error1Error 1.PNG

    Which leads me to this code.

    HTML Code: 
    When I enter information using the "asset request create form" and click on the email button, I get the same error and it leads me to the following code

    HTML Code: 
    Also. I cannot seem to be able to pull in the data from history whenever I need to change or add to it. I'ld like to pull the data using the ID

    I just put all the code under the userform. I'll move it to the workbook. Thanks for the catch.
    Last edited by Wayesu; 06-26-2014 at 10:22 AM.

  9. #9
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Somebody help me out... please...please...

    Norie, I'm counting on you

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    Wayesu

    Which form do you mean, is it the RotableCreationForm?

    What information do you enter when you get the error?

    Which line of code is highlighte

    Which form do you mean is it the RotableCreationForm?

  11. #11
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    Yes the rotableCreationForm. Click on it and just enter any information to test it.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    When I do that I get all sorts of message boxes when I click save.

    If I just click through them I can see something happening in the background but I don't get any errors.

  13. #13
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    When you click the save button, dont you get an erro message? What other messages are you getting?

    Are you able to email the file. Try emailing to yourself as a test.

    Are you able to change data information using the userform? I cant get the data show on the userform if I need to change an entry.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    Right, I've managed to generate an error on this line in the CheckList sub
    Please Login or Register  to view this content.
    What are you trying to do there?

    What are you trying to do with the sub CheckList?

  15. #15
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    That's where I'm getting the error.
    I'm trying to get the newly entered data that is saved on worksheet "Asset History" to save under worksheet "Asset Form" so that I can then email as a html to different people whose email are on the worksheet "EmailList"

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Error '1004'- Userform use and emailing data

    That code seems pretty complicated for saving data from a form to a worksheet.

    For a start, why are you looping through all the names in the workbook?

  17. #17
    Registered User
    Join Date
    06-13-2014
    Posts
    11

    Re: VBA Error '1004'- Userform use and emailing data

    I'm open to new and better way to code as long as I achieve the three main objectives:
    Have the ability to make changes to the entry by selecting any row using the ID
    Email the information entered on the userform using the template on worksheet name "Asset Form"
    Show any changes in a different colow whenever changes are made.

+ 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. Run time error 1004 on userform initialize
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-09-2014, 12:44 PM
  2. [SOLVED] Getting Error 1004 Object or Application Defined Error - Code to rearrange data
    By BlazzedTroll in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-10-2013, 12:10 PM
  3. Error 1004 - hitting a key in a dropdown list of a userform
    By ggabi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-20-2012, 06:19 AM
  4. Run time error 1004-created a userform
    By scrupo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2010, 04:11 AM
  5. Run-time error 1004 when trying to load UserForm
    By jasoncw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2008, 04:28 PM

Tags for this Thread

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