Macro to TRIM columns in different sheets with different start rows

agrospud

New Member
Joined
Apr 27, 2016
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am working on a macro to work with two sheets, with one sheet holding a source table for data to update a monthly pricelist in the destination sheet. The macro will add a description category from the source sheet to the destination sheet using a vba vlookup and then rearrange the layout of the pricelist the way I require it to look.

I noticed that the list of item codes for each item in the pricelist has a heap of trailing spaces after each item in the code list. I don’t know if this will vary as new lists are provided to me so I need to use TRIM to tidy up both the source item code and the reference code that the vlookup uses. Both lists of item codes are in column A of the two sheets but start in different row numbers. This caused me a bit of difficulty in trying to run the same TRIM code in two separate worksheets that start in different row numbers. I have been working on the components of the macro to get them working and then will combine them into one macro.

I have actually managed to get the TRIM part of the macro to work after much trial and error but I am not convinced I have done it the best way so am offering it up for advice on either improving it or alternatives please. The separate vlookup and layout components work well so are not shown.

Thanks.

VBA Code:
Sub Looping()

Dim ws As Worksheet
Dim Cel As Range

For Each ws In ActiveWorkbook.Worksheets
ws.Activate
If ws.Name = "SourceData" Then
For Each Cel In Range("A2", Range("A" & Rows.Count).End(xlUp))
Cel.Value = Trim(Cel.Value)
Next Cel
End If

If ws.Name = "Price List - Email" Then
For Each Cel In Range("A7", Range("A" & Rows.Count).End(xlUp))
Cel.Value = Trim(Cel.Value)
Next Cel
End If
Next

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This should be faster:
VBA Code:
Sub LoopingMod()

    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim i As Long
    
    Application.ScreenUpdating = False
   
    Set ws = Worksheets("SourceData")
    Set rng = ws.Range("A2:A" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
    arr = rng
    For i = 1 To UBound(arr)
        arr(i, 1) = Trim(arr(i, 1))
    Next i
    rng = arr
    
    Set ws = Worksheets("Price List - Email")
    Set rng = ws.Range("A7:A" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
    arr = rng
    For i = 1 To UBound(arr)
        arr(i, 1) = Trim(arr(i, 1))
    Next i
    rng = arr
    
    Application.ScreenUpdating = True

End Sub

PS: You might want to update your profile to show what version of Excel you are using.
 
Upvote 0
Hi Alex,
Works beautifully, thank you very much!
It has the added touch of the starting sheet staying as the active sheet, unlike my code which changed to the other one and it became the active one.
I have not used the UBound function before so will have to get my head around it.
Agrospud
 
Upvote 0
When you load a range into a variable that is just declared as a variant, it creates an array on the fly.
UBound returns what the max item number is in the array, so your For loop has an an end point for the item number counter.
(Not sure I’ve explained that very well but let me know how you go)

Anyway thanks for the feedback and glad I could help.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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