How do I change selected colors of this vba automatically?

VictorKZ

New Member
Joined
Sep 13, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,
I'm having trouble changing my Macro's colors when it automatically selects fields.
Does anyone have any idea how I can solve this problem?
Thanks for listening.

This code VBA i'm use:
VBA Code:
Sub RemoveLeadingSpace()

Dim Rng, WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
  For Each Rng In WorkRng
    Rng.Value = VBA.LTrim(Rng.Value)
Next
For Each Rng In WorkRng
    Rng.Value = VBA.RTrim(Rng.Value)
Next

MsgBox ("Formula was used in this Guide")
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi & welcome to MrExcel.
What exactly do you mean by "changing my Macro's colors when it automatically selects fields" that macro is not changing any colours & it doesn't select anything.
 
Upvote 0
Hi, i am sorry, i expressed myself wrong.
What i acctually want to do is to highlight cells
 
Upvote 0
Hi, i am sorry, i expressed myself wrong.
What i acctually want to do is to highlight cells whenever that macro is applied.
Can you help me?
Thanks
 
Upvote 0
Which cells?
What colour?
 
Upvote 0
Okay, in my first comment I added a macro that trims blank spaces in the beggig and end of a cell.
So I need that everytime this trim is apply the cell is colored to indicate that that macro was apply.

In other words, I need to highlight the cells wich my macro was apply.

Thank you!
 
Upvote 0
Ok, how about
VBA Code:
Sub RemoveLeadingSpace()

Dim Rng As Range, WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
  For Each Rng In WorkRng
    Rng.Value = Trim(Rng.Value)
  Next
  Rng.Interior.Color = vbYellow
MsgBox ("Formula was used in this Guide")
End Sub
 
Upvote 0
Solution
Thanks a lot for the help!!!
Now is working, only thing I had to change that's it:
VBA Code:
Sub RemoveLeadingSpace()

Dim Rng As Range, WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
  For Each Rng In WorkRng
    'Rng.Value = Trim(Rng.Value)
    Rng.Value = VBA.Trim(Rng.Value)
      Rng.Interior.Color = vbYellow
  Next
  'Rng.Interior.Color = vbYellow
MsgBox ("Formula was used in this Guide")
End Sub
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,551
Messages
6,125,478
Members
449,233
Latest member
Deardevil

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