I need to perform worksheet TRIM function on all the columns of the worksheet in the workbook?

I need to perform worksheet TRIM function on all the columns of the worksheet in the workbook?

Below is the example:-
Open a Workbook (ABC.xlsx) on which i need to perform worksheet TRIM function. There is a vba code on the command button which is present on second workbook (XYZ.xlsx). But I am facing an issue that I am not able to activate the Workbook (ABC.xlsx) & worksheet (test) while running the VBA code from the command button on workbook (xyz.xlsx).

Below is the code for Reference ->

Dim r As Range
Set oExcel1 = CreateObject("Excel.Application")
Set oExcel1_Workbook1 = oExcel1.Workbooks.Open ("C:\NewFolder1\ NewFolder2\ NewFolder3\ABC.xlsx")
oExcel1.Application.Visible = True
Set oExcel1_Worksheet1 = oExcel1_Workbook1.Worksheets ("test")
ColCount1 = oExcel1_Worksheet1.Cells (1, oExcel1_Worksheet1.Columns.Count).End(xlToLeft).Column

For i = 1 to ColCount1
celladdres = oExcel1_Worksheet1.Cells(1,i).Address
rangeValue = replace(cellAddress,"$","")
Set r = Intersect(Range(rangeValue).EntireColumn, ActiveSheet.UsedRange)
''''''''' HERE I AM FACING ISSUE TO ACTIVATE THE (test) worksheet from (ABC.xlsx) workbook. As per the above code the ActiveSheet is the worksheet (Sheet1) of (xyz.xlsx) workbook and ActiveSheet.usedRange is giving value of worksheet (Sheet1) of (xyz.xlsx) workbook. Help with code to make (test) worksheet as active and get UsedRange from that worksheet. '''''''''
r.Value = Evaluate("IF(ROW(" & r.Address & "),IF(" & r.Address & "<>"""",TRIM(" & r.Address & "),""""))")
Next
Amit ShahAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
to activate a workbook, you can try:
Workbooks("yourExcel.xlsx").Activate

Open in new window

to activate a worksheet, you can try:
Worksheets("yourSheet").Select

Open in new window

Amit ShahAuthor Commented:
Below is the code i tried but still i am not able to activate the worksheet which i want ->
oExcel1_Workbook1.Activate
oExcel1_Worksheet1.Activate
oExcel1_Worksheet1.Select
Set r = Intersect(Range(rangeValue).EntireColumn, ActiveSheet.UsedRange)

--- Still i am facing same issue. I am not getting UsedRange of the activated worksheet. It is reffering the worksheet where command button is present.
Could you please help
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:
....
Dim ws As Worksheet
    Set ws = Workbooks("ABC.xlsx").Worksheets("test")
    ws.Activate
Set r = Intersect(Range(rangeValue).EntireColumn, ActiveSheet.UsedRange)
...

Open in new window

or:
...
Dim ws As Worksheet
    Set ws = Workbooks("ABC.xlsx").Worksheets("test")
Set r = Intersect(Range(rangeValue).EntireColumn, ws.UsedRange)
...

Open in new window

you may also modify:
Range(rangeValue).EntireColumn

Open in new window

to:
ws.Range(rangeValue).EntireColumn

Open in new window

Amit ShahAuthor Commented:
Thanks Ryan for the help.
Below is the answer which is working for me.

Dim r As Range
Set oExcel1 = CreateObject("Excel.Application")
Set oExcel1_Workbook1 = oExcel1.Workbooks.Open ("C:\NewFolder1\ NewFolder2\ NewFolder3\ABC.xlsx")
oExcel1.Application.Visible = True
Set oExcel1_Worksheet1 = oExcel1_Workbook1.Worksheets ("test")
oExcel1.Workbooks("ABC.xlsx").Activate
oExcel1_Workbook1.Worksheets("test").Activate

ColCount1 = oExcel1_Worksheet1.Cells (1, oExcel1_Worksheet1.Columns.Count).End(xlToLeft).Column

For i = 1 to ColCount1
celladdres = oExcel1_Worksheet1.Cells(1,i).Address
rangeValue = replace(cellAddress,"$","")
Set r = oExcel1.Intersect(oExcel1_Workbook1.Worksheets("test").Range(rangeValue).EntireColumn, oExcel1_Workbook1.Worksheets("test").UsedRange)
oExcel1_Workbook1.Worksheets("test").Select
r.Value = oExcel1.Evaluate("IF(ROW(" & r.Address & "),IF(" & r.Address & "<>"""",TRIM(" & r.Address & "),""""))")

Next
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
ok, glad that u managed to solved the issues. let me know if my posts helped and led u to find the solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.