+ Reply to Thread
Results 1 to 10 of 10

Code not producing a value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Code not producing a value

    I have copied and pasted this code across from somewhere where the If statement is based on cell values so I know it works. Now it is based on userform values and refuses to produce anything.

    Mtt_Script = Userform
    UoW = Combobox 1
    Letter = Combobox 2
    Sophistciated refers to a Select case range

    Select Case ComboBox1
        Case 231, 232, 233, 234, 235, 236
             Letter = "Sophisticated"
        Case 221, 222, 228, 229, 237
             Letter = "Unsophisticated"
    End Select

    It must have something to do with the If statement but I have also copied each criteria from elsewhere in the code where it works and I cannot for the life of me work out why it does not work in this one instance. I cannot see the same two cells referenced in this way elsewhere in the code so it can't be ambigious coding.

    Please, please, please can someone show me the very simple thing I am doing wrong (it's always something simple that catches me out)????????


    ' Sophisticated Letter & Sophistication Test UoW

     If MTT_Script.UoW = "Sophistication Test" And MTT_Script.Letter = "Sophisticated" Then
            Sheets("Sheet2").Select
            Range("D2:S32").Select
            Selection.ClearContents
            Selection.Font.Underline = xlUnderlineStyleNone
            Selection.Font.ColorIndex = 0
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .MergeCells = True
            .WrapText = True
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        With Sheets("Sheet2").Range("D2:S32")
        End With
            .Value = "“Your case has been deemed Sophisticated which means you are not in scope of the review.“" & vbNewLine & _
            vbNewLine & _
            "“We wrote to you on " & Sheets("Sheet1").Range("P20") & " to advise you of this and to ask whether you wanted to provide further information“" & vbNewLine & _
            vbNewLine & _
            "“Have you received this letter?“" & vbNewLine & _
            "If No - Select the Unreceived Letters Button."
            .Font.Name = "Arial"
            .Font.Size = 22
            Range("A1").Select
        End With
    Userform is attached
    Attached Files Attached Files
    Last edited by Sc0tt1e; 11-29-2013 at 12:11 PM. Reason: Drop downs on attachment had no values

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

    Re: Code not producing a value

    What is MTT_Script.Letter?

    Is it some sort of additional control that has a Letter property?

    By the way, why aren't you referring to Letter from this piece of code?
    Select Case ComboBox1
        Case 231, 232, 233, 234, 235, 236
             Letter = "Sophisticated"
        Case 221, 222, 228, 229, 237
             Letter = "Unsophisticated"
    End Select
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Code not producing a value

    MTT_Script.Letter is Userform1.combobox2

    If I set combobox1 as letter editor doesn't like it, if I leave it as combobox1 it defines my selection for me

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

    Re: Code not producing a value

    I've just had a look at the workbook and the first thing I saw in the code was this.
    Dim UoW As ComboBox
    Dim Letter As ComboBox
    UoW and Letter are comboboxes on the userform so you shouldn't have variables with the same name in the code.

    Also, you refer to a ComboBox1 when there isn't a ComboBox1.

    ComboBox1 should be replaced with the name of the combobox which lists 231, 232 etc followed by .Value.

    I think Letter might be the relevant combobox so I've used it in the following code.
    Option Explicit
    Private Sub CommandButton1_Click()
    Dim strLetter As String
    
        Select Case Letter.Value
            Case 231, 232, 233, 234, 235, 236
                strLetter = "Sophisticated"
            Case 221, 222, 228, 229, 237
                strLetter = "Unsophisticated"
        End Select
    
        Sheets("Sheet1").Unprotect ("lewings")
        Sheets("Sheet2").Unprotect ("lewings")
        Sheets("Sheet3").Unprotect ("lewings")
    
        '   Sophistication Testing - No Letter Sent
    
        If Me.UoW.Value = "Sophistication Test" And strLetter = "None" Then
            With Sheets("Sheet2")
                With .Range("D2:S32")
                    .ClearContents
                    .Font.Underline = xlUnderlineStyleNone
                    .Font.ColorIndex = 0
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .MergeCells = True
                    .WrapText = True
                    .Borders(xlDiagonalDown).LineStyle = xlNone
                    .Borders(xlDiagonalUp).LineStyle = xlNone
                    .Bordersaround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=xlAutomatic
                    .Borders(xlInsideVertical).LineStyle = xlNone
                    .Borders(xlInsideHorizontal).LineStyle = xlNone
                    .Value = """Your case is currently at the Sophistication Testing phase. I cannot provide you with a precise timeframe as to how long it will take for your case to complete this stage as each case is different.""" & vbNewLine & _
                             """However on average once a case reaches this stage it typically takes  " & Sheets("sheet3").Range("H5") & "  weeks until an outcome is reached.""" & vbNewLine & _
                             vbNewLine & _
                             vbNewLine & _
                             """As soon as the outcome is known we will write to you."""
                End With
                With .Range("A1")
                    .Font.Name = "Arial"
                    .Font.Size = 22
                End With
            End With
    
            
        ElseIf Me.UoW = "Sophistication Test - No Unit of Work Quoted" And strLetter <> "None" Then
      
            ' code for No UoW & Sophistication Letter (Error)
            
        ElseIf Me.UoW = "Sophistication Test" And strLetter = "Sophisticated" And LDate.Value = True Then
            ' code for Sophisticated Letter & Sophistication Test UoW
            
        End If

  5. #5
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Code not producing a value

    Yep, you were spot on. After your last post I checked and I hadn't referenced the SelectCase up to that point that's why it was all working except this part.

    Someone helped me out with the Case Select and suggested I dim them both so I did, thanks for clarifying. All changed and all working fine

    You are a star, thank you

    How do I mark this as solved now, I couldn't see where to do it?

  6. #6
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Code not producing a value

    Ah Norie, that's where it's happening, it hasn't defined the Sophisticated and non sophisticated aspects of the Select Case.

    I did this once and it worked perfectly but excel closed all my sheets when I shut down the debug I was running and I lost all my work and am now rebuilding all I have researched and developed today.

  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: Code not producing a value

    Did you try the code I posted?

  8. #8
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Code not producing a value

    Yep changed combobox1 to Letters.Value and it produces what I need.

    I got rid of the dim combobox statements but what was the dim strLetter as string you added all about?

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

    Re: Code not producing a value

    It was to do with categorizing the selection made in the combobox Letter.

    I used strLetter as it's a string and so as not to clash with the combobox Letter.

    If you look further down the code I've used strLetter in the If/ElseIfs.

    I also changed MTT_Script to Me, Me refers to the userform.

    PS You should get rid of all that Select/Selection stuff.

  10. #10
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Code not producing a value

    Clearing up the code is defintely on the to do list. For now it works and I am on a deadline so I'll leave it as is for now but once completed I will go through and remove select/selection and bring the multi lines down into one to reduce size and complexity etc.

    I will change the strletter and script to me as that seems a good idea, appreciate all the advice and help

+ 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. I need a code for presence check to check multiple text boxes and combo boxes
    By Lee_wwfc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2013, 01:53 PM
  2. [SOLVED] Vba code to fill check in & check out counts
    By ashfaquebwd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2013, 09:02 AM
  3. [SOLVED] vba code to be correct and check ,vba code to brig data for the same refreince
    By funtastic in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2012, 02:29 PM
  4. [SOLVED] check my code - big ask
    By Little pete in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2005, 02:05 PM
  5. Just a code check please...
    By ste mac in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-21-2005, 06:06 AM

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