Need to double click on a cell before VBA code changes applies

ose

New Member
Joined
Nov 6, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi.

I have following Command Button which executed following code in order to remove empty line breaks from a given cell:

1699261526855.png

VBA Code:
Private Sub CommandButton1_Click()
 Cells(24, "M").Select
Selection.Replace What:="" & Chr(10) & "" & Chr(10) & "", Replacement:="" & Chr(10) & ""
End Sub

The problem is that nothing happens when I execute the code. The changes from code only applies when:

1 - I manually double click on the cell ("Edit Mode")
1699261640470.png

2 - Clicking anywhere else to remove focus on the cell
3 - Reexecuting the code

The problem is that I manually need to double click on each cell to get it to work.
The cell is copied from a table that is loaded from external source.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
See if the following gives you what you want (assumes M24 is on the active sheet when run)
VBA Code:
Private Sub CommandButton1_Click()
    Cells(24, "M").Replace Chr(10) & Chr(10), Chr(10), xlPart
End Sub
 
Upvote 0
This might be better...
VBA Code:
Private Sub CommandButton1_Click()
    Dim i As Long: i = 2
    Do Until i < 2
        i = InStr(1, Range("M24"), Chr(10) & Chr(10))
        Range("M24").Replace Chr(10) & Chr(10), Chr(10), xlPart
    Loop
End Sub
 
Upvote 0
Exceeded the time limit to edit; remove the Private, just:
VBA Code:
Sub CommandButton1_Click()
 
Upvote 0
Solution
Thanks for your reply @kevin9999

I tested and verified that it works for a single cell, which is great ! :).

I have tried to get it to work with multiple cells at once by extending the range functions to "M24:M28" but it did not work unfortunately:

Do you know how to apply this solution to multiple cells ?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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