VBA to get a number for a HTML style value without the "%" suffix

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. Windows
Hello.


In cell A1 (Sheet 1) I'm receiving a huge HTML code. I need to extract somehow values for 'top' and 'left' without % suffix.


for: style="top: 54%; left: 13%; I need only 54 and 13


Results should be placed in column B for top values (B2:B300) and column C for left values (C2:C300).




example.


HTML in cell A1: style="top: 69%; left: 8%; background-color: rgb(0, 255, 255); style="top: 3%; left: 61%;....


Results after macro run (Sheet 1):


ABC
1style="top: 69%; left: 8%; background-color: rgb(0, 255, 255); style="top: 3%; left: 61%;....TopLeft
2698
3361
4........
5........

<tbody>
</tbody>
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this for results starting "B2".
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Sep24
[COLOR="Navy"]Dim[/COLOR] txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] a, b
txt = Replace(Range("A1"), ";", ":")
Sp = Split(txt, ":")
a = 1: b = 1
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR="Navy"]If[/COLOR] Sp(n) Like "*top" [COLOR="Navy"]Then[/COLOR]
        a = a + 1
        [COLOR="Navy"]With[/COLOR] Cells(a, "B")
            .Value = Trim(Sp(n + 1))
            .NumberFormat = "general"
            .Value = .Value * 100
        [COLOR="Navy"]End[/COLOR] With
    [COLOR="Navy"]ElseIf[/COLOR] Sp(n) Like "*left" [COLOR="Navy"]Then[/COLOR]
         b = b + 1
        [COLOR="Navy"]With[/COLOR] Cells(b, "C")
            .Value = Trim(Sp(n + 1))
            .NumberFormat = "general"
            .Value = .Value * 100
        [COLOR="Navy"]End[/COLOR] With
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
You could start with something like this:

Code:
Sub Test1()
 
Dim temp
Dim ssTop As String
Dim sLeft As String
ssTop = ""
sLeft = ""
 
temp = Split(Range("A1").Value, " ")
For i = 0 To UBound(temp)
    If InStr(temp(i), "top") Then
        
        ssTop = Left(temp(i + 1), Len(temp(i + 1)) - 2)
        
    End If
    If InStr(temp(i), "left") Then
        sLeft = Left(temp(i + 1), Len(temp(i + 1)) - 2)
    End If
    
If ssTop <> "" And sLeft <> "" Then
 
    MsgBox ssTop & " " & sLeft
    ssTop = ""
    sLeft = ""
End If

 Next i
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,731
Members
448,294
Latest member
jmjmjmjmjmjm

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