سؤال

I am creating a UDF that is meant to produce a string that I can put into HighCharts. So basically, I am inputting two ranges of data (numbers or text) into the UDF and outputting a string or formatted pairs that looks like this [[ DataX, DataY], [DataX1, DataY1], ...]

Things work fine for the first range, but the second range give me garbage results.

Here's my code:

Function HCP(Rng1 As Range, Rng2 As Range) As String
' this function will take columns of data and properly format them for highcharts

Dim Str1, Str2, retVal As String
Dim leng, i, j As Integer
Dim col1, col2, row1, row2 As Long
Dim temp1, temp2 As Range

Str1 = "["
Str2 = "]"
col1 = Rng1.Column
col2 = Rng2.Column
row1 = Rng1.Row
row2 = Rng2.Row

leng = Rng1.Rows.Count

For i = 1 To leng
If i < leng Then
Set temp1 = Rng1.Cells(row1 + i - row1, col1)
Set temp2 = Rng2.Cells(row2 + i - row2, col2)

retVal = retVal & "[" & temp1 & "," & temp2 & "],"
Else
retVal = retVal & "[" & temp1 & "," & temp2 & "]"
End If

Next i
retVal = Str1 & retVal & Str2

HCP = retVal
End Function

I call the function using this formula: =hcp(D2:D61,O2:O61)

The results don't make sense to me. The first range of data starts one row too low. I get several nothing values then after a while I start getting data coming from another part of the sheet in column 29. I'm guessing that it's starting with the empty cells in col 29 but I have no idea why that would be.

Here's another clue that might help get to the bottom of this. If I use the formula =hcp(A2:A61,B2:B61) the formula is grabbing data from A and C and down. Where is it adding additional columns? when I do =hcp(A2:A61,C2:C61) it is starting at A and E. When I put in =hcp(A2:A61,D2:D61) I get A and G results.

The same thing happens when I change it to this =hcp(B2:B61,C2:C61) I get C and G... you see the pattern.

I don't see how it is multiplying by 2 on my second column?

هل كانت مفيدة؟

المحلول

First tip:

In line Dim col1, col2, row1, row2 As Long - only row2 declared as Long, but other variables are Variant.

You should use Dim col1 As Long, col2 As Long, row1 As Long, row2 As Long instead. The same for other declarations.

Second tip: in line Rng1.Cells(row1 + i - row1, col1) - expression row1 + i - row1 is always equals to i

Third tip: when you are using Rng1.Cells(row1 + i - row1, col1) it works like offset i-1 to the bottom and col1-1 to the right from top left cell of Rng1.

So for D2:D61 range col1 would be equals to 4 and that's means that Rng1.Cells(1, col1) would be G2 (offset to the bottom 1-1 and to the right 4-1 from D2) and it's not what you're expecting.

Try this function instead:

Function HCP(Rng1 As Range, Rng2 As Range) As String
    ' this function will take columns of data and properly format them for highcharts
    Dim retVal As String
    Dim i As Integer

    'if ranges doesn't contains one column and same rows count - return #VALUE error
    If Rng1.Rows.Count <> Rng2.Rows.Count Or _
        Rng1.Columns.Count <> 1 Or Rng2.Columns.Count <> 1 Then
        HCP = CVErr(xlErrValue)
        Exit Function
    End If

    For i = 1 To Rng1.Rows.Count
        retVal = retVal & "[" & Rng1.Cells(i, 1) & "," & Rng2.Cells(i, 1) & "],"
    Next i
    'remove last comma
    If retVal <> "" Then retVal = Left(retVal, Len(retVal) - 1)

    HCP = "[" & retVal & "]"
End Function
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top