VBA to concatenate cells B&C into D, but then clear cell D if B&C are manually cleared - code running slow

stacemc

New Member
Joined
Oct 27, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try add Apllication.ScreenUpdating
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Application.ScreenUpdating = False
Call CombineCols
Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0
Try add Apllication.ScreenUpdating
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Application.ScreenUpdating = False
Call CombineCols
Application.ScreenUpdating = True
End If
End Sub
Its still causing a significant delay in processing unfortunately. Just incase this is important - There is a large code within this workbook in the module section, I'm adding this code into the specific sheets code.
 
Upvote 0
If changes trigger calculation, you want to delay the calculation until the end, else it will wait for calculation to finish before continue code run
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Call CombineCols
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End If
End Sub

It is always good to add those two lines to make code runs faster in most cases
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
 
Upvote 0
If changes trigger calculation, you want to delay the calculation until the end, else it will wait for calculation to finish before continue code run
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3:C100")) Is Nothing Then
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Call CombineCols
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End If
End Sub

It is always good to add those two lines to make code runs faster in most cases
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Ok that makes sense to delay the calculation. But now it doesn't run CombineCols at all - I'm guessing because it doesn't know when to do it? Do I need a button to trigger the code?
 
Upvote 0
I see that this is the sheet change trigger event. Try to put calculation in the CombineCols sub

VBA Code:
Sub CombineCols()

Dim oWS As Worksheet, lLastRow As Long, r As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

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
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

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
 
Upvote 0

Forum statistics

Threads
1,215,233
Messages
6,123,772
Members
449,123
Latest member
StorageQueen24

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