Input in column H
from Row 2
column H
90 million +
550 million +
150 million +
2.1 billion +
110 million +
.
.
.
Expected Output in Column J
from Row 2
90
550
150
2.1
110
.
.
.
VBA Code:
Option Explicit
Sub ConvertRange()
Dim str As String
Dim Cet
Dim i, j, l As Long
Dim FinalRow As Long
Dim wk As Worksheet
Application.ScreenUpdating = False
Set wk = Sheets("Sheet1")
FinalRow = wk.Range("H900000").End(xlUp).Row
For i = 2 To FinalRow
str = Cells(i, "H").Value
str = Replace(str, " ", "+")
str = Replace(str, "", "+")
str = Replace(str, " ", "+")
Cet = Split(str, "+")
Range("J" & i) = Cet(LBound(Cet))
Next i
Application.ScreenUpdating = True
End Sub
But I am getting wrong Output in Column J
As follows
90 million
550 million
150 million
2.1 billion
110 million
.
.
.
I believe either of these commands would have replaced the white spaces with the +
sign
str = Replace(str, "", "+")
str = Replace(str, " ", "+")
And following line would have an Array wherein LBound value id Cet should be only numbers
Cet = Split(str, "+")
Where am I wrong ?
Basically what I want is Cet(0) should be the long or integer number and Cet(1) should be the text billion or million depending on the text in col H for every loop so that I can process this further
So if Column H Row 2 value is 90 million Cet(0)=90 and Cet(1)=million
It seems one issue may be that there is not a space
between the integer and the text string. Rather there is a NBSP
. That being the case, the following should work:
Option Explicit
Sub ConvertRange()
Dim Cet
Dim FinalRow As Long
Dim wk As Worksheet
Dim i As Long
Set wk = Sheets("Sheet1")
With wk
FinalRow = .Cells(.Rows.Count, "H").End(xlUp).Row
For i = 2 To FinalRow
Cet = Split(Replace(.Cells(i, "H"), Chr(160), Space(1)))
.Range("J" & i) = Cet(0)
.Range("K" & i) = Cet(1)
Next i
End With
End Sub
Split()
is working fine, you're just not supplying the correct arguments for your Replace()
method.
I believe either of these commands would have replaced the white spaces with the + sign
str = Replace(str, "", "+") str = Replace(str, "", "+")
""
is just an empty string - you want to replace new line characters for which there are vbConstants:
vbCr '// Carriage Return
vbLf '// Line Feed
vbCrLf '// Carriage Return Line Feed
vbNewLine '// Carriage Return Line Feed
Or you can use the Chr()
code:
Chr(13) '// Carriage Return
Chr(10) '// Line Feed
So you need something like:
str = Replace(str, vbCrLf, "+")
Would a formula like this in column J not suffice?
=LEFT(H2,FIND(" ",H2)-1)
It extracts all characters prior to the first space character.
Same approach in VBA like this:
Sub test1()
Dim mystring As String
Dim newstring As String
mystring = "1.2 million +" 'hard-coded string
newstring = Left(mystring, InStr(1, mystring, " ") - 1) 'alteration of my formula suggestion
MsgBox (newstring) 'see the output
End Sub
Rather than looping through the cells, a bulk Range.TextToColumns method would be appropriate as long as you discard the fields of information you do not want.
The sample you supplied has a non-breaking space (eg Char(160)
) between the number and the 'million'. This is a common anomaly when copying information from a web page. This should take care of that rogue space character as well as regular (eg Chr(32)
) spaces.
With Worksheets("Sheet9") '<~~ set this worksheet refernce properly
With Columns(1)
.TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
ConsecutiveDelimiter:=True, _
Tab:=False, Semicolon:=False, Comma:=False, _
Space:=True, Other:=True, OtherChar:=Chr(160), _
FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9))
End With
End With
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.