Backspace a cell using VBA

Kaiser TCF

New Member
Joined
Jul 20, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
hi,
fyi, I am using 2019 Excel


so in short. I want a VBA code that do this in excelsheet: "F2" -> "BackSpace"
(F2 enables editing a cell)

basically, I wrote a code that imports text into excel sheet cells. and I noticed that each cell have a new empty paragraph after the end of the text.

deleting the empty paraph is easy. just "F2" ->"Backspace". but I have to do it for hundreds of cells so thats why I need to write a code

thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long
 ar = Range("A1", Range("A" & Rows.Count).End(xlUp))
 
 For i = 1 To UBound(ar)
    ar(i, 1) = Mid(ar(i, 1), 1, Len(ar(i, 1)) - 1)
 Next
 
 Range("A1", Range("A" & Rows.Count).End(xlUp)) = ar
End Sub
 
Upvote 0
Try this

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long
 ar = Range("A1", Range("A" & Rows.Count).End(xlUp))
 
 For i = 1 To UBound(ar)
    ar(i, 1) = Mid(ar(i, 1), 1, Len(ar(i, 1)) - 1)
 Next
 
 Range("A1", Range("A" & Rows.Count).End(xlUp)) = ar
End Sub
thanks! it works
 
Upvote 0
Why not process all the cells at once?

If the extra character only occurs once in each cell then you should be able to just Find/Replace.
Would have to identify what that last character is (I have guessed CHAR(10) or Alt+Enter)
VBA Code:
Columns("A").Replace What:=Chr(10), Replacement:="", LookAt:=xlPart

If the extra character may also appear earlier in the cell and you don't want it removed from there, or if you can't/don't want to identify that last character you can still do them all at once ..

VBA Code:
With Range("A1", Range("A" & Rows.Count).End(xlUp))
  .Value = Evaluate(Replace("if(#="""","""",left(#,len(#)-1))", "#", .Address))
End With
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,007
Members
449,203
Latest member
Daymo66

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