Combining VBA Codes

JackReacher85

Banned user
Joined
Sep 14, 2021
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
All,

I have two lots of VBA that i need to have combined, my goal is to have a multi select drop down list applied only to cells in Column M ( first code) and add the date in to column O when all cells from Col A to M are completed (second code)

This is the multi select drop down code
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim selectedValue As String
    Dim oldValue As String

    ' Check if the change is within column M
    If Not Intersect(Target, Me.Range("M:M")) Is Nothing Then
        Application.EnableEvents = False
        For Each cell In Target
            If Not IsEmpty(cell.Value) Then
                ' If the cell already contains data, append the new value
                oldValue = cell.OldValue
                selectedValue = cell.Value
                If oldValue = "" Then
                    cell.Value = selectedValue
                Else
                    ' Avoid duplication
                    If InStr(1, oldValue, selectedValue, vbTextCompare) = 0 Then
                        cell.Value = oldValue & ", " & selectedValue
                    End If
                End If
            End If
        Next cell
        Application.EnableEvents = True
    End If
End Sub


This is the date addition code.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rowNum As Long
    Dim lastCol As Long
    Dim cell As Range
    Dim isComplete As Boolean

    ' Define the columns to check
    lastCol = 13 ' Column M

    ' Check if the change affects columns A to M
    If Not Intersect(Target, Me.Range("A:M")) Is Nothing Then
        Application.EnableEvents = False
        For Each cell In Target
            rowNum = cell.Row
            If rowNum > 1 Then ' Skip header row if any
                isComplete = True
                For i = 1 To lastCol
                    If IsEmpty(Me.Cells(rowNum, i)) Then
                        isComplete = False
                        Exit For
                    End If
                Next i
                
                ' If complete, add date to column O
                If isComplete Then
                    Me.Cells(rowNum, 15).Value = Date
                Else
                    ' Optionally clear date if not all cells are complete
                    Me.Cells(rowNum, 15).ClearContents
                End If
            End If
        Next cell
        Application.EnableEvents = True
    End If
End Sub

Ive racked my brain over this and cant seem to figure it out, any help is greatly appreciated.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
oldValue = cell.OldValue
That line in your first code doesn't work for me, but if it works for you then it's correct. In fact, I'm not going to touch the first code, I'm just going to add the second code.


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim selectedValue As String
    Dim oldValue As String

    ' Check if the change is within column M
    If Not Intersect(Target, Me.Range("M:M")) Is Nothing Then
        Application.EnableEvents = False
        For Each cell In Target
            If Not IsEmpty(cell.Value) Then
                ' If the cell already contains data, append the new value
                'oldValue = cell.oldValue
                oldValue = cell.Value
                selectedValue = cell.Value
                If oldValue = "" Then
                    cell.Value = selectedValue
                Else
                    ' Avoid duplication
                    If InStr(1, oldValue, selectedValue, vbTextCompare) = 0 Then
                        cell.Value = oldValue & ", " & selectedValue
                    End If
                End If
            End If
        Next cell
        Application.EnableEvents = True
    End If
    
    
    'add the date in to column O when all cells from Col A to M are completed
    Dim rowNum As Long
    Dim lastCol As Long, i As Long
    Dim isComplete As Boolean

    ' Define the columns to check
    lastCol = 13 ' Column M

    ' Check if the change affects columns A to M
    If Not Intersect(Target, Me.Range("A:M")) Is Nothing Then
        Application.EnableEvents = False
        For Each cell In Target
            rowNum = cell.Row
            If rowNum > 1 Then ' Skip header row if any
                isComplete = True
                For i = 1 To lastCol
                    If IsEmpty(Me.Cells(rowNum, i)) Then
                        isComplete = False
                        Exit For
                    End If
                Next i
                
                ' If complete, add date to column O
                If isComplete Then
                    Me.Cells(rowNum, 15).Value = Date
                Else
                    ' Optionally clear date if not all cells are complete
                    Me.Cells(rowNum, 15).ClearContents
                End If
            End If
        Next cell
        Application.EnableEvents = True
    End If
End Sub

----- --
Try the combined code and let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Hey Dante,

Im afraid this code only works for adding the date in it doesnt work with the multi-select drop down function only being applied to the cells in Column M.

Appreciate any other suggestions you may have :)
Thanks in advance
 
Upvote 0
doesnt work with the multi-select drop down function
As I told you in post #2, I did not modify your code and I warned you that the code does not work.
Now you can comment if the code worked for you.

This property does not exist: cell.OldValue, Or do you have more code in your sheet that you didn't put in your original post?
 
Upvote 0

Forum statistics

Threads
1,221,289
Messages
6,159,042
Members
451,534
Latest member
zooster

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