Extract 1 string between space (or ,) and W (or kW)

dudumomo

Board Regular
Joined
Jun 3, 2014
Messages
65
Hi there,

I have some lines like these:
blablabla blabla bla 400W blabla ==> 400W
blabla 400W blablabla blabla ==> 400W
blabla,0.5kW blabla bla bla bla ==> 0.5kW

I'm trying to build a formula to extract those data accordingly, but seems they can be betwen a coma or a space and a kW or W, I have difficulties to extract the data.
And also, we have to make sure before the kW or W, is a number. As some brand name or description could be let's say "Cowboy", we should not take this one but only when there is a number.

Any idea on how to do it?
The best will also be if kW, it converts in W (/1000) to get clean data.

Thanks for your help!
 
Last edited:
sorry, best I can do is....can't remove the Vaa or come up with the N/A.....I'm sure there a far better ways !!

Code:
Function VON(s As String) As String
With CreateObject("VBScript.RegExp")
  .Pattern = "([0-9]{2,}[VA|KVA|va|kva]+)"
  If .Test(s) Then VON = .Execute(s)(0).SubMatches(0)
End With
End Function




Excel 2007
AB
11510001464#&B? luu di?n d? phng 500KVA 3P/4W 380V500KVA
21510001465#&B? luu di?n d? phng 500kva 3P/4W 380V500kva
3KDTC-3307#&Ngu?n S8VS-06024 (chuy?n d?i ngu?n di?n d?u vo AC 220V xoay chi?u d?u ra 24V m?t chi?u), hng m?i 100%220V
4B? luu di?n ABC Offline 1000VA - Model CV1000 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)1000VA
5B? luu di?n Lumix Online 500vaa - Model LU500 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)500vaa
6B? luu di?n 8V6B-500VA - Model 8V6B500 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)500VA
Sheet1
Cell Formulas
RangeFormula
B1=von(A1)
B2=von(A2)
B3=von(A3)
B4=von(A4)
B5=von(A5)
B6=von(A6)
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
It is already a nice improvement. Thanks!

It's not perfect for the 220v and 500vaa tho, but I did not find a good parameter neither. It already helps to clean the data.

Not sure if there is a better way tho?

Anyone if another idea?

Thank you!!
 
Upvote 0
Is this any better?

Code:
Function ExtractVA(s As String) As String
  With CreateObject("VBScript.RegExp")
    .IgnoreCase = True
    .Pattern = "\d*\.?\d* *k?va"
    ExtractVA = .Execute(s)(0)
  End With
End Function

Excel Workbook
AB
1
21510001464#&B? luu di?n d? phng 500KVA 3P/4W 380V500KVA
31510001465#&B? luu di?n d? phng 500kva 3P/4W 380V500kva
4KDTC-3307#&Ngu?n S8VS-06024 (chuy?n d?i ngu?n di?n d?u vo AC 220V xoay chi?u d?u ra 24V m?t chi?u), hng m?i 100%#VALUE!
5B? luu di?n ABC Offline 1000VA - Model CV1000 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)1000VA
6B? luu di?n Lumix Online 500vaa - Model LU500 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)500va
7B? luu di?n 8V6B-500VA - Model 8V6B500 (Km sch hu?ng d?n s? d?ng), (Hng m?i 100%)500VA
8bla bla 33.24 VA etc33.24 VA
9
VA KVA
 
Last edited:
Upvote 0
@ Peter

For me it does not extract 500KVA in:

1510001464#&Bộ lưu điện model va phòng 500KVA 3P/4W 380VA

<tbody>
</tbody>
 
Upvote 0
@ Peter

For me it does not extract 500KVA in:

1510001464#&Bộ lưu điện model va phòng 500KVA 3P/4W 380VA

<tbody>
</tbody>
You are correct.

That resulted from me trying to allow for decimal numbers before the VA or KVA, even though were no such example in the sample data for VA/KVA. I can correct that but before doing so it would be beneficial to know whether ..

a) Decimal values could occur?

b) Two or more VA/KVA values are possible in the string like in István's sample ("500KVA" and 380VA") and, if so, what result(s) should be returned?
 
Upvote 0
You are correct.

That resulted from me trying to allow for decimal numbers before the VA or KVA, even though were no such example in the sample data for VA/KVA. I can correct that but before doing so it would be beneficial to know whether ..

a) Decimal values could occur?

b) Two or more VA/KVA values are possible in the string like in István's sample ("500KVA" and 380VA") and, if so, what result(s) should be returned?

380VA remained accidentally in the sample that I used to test my version under construction how it works in end position – it does not (but yours works). So the end string to be considered is the original 380V.
 
Upvote 0
Peter
Really like the function, well done ( as always).......I don't think I will ever completely come to grips with Regex.
Is there a GOOD learning resource available ?
They all seem so complex...:pray:(y)
 
Upvote 0
Thanks Michael for your reply!
Nice approach and it works.

I have another similar, but with VA and KVA. The issue here is sometimes, I got product name with 8V6B-500VA and the formula give me 8V. How can I force "VA" (Not only 'V")
Does this UDF (user defined function) work for you...
Code:
Function ExtractVA(S As String) As String
  Dim X As Long, Txt As String, VA() As String
  VA = Split(Replace(S, "-", " "), "va", , vbTextCompare)
  For X = 0 To UBound(VA) - 1
    Txt = Mid(VA(X), InStrRev(VA(X), " ") + 1)
    If Len(Txt) > 0 And Application.IsNumber(Val(Txt)) Then
      ExtractVA = UCase(Txt) & "VA"
      Exit For
    End If
  Next
End Function
 
Upvote 0
Does this UDF (user defined function) work for you...
Code:
Function ExtractVA(S As String) As String
  Dim X As Long, Txt As String, VA() As String
  VA = Split(Replace(S, "-", " "), "va", , vbTextCompare)
  For X = 0 To UBound(VA) - 1
    Txt = Mid(VA(X), InStrRev(VA(X), " ") + 1)
    [COLOR="#0000CD"]If Len(Txt) > 0 And Application.IsNumber(Val(Txt)) Then[/COLOR]
      ExtractVA = UCase(Txt) & "VA"
      Exit For
    End If
  Next
End Function

"Give an ovation for the 500KVA item"

Just wondering about the blue line?
Rich (BB code):
If Len(Txt) And Val(Txt) Then
 
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,599
Members
449,460
Latest member
jgharbawi

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