• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Combining 2 VBA Codes

Dokat

Member
Hi,

Can someone please help me combine below 2 codes?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Updateby Extendoffice 20160725
    Dim xFormula As String
    On Error GoTo Out:
    xFormula = Target.Cells(1).Validation.Formula1
    If Left(xFormula, 1) = "=" Then
        Target.Cells(1) = Range(Mid(xFormula, 1)).Cells(1).Value
    End If
Out:
End Sub

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r1 As Range, r2 As Range, r3 As Range
    Set r1 = Range("CE1")
    Set r2 = Range("CF1")
    Set r3 = Range("E2")
    Set r4 = Range("F2")
    If r1.Value = 1 Then
        r3.Interior.Color = vbWhite
    ElseIf r2.Value = 1 Then
        r4.Interior.Color = vbWhite
    Else
        r3.Interior.Color = 6
    End If
End Sub
 
Taking a stab at it. See if this works.

Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Updateby Extendoffice 20160725
  Dim xFormula As String
  Dim r1 As Range, r2 As Range, r3 As Range
 
  Set r1 = Range("CE1")
    Set r2 = Range("CF1")
    Set r3 = Range("E2")
    Set r4 = Range("F2")
    If r1.Value = 1 Then
        r3.Interior.Color = vbWhite
    ElseIf r2.Value = 1 Then
        r4.Interior.Color = vbWhite
    Else
        r3.Interior.Color = 6
    End If
 
    On Error GoTo Out:
    xFormula = Target.Cells(1).Validation.Formula1
    If Left(xFormula, 1) = "=" Then
        Target.Cells(1) = Range(Mid(xFormula, 1)).Cells(1).Value
    End If
Out:
End Sub
 
Back
Top