简体   繁体   中英

Convert text number to value

I have following values from Cell A17:A29

Col A      ColH   ColI
02494/1    191    191  Average  
02585/3    77     77   Average
02341/1    22     131 (Average for same ID in ColA)
02341/1    218

I need to average the values in Col H based on same IDs in Col A and put the average value in Col I. Since the Col A IDs are string I'm not getting error #N/A in Col I. Following are my code first try to convert IDs to values.

    Sub conver()
    Range("A17:A20").Select
    With Selection
    Selection.NumberFormat = "General"
    .Value = .Value
    End With
    End Sub 

Then

    Sub averag()
    With Worksheets("averages")
Range("I17:I" & .Cells(.Rows.Count, 1).End(xlUp).Row).Formula = "=   IF(A17<>A16,AVERAGEIF(A17:INDEX($A17:INDEX(A16:A55,MATCH(1E+99,A16:A55)+1),MATCH(TRUE,(INDEX($A17:INDEX(A16:A55,MATCH(1E+99,A16:A55)+1)<>A17,)),0)),A17,H17:INDEX($H17:INDEX(H17:H55,MATCH(1E+99,A16:A55)+1),MATCH(TRUE ,(INDEX($A17:INDEX(A16:A55,MATCH(1E+99,A16:A55)+1)<>A17,)),0))),"""")"
    End With

    End Sub

But it is not working yet. can someone help on this.or has other way to do it. I can't change the IDs format. Thank you.

edited : added solution for "unlimited" range

no need for any column A values conversion

if your relevant range lays between rows 17 and 29 of worksheet "averages", then simply go like this:

Sub averag()
     Worksheets("averages").Range("A17:A29").SpecialCells(xlCellTypeConstants, xlTextValues).Offset(, 8).FormulaR1C1 = "=IF(COUNTIF(R17C1:RC1,RC1)>1,"""",AVERAGEIF(R17C1:R29C1,RC1,R17C8:R29C8))"
end sub

moreover should the lower side of the range be dynamic(ie, not limited to row 29), then you could go like follows:

Option Explicit

Sub averag()
Dim IDRng As Range

With Worksheets("averages")
    Set IDRng = .Range("A17:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    IDRng.SpecialCells(xlCellTypeConstants, xlTextValues).Offset(, 8).FormulaR1C1 = "=IF(COUNTIF(R17C1:RC1,RC1)>1,"""",AVERAGEIF(" & IDRng.Address(, , xlR1C1) & ",RC1," & IDRng.Offset(, 7).Address(, , xlR1C1) & "))"
End With

End Sub

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM