Monday, 26 July 2021

Cross Join of Two Columns in Excel



Sub cross_join()


    Dim i, j, tot_sor_rows, tot_ref_rows As Integer

    Dim res_pos As Integer

    Dim sh1 As Worksheet

    

    Set sh1 = ThisWorkbook.Sheets("Sheet1")

    tot_sor_rows = sh1.Range("A1", sh1.Range("A1").End(xlDown)).Rows.Count

    tot_ref_rows = sh1.Range("B1", sh1.Range("B1").End(xlDown)).Rows.Count

    res_pos = 2

    

    For i = 2 To tot_sor_rows

        For j = 2 To tot_ref_rows

            sh1.Range("C" + CStr(res_pos)).Value = (CStr(sh1.Range("A" + CStr(i)).Value) + "-" + CStr(sh1.Range("B" + CStr(j)).Value))

            res_pos = res_pos + 1

        Next j

    Next i


End Sub