AutoFill to the last row

bobrandom123

New Member
Joined
Apr 19, 2012
Messages
11
I created a macro utilizing Excel 2007 in order to automate the process of using the TRIM function in column S to trim strings found in Column H and then AutoFill the entire column:

Code:
Sub TrimFileName()
Range("S2").Select
ActiveCell.FormulaR1C1 = "=TRIM(MID(RC[-11],24,25))"
Range("S2").Select
Selection.AutoFill Destination:=Range("S2:S65536")
Columns("S:S").EntireColumn.Autofit

End Sub

I am new to programming and I want to use macros in order to parse my data faster at work. Since the amount of data varies from sheet to sheet, how can I programatically get the range to go to the last row in the worksheet instead of it going to 65536?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try

Code:
Sub TrimFileName()
Dim LR As Long
LR = Range("H" & Rows.Count).End(xlUp).Row
Range("S2").FormulaR1C1 = "=TRIM(MID(RC[-11],24,25))"
Range("S2").AutoFill Destination:=Range("S2:S" & LR)
Columns("S").EntireColumn.AutoFit
End Sub
 
Upvote 0
You are welcome. In fact it could be simplified

Code:
Sub TrimFileName()
Dim LR As Long
LR = Range("H" & Rows.Count).End(xlUp).Row
With Range("S2")
    .FormulaR1C1 = "=TRIM(MID(RC[-11],24,25))"
    .AutoFill Destination:=Range("S2:S" & LR)
    .EntireColumn.AutoFit
End With
End Sub
 
Upvote 0
That does simplify things! How could I integrate the TrimFileName() macro with a copy/paste as values solution? Currently I have this code that removes the formula for the TRIM function:

Code:
Sub RemoveFormula()
    Columns("S:S").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("S1").Select
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Try

Code:
Sub TrimFileName()
Dim LR As Long
LR = Range("H" & Rows.Count).End(xlUp).Row
With Range("S2")
    .FormulaR1C1 = "=TRIM(MID(RC[-11],24,25))"
    .AutoFill Destination:=Range("S2:S" & LR)
    .EntireColumn.AutoFit
End With
Range("S2:S" & LR).Value = Range("S2:S" & LR).Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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