Cannot select not adjacent cells as range

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm using a VBA code that works great in removed blank spaces until I try to select cells that are scattered about on my worksheet (select by pressing Ctrl and left click mouse). I am wondering is there something I can do to make the VBA work no matter what cells I select? Thank you,
VBA Code:
Sub Trim()
Dim rng As Range
Set rng = Selection
rng.Value = Application.Trim(rng)
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try using
VBA Code:
Sub Trim()
Dim cell As Range
For Each cell In Selection
  cell = Application.Trim(cell)
Next cell
End Sub
 
Upvote 0
Solution
Hello, you helped me out the other night, and the VBA you suggested works great. However, I have another macro that will not work if I have the one we worked on on listed in the VBA. I was wondering is there a way I can reword it, so it won't jam up this other macro. The macro I'm referring to swaps the first and last name in a cell. I definitely will use ours more often, but occasionally I will need to use the other. The VBA code for the other macro is: The part it gets hung up on is
Excel Formula:
mycell.value=Trim
. Thank you so much!

VBA Code:
Sub ReverseName_v2()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
  
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  For Each myCell In myRange
    If InStr(myCell.value, " ") > 0 Then
      If MsgBox("switch first 2 words?" & vbCr & myCell.value, vbYesNo, "") = vbYes Then
        NameList = Split(myCell.value & " ", , 3)
        myCell.value = [B]Trim(Jo[/B]in(Array(NameList(1), NameList(0), NameList(2))))
      End If
    End If
  Next
End Sub

Public Function Trunc(ByVal value As Double) As Integer
  ' Truncate by calling Int on the Absolute value then multiply by the sign of the value.
  ' Int cannot truncate doubles that are negative
  Trunc = (Abs(value) / value) * Int(Abs(value))
End Function
 
Upvote 0
Hey, before you commit to any time, I got the issue resolved. I just had to change the Sub title very slightly. Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,527
Messages
6,125,334
Members
449,218
Latest member
Excel Master

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