PDA

View Full Version : [SOLVED:] MERGE 2 DİFFERENT VBA CODE



engbaris
03-15-2017, 11:34 AM
hi everyone,:hi:

I have 2 different code which one of this allows you to select multiple item from a drop-down list while the other code sends automatic outlook mail.

You can find these two code following;

As far as I know, to be able to run these code in the same excel sheet, ı have to merge these codes.

Can you help me to do this?




Private Sub Worksheet_Change(ByVal Target As Range)


Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 5 And Target.Row > 2 And Target.Row < 1038 Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub








Option Explicit


Private Sub Worksheet_Calculate()
Dim FormulaRange As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim MyLimit As Double

NotSentMsg = "Not Sent"
SentMsg = "Sent"


MyLimit = 0



Set FormulaRange = Me.Range("U3:U15")


On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If IsNumeric(.Value) = False Then
MyMsg = "Not numeric"
Else
If .Value = MyLimit Then
MyMsg = SentMsg
If .Offset(0, 1).Value = NotSentMsg Then
Call Mail_with_outlook
End If
Else
MyMsg = NotSentMsg
End If
End If
Application.EnableEvents = False
.Offset(0, 1).Value = MyMsg
Application.EnableEvents = True
End With
Next FormulaCell


ExitMacro:
Exit Sub


EndMacro:
Application.EnableEvents = True


MsgBox "Some Error occurred." _
& vbLf & Err.Number _
& vbLf & Err.Description


End Sub

offthelip
03-15-2017, 04:02 PM
Since these two subroutines are triggered by different worksheet events why can't you just copy them both to the worksheet that you want to run them on. They will both work in exactly as they did in the independent worksheets.

engbaris
03-15-2017, 09:55 PM
thanks for you reply. ı made a mistake.

ı want to use these codes in the same worksheet.

regards,

offthelip
03-16-2017, 01:50 AM
That is exactly what I thought: just copy both subroutines to the worksheet where you want them to run, they will both work. They don't interfer with each other as far as I can tell.

engbaris
03-16-2017, 10:10 AM
thanks a lot, you are right. it work well now.

offthelip
03-16-2017, 10:26 AM
Mark the thread as solved if you are happy with the answer, go to thread tools at the top.