Trim entire active row macro and delete 3 rows

miva0601

New Member
Joined
Mar 31, 2015
Messages
33
I am trying to trim the entire active row. Currently the macro is only trimming row 4 if my cell is in row 4.


Sub Trim()
'
' Trim Macro
'
' Keyboard Shortcut: Ctrl+t
'
[4:4] = [if(4:4<>"",trim(4:4),"")]


End Sub

Also, can anyone help create a macro that will trim all data in row 3 -- and then delete rows 1,2, and 4?


1AP Transaction Charges - *POSTED BY PART #
2
3Order/ship reference Item Their reference QuantityUM Currency
4________________________________________________________________________________________________________
5P220377 2880222-01 5066390EA USD
6P220377 2880222-01 5153660EA USD
7P220377 2880222-01 51439150EA USD
8P221782 2880222-01 5177050EA USD

<colgroup><col><col span="6"></colgroup><tbody>
</tbody>

<colgroup><col><col span="8"></colgroup><tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Code:
Sub t()
Dim c As Range
For Each c In Range(ActiveCell, Cells(ActiveCell.Row, Columns.Count).End(xlToLeft))
    If c.Value <> "" Then
        c = Trim(c.Value)
    End If
Next
End Sub
 
Upvote 0
I'm getting an error message: Wrong number of arguments or invalid property assignment. I added '.Value' to the last part -- this was causing an error too.

Code:
Sub Trim()
Dim c As Range
For Each c In Range(ActiveCell, Cells(ActiveCell.Row, Columns.Count).End(xlToLeft))
    If c.Value <> "" Then
        c.Value = Trim(c.Value)
    End If
Next
End Sub
 
Upvote 0
I guess I will have to research the reason that is not working. It should. Anyway, here is a modified version which will work.
Code:
Sub Trim()
Dim c As Range, a As String
For Each c In Range(ActiveCell, Cells(ActiveCell.Row, Columns.Count).End(xlToLeft))
    If c.Value <> "" Then
        c = LTrim(c)
        c = RTrim(c)
    End If
Next
End Sub
 
Upvote 0
Looks like it has to be qualified with Application to get it to work in this case, although I have used it many times before without that and got the correct results. Just another of those odd microsoft anomalies.
Code:
Sub t()
Dim c As Range
For Each c In Range(ActiveCell, Cells(ActiveCell.Row, Columns.Count).End(xlToLeft))
    If c <> "" Then
        c = Application.Trim(c)
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,663
Messages
6,056,626
Members
444,879
Latest member
suzndush

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