We help IT Professionals succeed at work.
Get Started

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

91 Views
Last Modified: 2016-02-11
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
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE