본문 바로가기
  • Welcome!
VBA VB.Net Code

Cell 값을 조합하기

by ToolBOX01 2024. 8. 30.
반응형

3개 이상을 조합하는것 가능하나, 시스템이 다운 될수 있습니다

code

Sub 모든조합()
    Dim lastRowA As Long, lastRowB As Long, lastRowC As Long
    Dim i As Long, j As Long, k As Long, L As Long
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1") ' 데이터가 있는 시트 이름 변경

    lastRowA = ws.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowB = ws.Cells(Rows.Count, "B").End(xlUp).Row
    lastRowC = ws.Cells(Rows.Count, "C").End(xlUp).Row
    lastRowD = ws.Cells(Rows.Count, "D").End(xlUp).Row

    ' 결과를 출력할 시트와 시작 위치 설정
    Dim resultSheet As Worksheet
    Set resultSheet = ThisWorkbook.Sheets.Add
    Dim nextRow As Long
    nextRow = 1

    For i = 1 To lastRowA
        For j = 1 To lastRowB
            For k = 1 To lastRowC
                For L = 1 To lastRowD
                    resultSheet.Cells(nextRow, 1) = ws.Cells(i, "A").Value
                    resultSheet.Cells(nextRow, 2) = ws.Cells(j, "B").Value
                    resultSheet.Cells(nextRow, 3) = ws.Cells(k, "C").Value
                    resultSheet.Cells(nextRow, 4) = ws.Cells(L, "D").Value
                    nextRow = nextRow + 1
                 Next L
            Next k
        Next j
    Next i
End Sub