He All,
I've got the below VBA which concatenates cells B&C into D, but also clears cell D if B&C are manually cleared. It works but it is very slow. If I remove the if statement for clearing the cells the concatenate works perfectly fast by itself. I'm wondering on how to improve this so that the sheet isn't extremely slow but having both if statements. I am very new to VBA and worked this code out through google searches so please bare with me if this is a simple/obvious fix.
Sub CombineCols()
Dim oWS As Worksheet, lLastRow As Long, r As Long
Set oWS = ActiveSheet
lLastRow = oWS.Cells.SpecialCells(xlLastCell).row
For r = 3 To lLastRow
' Combine if both B and C are not empty
If Len(oWS.Cells(r, 2)) > 0 And Len(oWS.Cells(r, 3)) > 0 Then
oWS.Cells(r, 4).Value = "Endorced - " & oWS.Cells(r, 2).Value & " - " & oWS.Cells(r, 3).Value
End If
' Clear contents if both B and C are empty
If Len(oWS.Cells(r, 2)) = 0 And Len(oWS.Cells(r, 3)) = 0 Then
oWS.Cells(r, 4).Value = ClearContents
End If
Next
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Call CombineCols
End If
End Sub
I've got the below VBA which concatenates cells B&C into D, but also clears cell D if B&C are manually cleared. It works but it is very slow. If I remove the if statement for clearing the cells the concatenate works perfectly fast by itself. I'm wondering on how to improve this so that the sheet isn't extremely slow but having both if statements. I am very new to VBA and worked this code out through google searches so please bare with me if this is a simple/obvious fix.
Sub CombineCols()
Dim oWS As Worksheet, lLastRow As Long, r As Long
Set oWS = ActiveSheet
lLastRow = oWS.Cells.SpecialCells(xlLastCell).row
For r = 3 To lLastRow
' Combine if both B and C are not empty
If Len(oWS.Cells(r, 2)) > 0 And Len(oWS.Cells(r, 3)) > 0 Then
oWS.Cells(r, 4).Value = "Endorced - " & oWS.Cells(r, 2).Value & " - " & oWS.Cells(r, 3).Value
End If
' Clear contents if both B and C are empty
If Len(oWS.Cells(r, 2)) = 0 And Len(oWS.Cells(r, 3)) = 0 Then
oWS.Cells(r, 4).Value = ClearContents
End If
Next
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Call CombineCols
End If
End Sub