+ Reply to Thread
Results 1 to 28 of 28

Code not functioning properly

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Code not functioning properly

    Hi guys,

    I have attached a sample wb. User John password test. I seem to have identical codes in sheet 4 & 5 bar a couple of different cell refrences and the code seems to be working fine in sheet 4 & not in sheet 5. The latest code I have introduced was to autocapitalise names so for example if I input jOE blOGgs it would automatically go to Joe Bloggs. This works perfect in sheet 4 but not in sheet 5. I am very new to VBA and don't know it very well so would love some feedback if there is an error or something in it. I do get help from you guys then when I insert it into existing code that's where I sometimes make the mistake.
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Code not functioning properly

    What code are you referring to?

  3. #3
    Forum Contributor Nitro2481's Avatar
    Join Date
    09-09-2014
    Location
    Laois, Ireland
    MS-Off Ver
    2013
    Posts
    323

    Re: Code not functioning properly

    The capitalising code doesn't seem to work for sheet 5. Also in sheet 5 it seems to be acting funny. You can click on a cell and alter it but it doesn't indicate that it is selected. Usually when you select a cell it is highlighted on all four sides. You cannot see the cell you selected and then the autocapitalisations don't work either

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,654

    Re: Code not functioning properly

        Select Case True
    Case .Column = 4
                Range("A" & Target.Row) = Date
    
             Case Not Intersect(Target, Range("D4:D500")) Is Nothing
    You have two case statements that both test if the target in in column D. Only the 1st case will evaluate. The second one is just ignored. This applies to the code in both sheets but Sheet4 has data validation drop down lists. So the selected values are already formatted.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Hey,

    Thanks for the reply. if I understand you right then I have changed the date column so its not the same as the other one. So when anything is typed in D the date populates in A. But the autocapitalising is in column E instead See below. However this is causing a weird bug now that when I type in E in sheet 4 the sheet freezes and then does something funny with the calculations at the top of the page.

    Option Explicit
    Option Compare Text
    Dim rw As Long
    Dim thisrow As Long
    Dim c As Excel.Range
    Dim str              As String
    Dim v                As Variant
    Dim iStart           As Integer
    Dim iEnd             As Integer
    Private Sub Worksheet_Change(ByVal Target As Range)
    Const sPW As String = "$P$2"
    Const sHide As String = "I:I, O:O"
    If Not Intersect(Target, Range(sPW)) Is Nothing Then
        If Target.Value = 1234 Then
            ActiveSheet.Unprotect
            'Range(sHide & 1).EntireColumn.Hidden = False
            Range(sHide).EntireColumn.Hidden = False
            ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
        ElseIf Target.Value = "" Then
            ActiveSheet.Unprotect
            'Range(sHide & 1).EntireColumn.Hidden = True
            Range(sHide).EntireColumn.Hidden = True
            ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
        End If
        End If
       With Target
        Select Case True
     Case .Column = 4
                Range("A" & Target.Row) = Date
    
             Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                For Each c In Intersect(Target, Range("E4:E500"))
    
                   ' don't process - just remove the .
                   If Left(c.Value, 1) = "~" Then
                      str = Mid(c.Value, 2)
                   Else
                      str = StrConv(c.Value, vbProperCase)
    
                      ' O'Leary, D'Alton,A'Courcey, N'Dou, De'Ath (really!)
    
                      If InStr(str, "o'") > 0 Or _
                         InStr(str, "d'") > 0 Or _
                         InStr(str, "a'") > 0 Or _
                         InStr(str, "n'") > 0 Or _
                         InStr(str, "de'") > 0 Then
    
                         iStart = InStr(str, "'") - 1
                         str = Left(str, iStart) & "'" & StrConv(Mid(str, iStart + 2), vbProperCase)
                      End If
    
                      ' von Adler, van Dieman
                      If InStr(str, " von ") > 0 Or InStr(str, " van ") > 0 Then
                         iStart = InStr(str, " v") - 1
                         str = Left(str, iStart) & StrConv(Mid(str, iStart + 1, 4), vbLowerCase) & Mid(str, iStart + 5)
                      End If
    
                      ' von der Recke - but the von has already been handled. 1 of the reasons this is not a 'Select Case' block
                      If InStr(str, " der ") > 0 Then
                         iStart = InStr(str, " der ") - 1
                         str = Left(str, iStart) & StrConv(Mid(str, iStart + 1, 4), vbLowerCase) & Mid(str, iStart + 5)
                      End If
    
                      ' Hyphenated
                      If InStr(str, "-") > 0 Then
    
                         ' Ignore if spaced already
                         If Mid(str, iStart + 2) <> " " Then
                            iStart = InStr(str, "-") - 1
                            str = Left(str, iStart) & "-" & StrConv(Mid(str, iStart + 2), vbProperCase)
                         End If
                      End If
    
                      ' Let's just include a Mc
                      If InStr(str, " mc") > 0 Then
                         iStart = InStr(str, " mc") + 2
                         str = Left(str, iStart) & StrConv(Mid(str, iStart + 1), vbProperCase)
                      End If
    
                      ' Never mind the de la, della and about 20 others but getting vanishingly small in numbers
                   End If
                   c.Value = str
                Next
    
          End Select
    
       End With
    
    Catch:
    
       Application.EnableEvents = True
    
    End Sub
    
        
        Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveSheet.Unprotect
            ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
    
    End Sub

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,654

    Re: Code not functioning properly

    It seems to work for me; it auto-capitalizes E, no freeze, nothing "funny" (whatever that means).

  7. #7
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Hi,

    Ok I have it not "acting funny" now. By that I meant it just froze for a sec and then it seemed to recalculate figures on the top to ridiculous figures then recalculated them back but this has stopped. Are you experiencing the same issue as me on sheet 5 for some reason. Unable to highlight a cell. you can select it and alter it but looking at the spreadsheet you have no idea where the curser is. It's only happening on this sheet?

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,654

    Re: Code not functioning properly

    Quote Originally Posted by nitro2482 View Post
    Hi,

    Ok I have it not "acting funny" now. By that I meant it just froze for a sec and then it seemed to recalculate figures on the top to ridiculous figures then recalculated them back but this has stopped. Are you experiencing the same issue as me on sheet 5 for some reason. Unable to highlight a cell. you can select it and alter it but looking at the spreadsheet you have no idea where the curser is. It's only happening on this sheet?
    Yes I can duplicate the selection issue on Sheet5. Not sure what's causing that. It seems if you unprotect the sheet, select a cell, and then re-protect the sheet, it then works as you would expect.

  9. #9
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    No answers, but you seem to have lost an Error handler, an On Error statement and an EnableEvents=False from the thread with your other ID (Nitro8421) - while not disabling events is possibly not the issue here, you really should turn off event handing if changing cell values in a Change event. You could cause a cascading event which will put Excel into an endless loop and eventually cause an 'Out of Stack Space' error. Disabling Events also means an error trap is essential so that events can always be re-enabled.

    Another question is why does there seem to be a Workbook level event (Workbook_BeforeClose) in what seems to be a Worksheet class. This will never execute..
    Last edited by cytop; 05-22-2016 at 12:32 PM.

  10. #10
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Thanks @alphafrog. Yeah it's strange how it's only happening on that sheet? Hey @cytop I haven't a clue to be honest so any guidance greatly appreciated. I got the extra bits of code tried to insert them but way out of my comfort zone. It's very possible that there is a few things that I have copied off other sheets. For example that before workbook close. Basically I wanted every sheet to protect before close. So I stuck that at the end of every sheet. Going by your reaction above that should probably be just on the "this workbook" code? I just wanted to make sure if any sheet was accidentally left unprotected then on save or close it would reprotect. Maybe if you could point out specifically some recommended changes to my codes i can replicate that across all sheets and hopefully that would help the bug in sheet 5 also.

  11. #11
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    should probably be just on the "this workbook" code
    Yes - you'll find any number of examples with the simplest of searches.

    Going back to your procedure. It seems you use the horrible ProperName function in more than one sheet. It should be moved to a module.

    Option Explicit
    Option Compare Text   ' THIS IS REQUIRED
    
    Public Function ProperName(ByVal str As Variant) As Variant
    
       Dim vResult As Variant
       Dim iStart As Integer
       
       '// Don't process anything starting with a ~
       '// Just so the user will be able to enter anything not catered
       '// for - Example a 'Fitz with the uppcer letter in the 5th position
       '// 'FitzSimons'
       '// Inpout with a ~ to start and it will be removed and  the remaining
       '// characters passed bask without change
       If Left(str, 1) = "~" Then
          vResult = Mid(str, 2)
       ElseIf IsNumeric(str) Then
          vResult = str
       Else
          vResult = StrConv(str, vbProperCase)
    
          ' O'Leary, D'Alton,A'Courcey, N'Dou, De'Ath (really!)
    
          If InStr(vResult, " o'") > 0 Or _
             InStr(vResult, " d'") > 0 Or _
             InStr(vResult, " a'") > 0 Or _
             InStr(vResult, " n'") > 0 Or _
             InStr(vResult, " de'") > 0 Then
    
             iStart = InStr(vResult, "'") - 1
             
             vResult = Left(vResult, iStart) & "'" & StrConv(Mid(vResult, iStart + 2), vbProperCase)
             
          End If
    
          ' von Adler, van Dieman
          If InStr(vResult, " von ") > 0 Or InStr(vResult, " van ") > 0 Then
             iStart = InStr(vResult, " v") - 1
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1, 4), vbLowerCase) & Mid(vResult, iStart + 5)
          End If
    
          ' von der Recke - but the von has already been handled. 1 of the reasons this is not a 'Select Case' block
          If InStr(vResult, " der ") > 0 Then
             iStart = InStr(vResult, " der ") - 1
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1, 4), vbLowerCase) & Mid(vResult, iStart + 5)
          End If
    
          ' Hyphenated
          If InStr(vResult, "-") > 0 Then
    
             ' Ignore if spaced already
             If Mid(vResult, iStart + 2) <> " " Then
                iStart = InStr(vResult, "-") - 1
                vResult = Left(vResult, iStart) & "-" & StrConv(Mid(vResult, iStart + 2), vbProperCase)
             End If
          End If
    
          ' Let's just include a Mc
          If InStr(vResult, " mc") > 0 Then
             iStart = InStr(vResult, " mc") + 2
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1), vbProperCase)
          End If
    
          ' Never mind the de la, della and about 20 others but getting vanishingly small in numbers
       End If
       
       ProperName = vResult
    
    End Function
    Your worksheet code then changes to
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Const sPW            As String = "$P$2"
       Const sHide          As String = "I:I, O:O"
       
       '// Store the current setting for EnableEvents
       '// And turn off
       Dim EventsEnabled As Boolean
    
       EventsEnabled = Application.EnableEvents
       Application.EnableEvents = False
    
       '// Always and ever, more especially if working with Events turned off
       On Error GoTo Catch
    
       If Not Intersect(Target, Range(sPW)) Is Nothing Then
    
          ActiveSheet.Unprotect
    
          'Range(sHide & 1).EntireColumn.Hidden = False
          Range(sHide).EntireColumn.Hidden = (Target.Value <> 1234)
          ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                              False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                              AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
                              AllowUsingPivotTables:=True
          ActiveSheet.EnableSelection = xlUnlockedCells
       End If
    
       With Target
          Select Case True
             Case .Column = 4
                Range("A" & Target.Row) = Date
    
             Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                For Each c In Intersect(Target, Range("E4:E500"))
                   c.Value = ProperName(c.Value)
                Next
    
          End Select
    
       End With
    
    Catch:
    
       Application.EnableEvents = True
       Exit Sub
       
       '// Debugging only - code will never get to here
       Resume
    
    End Sub

  12. #12
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Ok I've set up a new module with the first code you gave me. Then on all sheets I amended the code to the second part from "with select" onwards and I have deleted the before close on all sheets. I am getting an error on the individual sheets saying "select case without end select" with the final END SUB highlighted. Also at the start of each sheet I have
    Option Explicit
    Option Compare Text
    Dim rw As Long
    Dim thisrow As Long
    Dim c As Excel.Range
    Dim str              As String
    Dim v                As Variant
    Dim iStart           As Integer
    Dim iEnd             As Integer
    I got a lot of it from the "horrible" "proper" code. Now I have moved it to a module do I need most of this stuff.

  13. #13
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    I am getting an error on the individual sheets saying "select case without end select"
    The code I posted was tested, and works in so far as I understand what it is supposed to do - so the issue must be in the changes you made. As you haven't posted the code, there's nothing I can suggest other than the obvious - you have mismatched control structures.

    I didn't take the time to clean up redundant variables but as you have an 'Option Explicit' statement in the module you could simply comment them all out and try compiling the project. If it doesn't object then that's fine. If it does object, you can simply uncomment whichever one it complains about and try compiling again.

  14. #14
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Thank you for your patience.

    This is the module code

    Option Explicit
    Option Compare Text   ' THIS IS REQUIRED
    
    Public Function ProperName(ByVal str As Variant) As Variant
    
       Dim vResult As Variant
       Dim iStart As Integer
       
       '// Don't process anything starting with a ~
       '// Just so the user will be able to enter anything not catered
       '// for - Example a 'Fitz with the uppcer letter in the 5th position
       '// 'FitzSimons'
       '// Inpout with a ~ to start and it will be removed and  the remaining
       '// characters passed bask without change
       If Left(str, 1) = "~" Then
          vResult = Mid(str, 2)
       ElseIf IsNumeric(str) Then
          vResult = str
       Else
          vResult = StrConv(str, vbProperCase)
    
          ' O'Leary, D'Alton,A'Courcey, N'Dou, De'Ath (really!)
    
          If InStr(vResult, " o'") > 0 Or _
             InStr(vResult, " d'") > 0 Or _
             InStr(vResult, " a'") > 0 Or _
             InStr(vResult, " n'") > 0 Or _
             InStr(vResult, " de'") > 0 Then
    
             iStart = InStr(vResult, "'") - 1
             
             vResult = Left(vResult, iStart) & "'" & StrConv(Mid(vResult, iStart + 2), vbProperCase)
             
          End If
    
          ' von Adler, van Dieman
          If InStr(vResult, " von ") > 0 Or InStr(vResult, " van ") > 0 Then
             iStart = InStr(vResult, " v") - 1
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1, 4), vbLowerCase) & Mid(vResult, iStart + 5)
          End If
    
          ' von der Recke - but the von has already been handled. 1 of the reasons this is not a 'Select Case' block
          If InStr(vResult, " der ") > 0 Then
             iStart = InStr(vResult, " der ") - 1
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1, 4), vbLowerCase) & Mid(vResult, iStart + 5)
          End If
    
          ' Hyphenated
          If InStr(vResult, "-") > 0 Then
    
             ' Ignore if spaced already
             If Mid(vResult, iStart + 2) <> " " Then
                iStart = InStr(vResult, "-") - 1
                vResult = Left(vResult, iStart) & "-" & StrConv(Mid(vResult, iStart + 2), vbProperCase)
             End If
          End If
    
          ' Let's just include a Mc
          If InStr(vResult, " mc") > 0 Then
             iStart = InStr(vResult, " mc") + 2
             vResult = Left(vResult, iStart) & StrConv(Mid(vResult, iStart + 1), vbProperCase)
          End If
    
          ' Never mind the de la, della and about 20 others but getting vanishingly small in numbers
       End If
       
       ProperName = vResult
    
    End Function
    And this is the worksheet code

    'Option Explicit
    'Option Compare Text
    'Dim rw As Long
    'Dim thisrow As Long
    'Dim c As Excel.Range
    'Dim str              As String
    'Dim v                As Variant
    'Dim iStart           As Integer
    'Dim iEnd             As Integer
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Const sPW            As String = "$P$2"
       Const sHide          As String = "I:I, O:O"
       
       '// Store the current setting for EnableEvents
       '// And turn off
       Dim EventsEnabled As Boolean
    
       EventsEnabled = Application.EnableEvents
       Application.EnableEvents = False
    
       '// Always and ever, more especially if working with Events turned off
       On Error GoTo Catch
    
       If Not Intersect(Target, Range(sPW)) Is Nothing Then
    
          ActiveSheet.Unprotect
    
          'Range(sHide & 1).EntireColumn.Hidden = False
          Range(sHide).EntireColumn.Hidden = (Target.Value <> 1234)
          ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
                              False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                              AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
                              AllowUsingPivotTables:=True
          ActiveSheet.EnableSelection = xlUnlockedCells
       End If
    
       With Target
          Select Case True
             Case .Column = 4
                Range("A" & Target.Row) = Date
    
             Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                For Each c In Intersect(Target, Range("E4:E500"))
                   c.Value = ProperName(c.Value)
                Next
    
          End Select
    
       End With
    
    Catch:
    
       Application.EnableEvents = True
       Exit Sub
       
       '// Debugging only - code will never get to here
       Resume
    
    End Sub

  15. #15
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    That seems to be the code I posted earlier and I know that works so am unsure where you're getting the error. I could be wrong as I'm using a phone at the moment but I honestly cannot see any difference.

    Also, you should not comment out the 2 statements
    Option Compare Text
    Option Explicit
    These are not variables and you should consider them mandatory in every code module/worksheet module, userform and Class module.

    You also need to uncomment the line
    Dim c as Excel.Range
    But, more properly, it should be declared in the Change event as it is only used there - not as a module level variable.

  16. #16
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    Ignore - duplicate
    Last edited by cytop; 05-23-2016 at 01:54 AM.

  17. #17
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Hey,

    I have made the amendments you have suggested. I have included the 3 Dims and commented out the rest. Still showing the same error

    Option Explicit
    Option Compare Text
    'Dim thisrow As Long
    'Dim str              As String
    'Dim v                As Variant
    'Dim iStart           As Integer
    'Dim iEnd             As Integer
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Excel.Range
    Dim rw As Long 
      Application.EnableEvents = False
       On Error GoTo Catch
    
       With Target
    
          Select Case True
             Case .Column = 8 And UCase(.Value) = "PROCEEDING TO AIP"
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & .Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & .Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & .Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
    
    
             With Target
          Select Case True
             Case .Column = 4
                Range("A" & Target.Row) = Date
    
             Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                For Each c In Intersect(Target, Range("E4:E500"))
                   c.Value = ProperName(c.Value)
                Next
    
          End Select
    
       End With
    
    Catch:
    
       Application.EnableEvents = True
       Exit Sub
       
       '// Debugging only - code will never get to here
       Resume
    
    End Sub

  18. #18
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    I have attached as real an example of my situation as I can. It includes all the sheets that require your code as well as the module. Hope you can make some sense on my mess. User John password test
    Attached Files Attached Files

  19. #19
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    If you indent your code correctly, the issue will be fairly obvious:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim c                As Excel.Range
       Dim rw               As Long
    
       Application.EnableEvents = False
       On Error GoTo Catch
    
       With Target
    
          Select Case True
             Case .Column = 8 And UCase(.Value) = "PROCEEDING TO AIP"
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & .Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & .Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & .Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
    
    
                With Target
                   Select Case True
                      Case .Column = 4
                         Range("A" & Target.Row) = Date
    
                      Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                         For Each c In Intersect(Target, Range("E4:E500"))
                            c.Value = ProperName(c.Value)
                         Next
    
                   End Select
    
                End With
    
    Catch:
    
                Application.EnableEvents = True
                Exit Sub
    
                '// Debugging only - code will never get to here
                Resume
    
             End Sub
    The 'End Sub' should finish up in column 1 of the editor, so something is mismatched. or left hanging open - in this case, 2 control structures (If, Do, Select Case, Case) do not have a corresponding End If, Loop, End Case (The Case statement does not have a closing statement - it is followed by another Case statement or End Select).

    You mentioned earlier you are new to VBA, so treat this as a learning exercise - read the code slowly following the logic of what it is doing, considering if it is appropriate at that point.

    You should also be aware that a Select Case statement will only ever execute the first Case that is True, the others will be skipped. You test if the changed column is 8 and then move some information. Problem is, it is only when that Case is True that the current code will then enter the Select Case for Cols 4 & 5. As the column can only be 8 for that code to execute those conditions will never be True. Confused yet?

    To help you indent code there's a link to a free utility in another recent thread

  20. #20
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    I get that the error I made was I copied the bit in red ahich was repeating so therefore wasn't working right. I have deleted these 2 lines on each sheet and they do appear to be working properly.

    The date code however does not seem to be working correctly. If I type in the specific column the date appears grand. However if a code puts text in the column rather than me putting it in manually then the date does not populate.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim c                As Excel.Range
       Dim rw               As Long
    
       Application.EnableEvents = False
       On Error GoTo Catch
    
       With Target
    
          Select Case True
             Case .Column = 8 And UCase(.Value) = "PROCEEDING TO AIP"
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & .Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & .Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & .Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
    
    
                With Target
                   Select Case True               
    
       Case .Column = 4
                         Range("A" & Target.Row) = Date
    
                      Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                         For Each c In Intersect(Target, Range("E4:E500"))
                            c.Value = ProperName(c.Value)
                         Next
    
                   End Select
    
                End With
    
    Catch:
    
                Application.EnableEvents = True
                Exit Sub
    
                '// Debugging only - code will never get to here
                Resume
    
             End Sub

  21. #21
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    and they do appear to be working properly
    Looking at the code you posted, I can't see how. You still have unmatched control structures. Yes, I know I could simply post the the correction but this is too fundamental. You have to understand both the error and the logic.

    You need to indent this correctly in order to see the issue.

  22. #22
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Never mind, I know that doesn't work. However going by your your statement that select case true only works on the first thing does that mean I need to put a new one in each time.


    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim c                As Excel.Range
       Dim rw               As Long
    
       Application.EnableEvents = False
       On Error GoTo Catch
    
       With Target
    
          Select Case True
             Case .Column = 8 And UCase(.Value) = "PROCEEDING TO AIP"
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & .Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & .Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & .Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
    End select
    
    select case true   
    Case .Column = 4
                         Range("A" & Target.Row) = Date
    
                      Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
                         For Each c In Intersect(Target, Range("E4:E500"))
                            c.Value = ProperName(c.Value)
                         Next
                   End Select
    
                End With
    
    Catch:
    
                Application.EnableEvents = True
                Exit Sub
    
                '// Debugging only - code will never get to here
                Resume
    
             End Sub
    Last edited by nitro2482; 05-23-2016 at 11:55 AM.

  23. #23
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Once I'm not annoying you I'm happy to learn. I don't use VBA in my day to day. I just use excel as a hobby and to manage my company finances and activity. I thought I was great with my sum(), If's, even lookups. I asked how to do something one day and I got a VBA code and it has completely opened Excel to me.

    I have a Private sub & End Sub
    I have a with & end With
    I have a select case & end select
    I have CASE but no END Case, but going by your earlier comments Case doesn't have end just a end select so do I put that in once to end the case and again to end the select?



    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim c                As Excel.Range
       Dim rw               As Long
    
       Application.EnableEvents = False
       On Error GoTo Catch
    
       With Target
    
          Select Case True
             Case .Column = 8 And UCase(.Value) = "PROCEEDING TO AIP"
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & .Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & .Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & .Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
    
       Case .Column = 4
                         Range("A" & Target.Row) = Date
    
                      Case Not Intersect(Target, Range("E4:E500")) Is Nothing
    
    end Select
    
                         For Each c In Intersect(Target, Range("E4:E500"))
                            c.Value = ProperName(c.Value)
                         Next
                   End Select
    
                End With
    
    Catch:
    
                Application.EnableEvents = True
                Exit Sub
    
                '// Debugging only - code will never get to here
                Resume
    
             End Sub

  24. #24
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    earlier comments Case doesn't have end just a end select
    Just to clarify, a typical Select Case block:
       Select Case x
          Case 1
             'Code
          Case 2
             ' Code
          Case Else
             ' Code]
       End Select
    The 'Case Else' is a catch-all for when no other item matches - it may, or may not, be included depending on the exact requirements. So a Case statement does not have an explicit End statement, it is either followed by another Case, and/or an 'End Select'.

    I'm happy to learn
    Then I'll say it again - Indent your code, it helps enormously when trying to match control structures and following the flow of the code. If you do not have permissions to download the add-in at work (or can't convince your IT dept t get it and install it for you) then go to the VBA Tools menu, select Options and on the Editor tab select everything in the 'Code Settings' frame. Auto Indent is one of the options but unfortunately it does not include Auto 'Outdent'. You need to type a Shift-Tab before an 'End If', 'End Select' etc.

    To save time, your procedure has been re-written to remove the Various control structures. you may find this easier to understand and it covers another logic flaw in the original code - editing multiple cells & pressing Ctrl-Enter could have caused an error.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim c                As Excel.Range
       Dim rw               As Long
       Dim AppEvents        As Boolean
    
       '// You don;t know the state of EnableEvents, so save the current setting so it can
       '// be restored later
       AppEvents = Application.EnableEvents
       '// Turn off events to prevent recursive calls.
       Application.EnableEvents = False
    
       On Error GoTo Catch
    
       '// The SELECT CASE, WITH and other control structures seem to be causing issues
       '// so... get rid of as many of them as possible.
       '// The For...Next loops are in case multiple cells are edited and confirmed
       '// with Ctrl-Enter. This is somethng else that would have caused an error in
       '// your original code.
       '// Slightly less effificent but you'll never notice the difference.
       If Not Intersect(Target, Range("H:H")) Is Nothing Then
          For Each c In Intersect(Target, Range("H:H"))
    
             If UCase(c.Value) = "PROCEEDING TO AIP" Then
                rw = Sheets("Mort Figs").Range("B" & Rows.Count).End(xlUp).Row + 1
                Range("E" & c.Row).Copy Destination:=Sheets("Mort Figs").Range("b" & rw)
                Range("g" & c.Row).Copy Destination:=Sheets("Mort Figs").Range("e" & rw)
                Range("d" & c.Row).Copy Destination:=Sheets("Mort Figs").Range("d" & rw)
             End If
          Next
       End If
    
       If Not Intersect(Target, Range("D:D")) Is Nothing Then
          For Each c In Intersect(Target, Range("D:D"))
             Range("A" & c.Row) = Date
          Next
       End If
    
       If Not Intersect(Target, Range("E4:E500")) Is Nothing Then
          For Each c In Intersect(Target, Range("E4:E500"))
             c.Value = propername(c.Value)
          Next
       End If
    
    Catch:
    
       Application.EnableEvents = AppEvents
       Exit Sub
    
       '// Debugging only - code will never get to here
       Resume
    
    End Sub
    Last edited by cytop; 05-23-2016 at 12:24 PM.

  25. #25
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    Hey @cytop,

    I'm a bit confused. I've taken your new code above. I used to have all if's on my code and the guy who wrote the proper name code in the worksheet changed everything to case. I have changed all the sheets back to if's now which does make it a little easier to understand.

    Having done this all seems well. The date is populating when text is manually typed in the cell or drop down. However the date does not go in the cell if the data is transferred to the trigger cell automatically. Also this code is not working correctly. The bit i red is flagging as a problem. I have tried to indent all the codes as you have suggested but I cannot see why it is no working. The very same code in yellow is working ok so I'm guessing its something wrong with the target sheet 4 but the code seems identical to sheet 5 where it is working. User john password test. Sorry I know I'm probably wrecking your head.

    Dim rw As Long, CopyTo As String
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("AB:AC")) Is Nothing Then
      With Target
        If UCase(.Value) = "BLOCK" Then
          If .Column = 28 Then
            CopyTo = "Banc ***"
            rw = Sheets(CopyTo).Range("E" & Rows.Count).End(xlUp).Row + 1
            Range("B" & .Row).Copy Destination:=Sheets(CopyTo).Range("E" & rw)
            Range("AB" & .Row).Copy Destination:=Sheets(CopyTo).Range("f" & rw)
            Sheets(CopyTo).Range("J" & rw).Value = "Sent Up"  'Put "Sent Up" in Column J
        ElseIf .Column = 29 Then
                CopyTo = "Home Ins"
            rw = Sheets(CopyTo).Range("D" & Rows.Count).End(xlUp).Row + 1
            Range("B" & .Row & ":C" & .Row).Copy Destination:=Sheets(CopyTo).Range("D" & rw)
            Range("AC" & .Row).Copy Destination:=Sheets(CopyTo).Range("F" & rw)
            Range("D" & .Row).Copy Destination:=Sheets(CopyTo).Range("g" & rw)
            Sheets(CopyTo).Range("J" & rw).Value = "Sent Up" 'Put "Sent Up" in Column K
            
          End If
        End If
      End With
    End If
    Const sPW As String = "$AJ$1"
    Const sHide As String = "Aa:Aa, AG:AG, Ak:Ak, Ap:Ap, AQ:AQ, Av:Av, Aw:Aw, Bb:Bb, Bc:Bc, Bh:Bh, Bi:Bi, Bn:Bn, Bo:Bo, Bt:Bt, Bu:Bu, Bz:Bz, Ca:Ca "
    If Not Intersect(Target, Range(sPW)) Is Nothing Then
        If Target.Value = 1234 Then
            ActiveSheet.Unprotect
            'Range(sHide & 1).EntireColumn.Hidden = False
            Range(sHide).EntireColumn.Hidden = False
            ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
        ElseIf Target.Value = "" Then
            ActiveSheet.Unprotect
            'Range(sHide & 1).EntireColumn.Hidden = True
            Range(sHide).EntireColumn.Hidden = True
            ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
        End If
        End If
    
    If Target.Column = 24 Then
        ActiveSheet.Unprotect
        If Range("AB" & Target.Row) = "Block" Then
            lr = Sheets("Banc ***").Range("B" & Rows.Count).End(xlUp).Row
            test = Sheets("Mort Figs").Range("B" & Target.Row).Value & Sheets("Mort Figs").Range("AB" & Target.Row).Value
            For x = 5 To lr
                test2 = Sheets("Banc ***").Range("E" & x).Value & Sheets("Banc ***").Range("F" & x).Value
                If test2 = test Then
                    my_row = x
                    Exit For
                End If
            Next
            Sheets("Banc ***").Range("J" & my_row) = "Issued"
            Sheets("Banc ***").Range("K" & my_row) = Target
            
        End If
        If Range("AC" & Target.Row) = "Block" Then
            lr = Sheets("Home Ins").Range("B" & Rows.Count).End(xlUp).Row
            test = Sheets("Mort Figs").Range("B" & Target.Row).Value & Sheets("Mort Figs").Range("AC" & Target.Row).Value
            For x = 5 To lr
                test2 = Sheets("Home Ins").Range("D" & x).Value & Sheets("Home Ins").Range("F" & x).Value
                If test2 = test Then
                    my_row = x
                    Exit For
                End If
            Next
            Sheets("Home Ins").Range("j" & my_row) = "Issued"
            Sheets("Home Ins").Range("k" & my_row) = Target
            
        
            
        End If
    
       ActiveSheet.Unprotect
            ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
            False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
            AllowUsingPivotTables:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
    End If
    End Sub
    Attached Files Attached Files

  26. #26
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code not functioning properly

    The Case Statements were just as easy to understand if you had indented the code correctly - I won't stop emphasizing how critical to understanding code that can be, especially when you start to get nested control structures. And you should go and re-read your other thread and note who "the guy who wrote the proper name code" is.

    On that tack, I have to ask why do you use 2 IDs...? Am curious.

    if the data is transferred to the trigger cell automatically
    You don't explain how the data is transferred 'automatically' ... remember no one except you understands the processes, procedures and data flow of your workbook. You need to be very precise when asking about errors - you need to detail the steps leading up to the error.

    In the sample workbook you posted, the error highlighted Red is raised because a referenced cell has a #REF error - I can't comment any further as I have no idea what should be there.

    For these additional errors I think you would be better off starting new threads, 1 for each problem and keep each thread focused on just the one problem.

  27. #27
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    The ref# is just a drop down list off all staff names. That information was on a sheet I deleted for privacy and space reason so I could upload the wb. Sorry I figured reading the formula you could make sense of the flow. The trigger is if column 24 has text in it and column AB has the word block in it then look for the name in column b. Match it with the same name in sheet 5 and change the words sent up to issued and put in the same day that you put in sheet 2 column 24 in sheet 5. I'll try it today in work with a fully functional work book.

    Ha ha awkward. I thought I recognised your cytop handle but you referenced the "horrible" proper name function I thought you were giving out about the author of it.

    Regarding the 2 profiles. You were helping me loads with the proper name code. You were coming back really quick with loads of information. Then when you changed it to allow for o' and mc etc it started causing me troubles and then you went quiet ( God forbid you have other things to do then help me out ) but that happens on here sometimes where people engage with you. Bring you so far along then disappear. I also notice that once a post had been up for a few days it seems to get less and less attention. So I sometimes close a thread and reopen a new one and then someone new seems to pick it up quite quickly. I did that that day and I got an infraction for duplicate thread. I really like this forum, find everyone like yourself very generous with their time and very helpful and would hate to cause myself any hassle on this site so I thought I'd set up a second profile rather than have a duplicate post. It's just I jumped right in and put your code into my main sheet rather than testing it first like I usually do and my main sheet was then acting up.

    I really appreciate your help and support. I'm sure our paths will cross again on here. I'm sure I'll mess up many more formula / code. Maybe there is a place I can read up on the absolute basics of VBA like real basics. It's hard to spot errors if your not even sure why if is used or case is used or the logic behind code. Maybe if I could learn some of the logic the errors would seem more obvious.

    Anyway thank you so much for your help & patience.
    Last edited by nitro2482; 05-24-2016 at 02:19 AM.

  28. #28
    Registered User
    Join Date
    05-22-2016
    Location
    Ireland
    MS-Off Ver
    2003
    Posts
    14

    Re: Code not functioning properly

    FYI that error did not happen on the full version of my workbook. Thanks again.

+ 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. [SOLVED] Formula not functioning properly
    By Dena in forum Excel General
    Replies: 11
    Last Post: 05-16-2014, 03:04 PM
  2. [SOLVED] SQL string not functioning properly
    By batador in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-19-2013, 06:03 AM
  3. Countif, not functioning properly
    By tradeform2 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-11-2013, 05:45 PM
  4. [SOLVED] =IF Statement not functioning properly
    By MarcLewis in forum Excel General
    Replies: 4
    Last Post: 08-30-2012, 09:53 AM
  5. Formula not functioning properly
    By Spellbound in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-02-2007, 08:01 PM
  6. Read Only Not Functioning Properly
    By Shelagh in forum Excel General
    Replies: 0
    Last Post: 04-21-2006, 05:25 AM
  7. Excel 98 not functioning properly on formula
    By Michael Smith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2005, 09:05 PM

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