Calculation of cells with string value

PANKAJUTEKAR

Board Regular
Joined
Jun 10, 2011
Messages
79
Good Morning All,:)

I am beginer for Vba, i need all yours kind suggestion for following -

I have query about calculation of cells.
Problem is - cells containg
3.7kw
5.5kw
and so on.

Wht i was trying since morning...addition of this cells i required along with "kw" in the output. but when i am trying calculate this string value not accepted.

Is there any solution like str etc.... to accept and addition of this cells along with string?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
A1 = 3.7kw
A2 = 5.5kw

The sum is: =LEFT(A1,LEN(A1)-2)+LEFT(A2,LEN(A2)-2)
 
Upvote 0
The approach I would use is to format the cells with a custom number format 0.0"kw" so that the number 2.3 would be displayed as 2.3kw, then conversion would be unneeded.

However if you are stuck with working with a string you could use
=left(a1,len(a1)-2)+0 to convert from the string "34.3kw" to the number 34.3
 
Upvote 0
Thank You Sir for your kind reply.

But sir, how do i do this in vba coding?..........for multiple cells
 
Last edited:
Upvote 0
UDF. Summing.
Code:
Function SumKW(rng As Range) As Double

    Dim arr As Variant, i As Integer, res As Double
    
    arr = rng
    For i = LBound(arr, 1) To UBound(arr, 1)
        res = res + Left(arr(i, 1), Len(arr(i, 1)) - 2)
    Next
    
    SumKW = res

End Function
 
Last edited:
Upvote 0
Dear Sir,

One more problem is there -
a listbox contain some values.

"Private Sub Worksheet_Activate()" i added that items on worksheet active, also i was tryed on worksheet open also.
but the value of listbox not showing when i am opened the sheet newly.
i have to go to in coding part and do "alt - f11" then only they displayed.

So sir is there other for this?
 
Upvote 0
Improved version. Can take contigous and non-contigous cells.
Code:
Function SumKW(ParamArray arr() As Variant) As Double

    Application.Volatile
    
    Dim i As Integer, res As Double
    Dim cell As Range
    
    For i = LBound(arr) To UBound(arr)
        For Each cell In arr(i)
            res = res + Left(cell, Len(cell) - 2)
        Next
    Next
    
    SumKW = res

End Function
 
Upvote 0
But Sir, how do i select the prpoer cell where i need the total sum.
suppose cells are b67:b112 this total sum display in b115.
This b115 i cant able to recognize.

can u please suggest for the above?
 
Upvote 0
Type in B115: =SumKW(B67:B112)

If you want to add non-contigous ranges, type in:
=SumKW(B67:B112,B55:B60,B40)
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

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