excelvbamacosexcel-2011

Display Co-Ordinates


Cell A1 has top left coord of box, A2 has bottom right coord of box, how do I get A3 to display all the coords of the box in csv format?

Ex:

Cell A1 = (24,20)
Cell A2 = (26,19)

How to get this?:

Cell A3 = (24,20),(25,20),(26,20),(24,19),(25,19),(26,19)

I am using Excel 2011


Solution

  • Is this what you are trying? (I hope I have understood your query correctly. If not then feel free to correct me)

    Paste this code in a module and then call the =GetCord(A1,A2) in cell A3 as shown in the screenshot below.

    Public Function GetCord(rng1 As Range, rng2 As Range) As String
        Dim strTmp As String, strTmp1 As String, strTmp2 As String
        Dim a As Long, b As Long, i As Long, j As Long, k As Long
    
        strTmp1 = rng1.Value: strTmp2 = rng2.Value
    
        strTmp1 = Trim(Replace(strTmp1, "(", ""))
        strTmp1 = Trim(Replace(strTmp1, ")", ""))
    
        strTmp2 = Trim(Replace(strTmp2, "(", ""))
        strTmp2 = Trim(Replace(strTmp2, ")", ""))
    
        i = Val(Split(strTmp1, ",")(0))
        j = Val(Split(strTmp1, ",")(1))
    
        k = Val(Split(strTmp2, ",")(0))
        l = Val(Split(strTmp2, ",")(1))
    
        For b = j To l Step -1
            For a = i To k
                strTmp = strTmp & "(" & a & "," & b & "),"
            Next a
        Next b
    
        GetCord = Left(strTmp, Len(strTmp) - 1)
    End Function
    

    Screenshot

    enter image description here

    Note: The above function is assuming that the y Co-Ordinates are in decreasing order. To cater for all scenarios, you will have to include that check in the function.