VBA - Convert text to number

ebilbrough

Board Regular
Joined
Nov 17, 2010
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have a range of text (B3:AD1500) that is in text format. All the text format cells represent hours worked and have an "h" at the end (ex. 23.25h, 0.00h).

I'm looking for a way in VBA to have the "h"dropped and convert the text to a number.

Thanks in advance.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

ebilbrough

Board Regular
Joined
Nov 17, 2010
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I need the result to also convert the cell to a number format. Is this possible?
 
Upvote 0

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
Before:

B​
C​
D​
E​
F​
G​
H​
3​
7.85h1.98h16.23h18.37h14.57h20.77h12.24h
4​
1.95h12.24h22.81h18.14h11.15h6.18h14.81h
5​
12.54h11.40h16.70h9.74h4.12h16.40h17.65h
6​
3.94h2.89h11.62h12.74h14.52h1.95h23.97h
7​
16.37h5.75h7.23h18.84h4.18h20.69h9.22h
8​
12.74h4.51h14.52h20.63h5.30h5.25h20.65h

Code:

Code:
Sub eb()
  With Range("B3:AD1500")
    .Replace "h", ""
    With Cells(Rows.Count, Columns.Count)
      .Value = 24
      .Copy
    End With
    .PasteSpecial Paste:=xlPasteValues, Operation:=xlDivide
    .NumberFormat = "[h]:mm"
    Cells(Rows.Count, Columns.Count).Clear
  End With
  ActiveSheet.UsedRange
End Sub

After:

B​
C​
D​
E​
F​
G​
H​
3​
7:51​
1:58​
16:13​
18:22​
14:34​
20:46​
12:14​
4​
1:57​
12:14​
22:48​
18:08​
11:09​
6:10​
14:48​
5​
12:32​
11:24​
16:42​
9:44​
4:07​
16:24​
17:39​
6​
3:56​
2:53​
11:37​
12:44​
14:31​
1:57​
23:58​
7​
16:22​
5:45​
7:13​
18:50​
4:10​
20:41​
9:13​
8​
12:44​
4:30​
14:31​
20:37​
5:18​
5:15​
20:39​
 
Last edited:
Upvote 0

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Something like this (obtained mainly from the macro recorder while doing it by hand)

Code:
Sub Macro1()
With Range("A1:A10")
    .Replace What:="h", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    .NumberFormat = "0.00"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,685
Messages
5,988,002
Members
440,125
Latest member
vincentchu2369

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
Top