Excel Vba to get text from LEFT of column

matthewlrx

New Member
Joined
Jul 4, 2022
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
Hi all

I have data which gets sent to me but this data has 2 parts of data in each cell. I only need the left (or first) part of each cell.

In the picture below you will see the first column is time, I only need 11:01min and the second and third column il only need the first percentage number. Is there a VBA i can run that will go change each cell in each column? The tricky part is the percentage can change so it could be 0% or 88% so i cant choose the normal LEFT,3 option.

1659630139042.png
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
For just A1:C1, you could use:
VBA Code:
Sub matthewlrx()
Range("A1").Value = Left(Range("A1").Value, InStr(1, Range("A1").Value, "min") + 2)
Range("B1").Value = Left(Range("B1").Value, InStr(1, Range("B1").Value, "%"))
Range("C1").Value = Left(Range("C1").Value, InStr(1, Range("C1").Value, "%"))
End Sub
This could be adjusted to loop through multiple cells in each column, rather than just looking at those three cells, of course.
 
Upvote 0
For just A1:C1, you could use:
VBA Code:
Sub matthewlrx()
Range("A1").Value = Left(Range("A1").Value, InStr(1, Range("A1").Value, "min") + 2)
Range("B1").Value = Left(Range("B1").Value, InStr(1, Range("B1").Value, "%"))
Range("C1").Value = Left(Range("C1").Value, InStr(1, Range("C1").Value, "%"))
End Sub
This could be adjusted to loop through multiple cells in each column, rather than just looking at those three cells, of course.
that works great thank you. How would i loop it from the second line all the way down to the last row though?
 
Upvote 0
Try this. It assumes your last row is the same in all columns, so finds the last used row in column A. If the columns end at different rows, you'd just need to find that value and apply it to the ranges.
VBA Code:
Sub matthewlrx2()
Dim lastrow As Long, ce As Range
lastrow = Range("A" & Rows.Count).End(xlUp).Row

For Each ce In Range("A1:A" & lastrow)
    ce.Value = Left(ce.Value, InStr(1, ce.Value, "min") + 2)
Next ce

For Each ce In Range("B1:C" & lastrow)
    ce.Value = Left(ce.Value, InStr(1, ce.Value, "%"))
Next ce

End Sub
 
Upvote 0
Solution
Try this. It assumes your last row is the same in all columns, so finds the last used row in column A. If the columns end at different rows, you'd just need to find that value and apply it to the ranges.
VBA Code:
Sub matthewlrx2()
Dim lastrow As Long, ce As Range
lastrow = Range("A" & Rows.Count).End(xlUp).Row

For Each ce In Range("A1:A" & lastrow)
    ce.Value = Left(ce.Value, InStr(1, ce.Value, "min") + 2)
Next ce

For Each ce In Range("B1:C" & lastrow)
    ce.Value = Left(ce.Value, InStr(1, ce.Value, "%"))
Next ce

End Sub
Fantastic, thank you so so much this works GREAT.
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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