Copied sheet code to new file and lost some functionality....not sure how/why

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
77
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I had a wonderful bunch of code working perfectly on a project, but I have been asked to change the formatting and layout of the sheet and thought it would be easiest to create a new file and transfer sheet code across.
In doing so, some of the functionality is not working and I can't for the life of me figure it out.
Not sure the best way to get you up to speed but attached is a mini sheet, and below is the sheet code. I have highlighted in red the sections that are not triggering when the change is made on the sheet.
It should be known, I didn't write this code, it was made up from contributions from some very handy people on here as my skills are lacking somewhat.

Thanks in advance for your help.
Hayden

Rich (BB code):
Dim pVal
Dim swapval As Boolean

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
pVal = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Resp As String
Dim prevValue
prevValue = pVal

On Error GoTo ExitNow
Application.EnableEvents = False
If Target.CountLarge = 1 And Not Intersect(Target, Range("G10:T153")) Is Nothing Then '<--- Change Target Range Here
    If swapval = False Then
'---EDO/EDO-U
        If prevValue = "EDO" Or prevValue = "EDO-U" Then
            If InStr(Target.Value, "0") Then
                With Target
                    ActiveSheet.Unprotect
                    .Interior.Color = RGB(0, 176, 240)
                    .Font.Color = RGB(255, 0, 0)
                    'Input Box below. change text and title as needed:
                    Resp = Application.InputBox("You are allocating an open shift to a DAO on an EDO.         Please include details of when this shift was added and notify the DAO at the earliest opportunity.", _
                    Title:="Open shift added on an EDO")
                    ActiveSheet.Protect DrawingObjects:=False
                End With
            End If
        Else
'-------OFF/OFF-U
            If prevValue = "OFF" Or prevValue = "OFF-U" Then
                If InStr(Target.Value, "0") Then
                    With Target
                        ActiveSheet.Unprotect
                        .Interior.Color = RGB(255, 255, 0)
                        .Font.Color = RGB(255, 0, 0)
                        'Input Box below. change text and title as needed:
                    Resp = Application.InputBox("You are allocating an open shift to a DAO on a day OFF.         Please include details of when this shift was added and notify the DAO at the earliest opportunity.", _
                    Title:="Open shift added on a day OFF")
                        ActiveSheet.Protect DrawingObjects:=False
                    End With
                End If
            End If
        End If
    Else
        swapval = False
    End If
'---Absenteeism Details
    With Target
        Select Case .Value
            Case "SDO", "STFN", "CDO", "CTFN" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of absenteeism", _
                Title:="Absenteeism Details")
            Case "OFF-U", "EDO-U" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of DAO request to be marked unavailable on OFF/EDO.", _
                Title:="OFF/EDO Unavailability Details")
        End Select
    End With
'---DAO Shift Extension Confirmation/Decline
    With Target
        If InStr(1, .Value, "OK") > 0 Then
            Resp = Application.InputBox("Please enter details of when this shift extension was confirmed by the DAO. " & _
            "Including time and date and how it was confirmed", "DAO Shift Extension Confirmation", , , , , , 2)
        Else
            If InStr(1, .Value, "DEC") > 0 Then
                Resp = Application.InputBox("Please enter details of when this shift extension was declined by the DAO. " & _
                "Including time and date when it was declined", "DAO Shift Extension Declined", , , , , , 2)
        Else
            If InStr(1, .Value, "?") > 0 Then
                Resp = Application.InputBox("Please enter details of when this shift extension was added to the DAOs roster. " & _
                "Including time and date when it was added", "DAO Shift Extension added", , , , , , 2)
                End If
            End If
        End If
    End With
    Call AddComment(Target, Resp)
End If

ExitNow:
Application.EnableEvents = True
End Sub

Sub AddComment(rng As Range, cTxt As String)
With rng
    If Len(cTxt) > 0 And cTxt <> "False" Then
        If .Comment Is Nothing Then
            ActiveSheet.Unprotect
            .AddComment Text:=cTxt
            ActiveSheet.Protect DrawingObjects:=False
        Else
            ActiveSheet.Unprotect
            .Comment.Text .Comment.Text & vbLf & cTxt
            ActiveSheet.Protect DrawingObjects:=False
        End If
    End If
End With
End Sub
Sub ActionSwap()
Dim sCmt As String
Dim i As Long
Dim rCell As Range
Dim area1 As Variant, area2 As Variant, swapval As Variant
   
sCmt = InputBox( _
  Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
  "Comment will be added to all cells in Selection", _
  Title:="DAO Swap Details")
If sCmt = "" Then
    MsgBox "No comment added"
Else
    For Each rCell In Selection
    With rCell
        If .Comment Is Nothing Then
        .AddComment.Text sCmt
    Else
        .Comment.Text sCmt & vbLf & .Comment.Text
    End If
End With
Next
End If
Set rCell = Nothing
If Selection.Areas.Count <> 2 Then Exit Sub
If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
    MsgBox ("Selection areas must have the same number of columns")
    Exit Sub
End If
area1 = Selection.Areas(1)
area2 = Selection.Areas(2)
If Selection.Areas(1).Columns.Count = 1 Then
    swapval = area1
    area1 = area2
    area2 = swapval
Else
    For i = LBound(area1, 2) To UBound(area1, 2)
        swapval = area1(1, i)
        area1(1, i) = area2(1, i)
        area2(1, i) = swapval
    Next
End If
Selection.Areas(1) = area1
Selection.Areas(2) = area2
swapval = True
End Sub


DIGITAL ROSTER MACH2.xlsm
ABCDEFGHIJKLMNOPQRST
1
2FORTNIGHT COMMENCINGSUNDAYMONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAYMONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAY
3              
4Employee NameOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen ShiftsOpen Shifts
5#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!#CALC!
6
7
8
9
10DAO Team Leaders
11Line 1Rostered T/L Shift0530TL0530TL0530TL0530TL0530TLOFFOFFOFFOFFOFF0530TL0530TL0530TL0530TL
12
13Original T/L 
14Remarks9
15Line 2Rostered T/L Shift1330TL1330TLOFFOFF1330TL1330TL1330TL1330TL1330TL1330TL1330TLOFFOFFOFF
16
17Original T/L 
18Remarks9
19Line 3Rostered T/L ShiftOFFOFF1330TL1330TL2130TL2130TL2130TL2130TL2130TLOFFOFFOFF1330TL1330TL
20
21Original T/L 
22Remarks9
23Line 4Rostered T/L Shift2130TL2130TL2130TL2130TLOFFOFFOFFOFFOFF2130TL2130TL2130TL2130TL2130TL
24
25Original T/L 
26Remarks9
27Line 5Rostered T/L ShiftOFFOFFOFF0700AD0700AD0530TL0530TL0530TL0530TL0530TLOFF1330TL1200ADOFF
28
29Original T/L 
30Remarks9
MASTER
Cell Formulas
RangeFormula
G3G3=IF(A4="","",A4)
H3:T3H3=IF(G3="","",G3+1)
G5,M5:N5,T5G5=FILTER('https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - TRIAL (LIVE).xlsm]DATA'!$AL$3:$AL$17,NOT(COUNTIF(G11:G136,'https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - TRIAL (LIVE).xlsm]DATA'!$AL$3:$AL$17)))
H5:L5,O5:S5H5=FILTER('https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - TRIAL (LIVE).xlsm]DATA'!$AM$3:$AM$19,NOT(COUNTIF(H11:H136,'https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - TRIAL (LIVE).xlsm]DATA'!$AM$3:$AM$19)))
C11,C27,C23,C19,C15C11=IF($A$4="","",VLOOKUP(CONCATENATE(VALUE($A$4),$A11),'https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - trial version backup.xlsm]DATA'!$U$3:$V$1568,2,0))
C13,C29,C25,C21,C17C13=IF($A$4="","",VLOOKUP(CONCATENATE(VALUE($A$4),$A11),'https://metrotrains-my.sharepoint.com/personal/hayden_meaclem_metrotrains_com_au/Documents/[DAO DIGITAL ROSTER - trial version backup.xlsm]DATA'!$U$3:$V$1568,2,0))
E14,E30,E26,E22,E18E14=SUM(AK11:AX11)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G5:T5Cellcontains an errortextNO
E14,E18,E22,E26,E30Cell Value>=12textNO
E14,E18,E22,E26,E30Cell Value=11textNO
E14,E18,E22,E26,E30Cell Value=10textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "GAZETTE"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "AV"textNO
E14,E18,E22,E26,E30Cell Value=9textNO
E14,E18,E22,E26,E30Cellcontains an errortextNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "EDO-U"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "OFF-U"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "BLV"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "CTFN"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "CDO"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "STFN"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "SDO"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "LSL"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "A/L"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "PHC"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "OFF"textNO
G11:T11,G30:T30,G14:T15,G22:T23,G26:T27,G18:T19Cell Valuecontains "EDO"textNO
C13,C17,C21,C25,C29Expression=IF(C13=C11,TRUE)textNO
C2Cell Valuecontains "UNPUBLISHED"textNO
C2Cell Valuecontains "PUBLISHED"textNO
G6:T9Cellcontains an errortextNO
G14:T14,G18:T18,G22:T22,G26:T26,G30:T30Expression=NOT(ISERROR(FIND("OK",#REF!)))textNO
Cells with Data Validation
CellAllowCriteria
G11:T13List=DATA!$AF$4:$AF$47
G15:T17List=DATA!$AF$4:$AF$47
G19:T21List=DATA!$AF$4:$AF$47
G23:T25List=DATA!$AF$4:$AF$47
G27:T29List=DATA!$AF$4:$AF$47
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
G'day all,
I have this code which works perfectly, however I have needed to change the formatting of the sheet and merge the cells that would be targetted. I believe this is stopping the code from being executed.
Is there something I can change in the code to have it action on merged cells?
Should be mentioned not all the target cells are merged, some are made up of 1 single cell or up to a total of 3 cells merged.

VBA Code:
Dim pVal
Dim swapval As Boolean

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
pVal = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Resp As String
Dim prevValue
prevValue = pVal

On Error GoTo ExitNow
Application.EnableEvents = False
If Target.CountLarge = 1 And Not Intersect(Target, Range("G11:T178")) Is Nothing Then '<--- Change Target Range Here
    If swapval = False Then
'---EDO/EDO-U
        If prevValue = "EDO" Or prevValue = "EDO-U" Then
            If InStr(Target.Value, "0") Then
                With Target
                    ActiveSheet.Unprotect
                    .Interior.Color = RGB(0, 176, 240)
                    .Font.Color = RGB(255, 0, 0)
                    'Input Box below. change text and title as needed:
                    Resp = Application.InputBox("You are allocating an open shift to a DAO on an EDO.         Please include details of when this shift was added and notify the DAO at the earliest opportunity.", _
                    Title:="Open shift added on an EDO")
                    ActiveSheet.Protect DrawingObjects:=False
                End With
            End If
        Else
'-------OFF/OFF-U
            If prevValue = "OFF" Or prevValue = "OFF-U" Then
                If InStr(Target.Value, "0") Then
                    With Target
                        ActiveSheet.Unprotect
                        .Interior.Color = RGB(255, 255, 0)
                        .Font.Color = RGB(255, 0, 0)
                        'Input Box below. change text and title as needed:
                    Resp = Application.InputBox("You are allocating an open shift to a DAO on a day OFF.         Please include details of when this shift was added and notify the DAO at the earliest opportunity.", _
                    Title:="Open shift added on a day OFF")
                        ActiveSheet.Protect DrawingObjects:=False
                    End With
                End If
            End If
        End If
    Else
        swapval = False
    End If
'---Absenteeism Details
    With Target
        Select Case .Value
            Case "SDO", "STFN", "CDO", "CTFN" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of absenteeism", _
                Title:="Absenteeism Details")
            Case "OFF-U", "EDO-U" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of DAO request to be marked unavailable on OFF/EDO.", _
                Title:="OFF/EDO Unavailability Details")
        End Select
    End With
'---DAO Shift Extension Confirmation/Decline
    With Target
        If InStr(1, .Value, "OK") > 0 Then
            Resp = Application.InputBox("Please enter details of when this shift extension was confirmed by the DAO. " & _
            "Including time and date and how it was confirmed", "DAO Shift Extension Confirmation", , , , , , 2)
        Else
            If InStr(1, .Value, "DEC") > 0 Then
                Resp = Application.InputBox("Please enter details of when this shift extension was declined by the DAO. " & _
                "Including time and date when it was declined", "DAO Shift Extension Declined", , , , , , 2)
        Else
            If InStr(1, .Value, "?") > 0 Then
                Resp = Application.InputBox("Please enter details of when this shift extension was added to the DAOs roster. " & _
                "Including time and date when it was added", "DAO Shift Extension added", , , , , , 2)
                End If
            End If
        End If
    End With
    Call AddComment(Target, Resp)
End If

ExitNow:
Application.EnableEvents = True
End Sub

Sub AddComment(rng As Range, cTxt As String)
With rng
    If Len(cTxt) > 0 And cTxt <> "False" Then
        If .Comment Is Nothing Then
            ActiveSheet.Unprotect
            .AddComment Text:=cTxt
            ActiveSheet.Protect DrawingObjects:=False
        Else
            ActiveSheet.Unprotect
            .Comment.Text .Comment.Text & vbLf & cTxt
            ActiveSheet.Protect DrawingObjects:=False
        End If
    End If
End With
End Sub
Sub ActionSwap()
Dim sCmt As String
Dim i As Long
Dim rCell As Range
Dim area1 As Variant, area2 As Variant, swapval As Variant
    
sCmt = InputBox( _
  Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
  "Comment will be added to all cells in Selection", _
  Title:="DAO Swap Details")
If sCmt = "" Then
    MsgBox "No comment added"
Else
    For Each rCell In Selection
    With rCell
        If .Comment Is Nothing Then
        .AddComment.Text sCmt
    Else
        .Comment.Text sCmt & vbLf & .Comment.Text
    End If
End With
Next
End If
Set rCell = Nothing
If Selection.Areas.Count <> 2 Then Exit Sub
If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
    MsgBox ("Selection areas must have the same number of columns")
    Exit Sub
End If
area1 = Selection.Areas(1)
area2 = Selection.Areas(2)
If Selection.Areas(1).Columns.Count = 1 Then
    swapval = area1
    area1 = area2
    area2 = swapval
Else
    For i = LBound(area1, 2) To UBound(area1, 2)
        swapval = area1(1, i)
        area1(1, i) = area2(1, i)
        area2(1, i) = swapval
    Next
End If
Selection.Areas(1) = area1
Selection.Areas(2) = area2
swapval = True
End Sub

Cheers
Hayden
 
Upvote 0
Is this really the same question as your post from yesterday here?

Your code in both looks to be the same, so I suspect you are really asking the same question.
If so, you should continue in the original thread instead of starting a second thread for the same question.
 
Upvote 0
Is this really the same question as your post from yesterday here?

Your code in both looks to be the same, so I suspect you are really asking the same question.
If so, you should continue in the original thread instead of starting a second thread for the same question.
Hey Joe,
Yes, you're right. I wasn't sure what the issue was with the code, but since discovered it's the merged cells, hence the new question. My error.
Happy to have either question deleted if appropriate.

Cheers
Hayden
 
Upvote 0
OK, I have merged your two threads, as per rule 12 here: Copied sheet code to new file and lost some functionality....not sure how/why

Regarding your question, I would very highly recommend getting rid of the merged cells if at all possible. Merged cells are perhaps the worst single feature of Excel, and cause all sorts of issues for things like VBA, sorting, etc.

If you are merging cells across single rows, you can get the exact same visual effect without all the issues by using the "Center Across Selection" formatting option instead of using merged cells.
See here: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis
 
Upvote 0
OK, I have merged your two threads, as per rule 12 here: Copied sheet code to new file and lost some functionality....not sure how/why

Regarding your question, I would very highly recommend getting rid of the merged cells if at all possible. Merged cells are perhaps the worst single feature of Excel, and cause all sorts of issues for things like VBA, sorting, etc.

If you are merging cells across single rows, you can get the exact same visual effect without all the issues by using the "Center Across Selection" formatting option instead of using merged cells.
See here: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis
Thanks Joe! Appreciate the help and apologises for the double up. Noted for future.
Cheers
 
Upvote 0

Forum statistics

Threads
1,223,936
Messages
6,175,499
Members
452,650
Latest member
Tinfish

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top