+ Reply to Thread
Results 1 to 11 of 11

2 different errors im struggling with

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    15

    2 different errors im struggling with

    Hi everyone,

    I'm very new to all this and have been working on a project to help out with work. I've been following some online videos and tried some different stuff I've found on these forums, but still am at a loss on how to fix the errors.

    The first one was a 'block if without end if', but following some replies i saw on different threads i added a extra 'end if' and now its become a 'for without next' error. That is for a user-form error messagebox I'm trying to have. When someone doesn't fill in all the fields, i want the message to play and the data to not be added to the spreadsheet until this is complete.

    The second is 'Object variable or with variable not set'. That is for a macro i recorded basically using a combo box to select different options to filter the data (i know i could basically just use a filter, but i think this would be more user friendly).

    The whole project has become exceedingly messy. I've got a lot of random code lines in there that i have no idea where they come from.

    I've attached a copy of my project.

    Thanks for any help people can provide. I'm sorry if questions are a bit stupid.

    Regards,
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: 2 different errors im struggling with

    Hi and welcome to the forum

    At the moment you have two IF statements and only one End If. They should be paired.

    You also have a For Each Ctrl without a Next Ctrl
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

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

    Re: 2 different errors im struggling with

    This should sort the first error.
    Private Sub btnAdd_Click()
    Dim ws As Worksheet
    Dim i As Variant
    Dim newRow As Long
    
        Set ws = Worksheets("Overstocks")
    
        newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    
        ws.Cells(newRow, 1).Value = Me.txtBrand.Value
        ws.Cells(newRow, 2).Value = Me.cbCategory.Value
        ws.Cells(newRow, 3).Value = Me.txtQuantity.Value
        ws.Cells(newRow, 4).Value = IIf(opYes.Value, "Yes", "No")
        ws.Cells(newRow, 5).Value = Date
    
        Clear_Form
    
        'validate textbox entry
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is MSForms.TextBox Then
                If ctrl.Value = vbNullString Then
                    MsgBox "You must complete all entries"
                    ctrl.SetFocus
                    Unload Me
                  Exit Sub
                End If
            End If
        Next ctrl
        
    End Sub
    Not sure where to look for the other code that's causing problems, where is that located?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    01-26-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    15

    Re: 2 different errors im struggling with

    Quote Originally Posted by Richard Buttrey View Post
    Hi and welcome to the forum

    At the moment you have two IF statements and only one End If. They should be paired.

    You also have a For Each Ctrl without a Next Ctrl
    Thank you both for your input and really quick replies!

    Thanks Richard, I'll have a look at some YouTube videos about that. Random question is there anyone online you who's videos you recommend? I feel like I'm not learning the important basics, with the current videos I'm watching.

    Quote Originally Posted by Norie View Post
    This should sort the first error.
    Private Sub btnAdd_Click()
    Dim ws As Worksheet
    Dim i As Variant
    Dim newRow As Long
    
        Set ws = Worksheets("Overstocks")
    
        newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    
        ws.Cells(newRow, 1).Value = Me.txtBrand.Value
        ws.Cells(newRow, 2).Value = Me.cbCategory.Value
        ws.Cells(newRow, 3).Value = Me.txtQuantity.Value
        ws.Cells(newRow, 4).Value = IIf(opYes.Value, "Yes", "No")
        ws.Cells(newRow, 5).Value = Date
    
        Clear_Form
    
        'validate textbox entry
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is MSForms.TextBox Then
                If ctrl.Value = vbNullString Then
                    MsgBox "You must complete all entries"
                    ctrl.SetFocus
                    Unload Me
                  Exit Sub
                End If
            End If
        Next ctrl
        
    End Sub
    Not sure where to look for the other code that's causing problems, where is that located?
    I added that code Norie thank you very much. The problem I'm still having is even though i don't complete the userform, it still automatically adds the IST 'no' section. How do i rectify this? The second error is located in module 2, no idea how I've ended up putting it there lol.

  5. #5
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: 2 different errors im struggling with

    Please run the attached file...
    Made it working without errors
    Attached Files Attached Files
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  6. #6
    Registered User
    Join Date
    01-26-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    15

    Re: 2 different errors im struggling with

    Quote Originally Posted by Parth007 View Post
    Please run the attached file...
    Made it working without errors
    Thanks Parth007 for that. I'm still getting a error with the combobox on the spreadsheet that's supposed to sort the data? P.S i like your choice in beers

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

    Re: 2 different errors im struggling with

    Do you mean that data from the userform is still going to the worksheet even if the fields haven't been completed?

    If so then you need to shift the code about a bit.
    Private Sub btnAdd_Click()
    Dim ws As Worksheet
    Dim i As Variant
    Dim newRow As Long
    
        'validate textbox entry
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is MSForms.TextBox Then
                If ctrl.Value = vbNullString Then
                    MsgBox "You must complete all entries"
                    ctrl.SetFocus
                    Unload Me
                  Exit Sub
                End If
            End If
        Next ctrl
        
        Set ws = Worksheets("Overstocks")
    
        newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
        ws.Cells(newRow, 1).Value = Me.txtBrand.Value
        ws.Cells(newRow, 2).Value = Me.cbCategory.Value
        ws.Cells(newRow, 3).Value = Me.txtQuantity.Value
        ws.Cells(newRow, 4).Value = IIf(opYes.Value, "Yes", "No")
        ws.Cells(newRow, 5).Value = Date
    
        Clear_Form
    
    End Sub
    As for the other error, there's no Module2.

  8. #8
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: 2 different errors im struggling with

    thanks Norie, Even i was about to figure it put.. Your code did work perfectly as always

  9. #9
    Registered User
    Join Date
    01-26-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    15

    Re: 2 different errors im struggling with

    Thanks for your help guys! This is going to help me no end at work. I truly appreciate you giving up your time to help.

    Norie I moved the module into the correct project (at least i think i have lol) Overstocks fixed by Parth and Norie.xlsm

    Otherwise heres the code

    Option Explicit
    
    Sub Brand2()
    '
    ' Brand2 Macro
    ' Sort A-Z for brand
    
    '
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
            Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Sub Category()
    '
    ' Category Macro
    ' Sort Category a-z
    '
    
    '
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
            Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Sub Quantity()
    '
    ' Quantity Macro
    ' Sort Quantity Highest to Lowest
    '
    
    '
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
            Range("C1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Sub IST()
    '
    ' IST Macro
    ' Sort Z-A
    '
    
    '
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
            Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Sub Date1()
    '
    ' Date1 Macro
    ' Lowest First
    '
    
    '
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
            Range("E1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Thanks again guyss, I hope i can contribute back to the forum one day

  10. #10
    Registered User
    Join Date
    01-26-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    15

    Re: 2 different errors im struggling with

    I thought maybe i was getting the 2nd error because i was using a normal combo box. I've tried it with a activeX control and also without Option Explicit. All to no avail. Still getting the same error

  11. #11
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: 2 different errors im struggling with

    On which line you getting error?

+ 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. Struggling with IF
    By KClark in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-10-2011, 04:37 AM
  2. struggling
    By Tomas mcD in forum Excel General
    Replies: 1
    Last Post: 03-07-2011, 08:25 AM
  3. [SOLVED] Excel Throwing Circular Errors When No Errors Exist
    By MDW in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-10-2006, 09:20 AM
  4. Struggling IF value
    By Rebecca in forum Excel General
    Replies: 5
    Last Post: 04-06-2006, 04:55 PM
  5. Help, I'm Struggling!
    By Fybo in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 03-04-2005, 04:06 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