미해결 엑셀2010 이전기타
vba 행 추가 매크로에서 마지막 행 자동으로 찾는 방법 문의 합니다.
안녕하세요
vba 관련 문의 사항이 있습니다.
조건에 따라 행을 추가 하고 추가 된 행에 날짜 및 합계를 기록 하는 매크로 입니다.
행 추가 전 lastRow를 통해 값을 구했으나, for 문으로 행을 추가 하는 경우, 기존 lastRow 값으로 추가 된 행 까지 진행 되지 않는 문제가 있어
lastRow 값에 임의 값 100 을 더 진행하게 했습니다.
질문은 추가 되는 행까지 확인해서 마지막 행까지 계속해서 진행 할 수 있는 방법이 있을까 해서 문의 합니다.
감사합니다
' 마지막 행을 찾습니다.
lastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
newlastRow = lastRow + 100 ← 추가기입
If currentDate = 0 Then
MsgBox "D2 셀은 날짜 형식의 데이터가 아닌 것 같습니다.", vbExclamation
Exit Sub
End If
' 아래로 이동하면서 날짜, 거래처, 중량을 비교하고 행을 추가합니다.
For currentRow = 2 To newlastRow
' G, I, K, L 열의 데이터를 가져옵니다.
On Error Resume Next
newDate = DateValue(ws.Range("D" & currentRow).Value)
newSupplier = ws.Range("R" & currentRow).Value
newSum = CDbl(ws.Range("N" & currentRow).Value)
newQuantity = CDbl(ws.Range("L" & currentRow).Value)
..............
End Sub
vba 관련 문의 사항이 있습니다.
조건에 따라 행을 추가 하고 추가 된 행에 날짜 및 합계를 기록 하는 매크로 입니다.
행 추가 전 lastRow를 통해 값을 구했으나, for 문으로 행을 추가 하는 경우, 기존 lastRow 값으로 추가 된 행 까지 진행 되지 않는 문제가 있어
lastRow 값에 임의 값 100 을 더 진행하게 했습니다.
질문은 추가 되는 행까지 확인해서 마지막 행까지 계속해서 진행 할 수 있는 방법이 있을까 해서 문의 합니다.
감사합니다
' 마지막 행을 찾습니다.
lastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
newlastRow = lastRow + 100 ← 추가기입
If currentDate = 0 Then
MsgBox "D2 셀은 날짜 형식의 데이터가 아닌 것 같습니다.", vbExclamation
Exit Sub
End If
' 아래로 이동하면서 날짜, 거래처, 중량을 비교하고 행을 추가합니다.
For currentRow = 2 To newlastRow
' G, I, K, L 열의 데이터를 가져옵니다.
On Error Resume Next
newDate = DateValue(ws.Range("D" & currentRow).Value)
newSupplier = ws.Range("R" & currentRow).Value
newSum = CDbl(ws.Range("N" & currentRow).Value)
newQuantity = CDbl(ws.Range("L" & currentRow).Value)
..............
End Sub
E
숲
또
P
아
댓글 4
코드 중에서 lastRow 아래쪽으로 행을 삽입하거나 데이터를 입력하는 부분이 보이지 않네요...
어떻게 변경이 되는지 봐야,,,합니다.
newlastRow 변수로 인해 중간에 Exit Sub 종료 하게 했습니다.
Sub 공급사별행추가()
Dim ws As Worksheet
Dim lastRow As Long
Dim currentRow As Long
Dim currentDate As Date
Dim currentSupplier As String
Dim currentTotalQuantity As Double
Dim currentTotalSum As Double
Dim newDate As Date
Dim newSupplier As String
Dim newQuantity As Double
Dim newSum As Double
' 작업할 시트를 선택합니다. 시트 이름을 수정하세요.
Set ws = ThisWorkbook.Sheets("공급사별")
' 마지막 행을 찾습니다.
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
newlastRow = lastRow + 100 ← 추가 기입
' 마지막 행을 삭제 합니다. '
ws.Rows(lastRow).Delete
' 2번행을 마지막행에 복사합니다.
ws.Rows(2).Copy
ws.Rows(lastRow).Insert Shift:=xlDown
Application.CutCopyMode = False ' 복사 모드 해제
' G2, I2, K2, L2 셀에 있는 날짜, 거래처, 수량, 중량 정보를 가져옵니다.
On Error Resume Next
currentDate = DateValue(ws.Range("G2").Value)
currentSupplier = ws.Range("I2").Value
currentTotalQuantity = 0
currentTotalSum = 0
On Error GoTo 0
If currentDate = 0 Then
MsgBox "G2 셀에 날짜 형식의 데이터가 아닌 것 같습니다.", vbExclamation
Exit Sub
End If
' 아래로 이동하면서 날짜, 거래처, 중량을 비교하고 행을 추가합니다.
For currentRow = 2 To newlastRow
' G, I, K, L 열의 데이터를 가져옵니다.
On Error Resume Next
newDate = DateValue(ws.Range("G" & currentRow).Value)
newSupplier = ws.Range("I" & currentRow).Value
newQuantity = CDbl(ws.Range("K" & currentRow).Value)
newSum = CDbl(ws.Range("L" & currentRow).Value)
On Error GoTo 0
If newDate = 0 Or newSupplier = "" Then
' 마지막 행을 찾습니다.
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
' 마지막 행을 삭제 합니다. '
ws.Rows(lastRow).Delete
' 날짜 형식이나 거래처 정보가 없는 경우, 메시지를 표시하고 스킵합니다.
MsgBox "G" & currentRow & " 셀에 날짜 형식의 데이터 또는 거래처 정보가 없는 것 같습니다. 이 행은 스킵됩니다.", vbExclamation
Sheets("자동화").Select
Range("A7").Select
Exit Sub
Else
' 다음 날짜, 거래처, 중량과 현재 값 비교
If newDate <> currentDate Or newSupplier <> currentSupplier Or currentTotalSum + newSum > 30600 Then
' 다음 날짜나 거래처가 이전과 다르거나 중량의 합계가 30,600을 넘으면 새로운 행 추가
ws.Rows(currentRow).Insert Shift:=xlDown
newlastRow = newlastRow + 1 ' newlastRow 업데이트
ws.Range("G" & currentRow).Value = currentDate
ws.Range("I" & currentRow).Value = currentSupplier
ws.Range("K" & currentRow).Value = currentTotalQuantity
ws.Range("L" & currentRow).Value = currentTotalSum
' E, G, L 열만 노란색으로 채웁니다.
ws.Range("G" & currentRow).Interior.Color = RGB(255, 255, 0)
ws.Range("I" & currentRow).Interior.Color = RGB(255, 255, 0)
ws.Range("K" & currentRow & ":L" & currentRow).Interior.Color = RGB(255, 255, 0)
' 다음 날짜, 거래처, 수량을 현재 값으로 업데이트
currentDate = newDate
currentSupplier = newSupplier
currentTotalQuantity = 0
currentTotalSum = 0
' 추가된 행을 건너뛰기 위해 currentRow를 1 증가
currentRow = currentRow + 1
End If
End If
' 현재 행의 수량을 현재 수량의 합계에 추가
currentTotalQuantity = currentTotalQuantity + newQuantity
currentTotalSum = currentTotalSum + newSum
Next currentRow
Sheets("자동화").Select
Range("A7").Select
End Sub
For,,, Next 문을 아래처럼 거꾸로 작업하세요.
For CurrentRow = lastRow to 2 step -1
...
Next CurrentRow
2번 행부터 시작해야 하는데, 마땅한 방법이 생각 나지 않네요
감사합니다.