I want to push a button and have all cells in the column be reduced in length...

powerstjm

New Member
Joined
Oct 9, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I want to be able to push a button and run a for each ( or any ) loop through the whole column.....after pressing the button i want each cell to be changed to just the cost code number with first dash. for example....

01-005 contingency --> after pressing the button i would only want it to read --> 01-005

Not a complete beginner with VB but with excel i am
looking for something along the lines of --> 'for each row in column A convert the cell LEFT ( cell.text , 6 ) --> I may be way off but hopefully someone can give me some advice

.
excel.png
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I am using something similar. There is a Text-to-column feature that will separate the information to multiple columns. You can set the first column to be the number (##-###). Might need to set a filler column for the "-" then the next column would be the description.

This should at least separate the number out.

Columns("A:A").Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(6, 1)), TrailingMinusNumbers:=True
 
Upvote 0
update on the coding... This will split it to 3 separate columns. Number will be alone. B column will have the "-" separator. C column will have the description.

Sub Macro1()

Columns("A:A").Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(6, 1), Array(9, 1)), TrailingMinusNumbers:=True

End Sub
 
Upvote 0
Another option
VBA Code:
Sub Powerstjm()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .TextToColumns Range("A2"), xlFixedWidth, , , , , , , , , Array(Array(0, 1), Array(6, 9))
   End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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