2015-01-11 2 views
0

Excel 용 VBA 코드를 만들려고합니다. 여러 제품의 데이터를 제품과 동일한 이름의 새 시트로 복사 할 수 있습니다. 각 제품에 대한 다른 데이터는 새 시트로 복사되지 않는 한 열의 날짜로 구분됩니다. 다음 코드를 만들었고 한 제품에서 작동하지만 두 번째 제품을 추가하면 코드가 잘못됩니다. 두 번째 제품의 첫 번째 열을 복사하는 대신 이전 제품의 세 번째 열을 다시 복사 한 다음 두 번째 제품의 두 번째 열로 곧바로 건너 뜁니다. 따라서 코드는 두 번째 제품의 전체 첫 번째 열을 제거합니다.열의 올바르지 않은 복사

Sub Forecast_Products() 
Dim iterations As Integer 
iterations = Cells(68, 1).Value 
Dim i As Integer, j As Integer 
For i = 1 To iterations 
    Cells(69, i).Value = 0 
    For j = 2 To 6 Step 2 
     Dim startCell As String, endCell As String 
     startCell = Col_Letter(j + 7 * (i - 1)) & "9" 
     endCell = Col_Letter(j + 7 * (i - 1)) & "60" 
     Range(startCell, endCell).Select 
     Dim salesCount As Integer 
     salesCount = Cells(69).Value 
     Cells(69).Value = salesCount + Application.WorksheetFunction.CountIf(Range(startCell, endCell), ">=0") 
     Selection.Copy 
     Dim productName As String 
     Sheets("Input").Activate 
     productName = Cells(70, i).Value 
     MsgBox (productName & " 70, " & CStr(i)) 
     Sheets(productName).Activate 
     Dim rowStart As Variant 
     rowStart = CStr(11 + (52 * (j/2 - 1))) 
     Range("B" & rowStart).Select 
     Selection.PasteSpecial xlValue 
     Range("M" & rowStart).Select 
     Selection.PasteSpecial xlValue 
     Sheets("Input").Activate 
    Next 
    Dim rowCount As Integer 
    rowCount = Cells(69, i).Value + 10 
    Sheets(Cells(70, i).Value).Activate 
    For j = 4 To 8 
     Dim formula As Variant 
     formula = Cells(17, j).Copy 
     startCell = Col_Letter(j) & "18" 
     endCell = Col_Letter(j) & CStr(rowCount) 
     Range(startCell, endCell).Select 
     Selection.PasteSpecial xlAll 
    Next 
Next 

End Sub 

Function Col_Letter(lngCol As Integer) As String 
Dim vArr 
vArr = Split(Cells(1, lngCol).Address(True, False), "$") 
Col_Letter = vArr(0) 
End Function 
+0

Excel과 관련이 있습니까? 아니면 시트, 범위, 셀 및 선택 항목이있는 다른 코드와 함께 VBA를 사용하고 있습니까? –

+0

죄송합니다. Excel에서 코드를 사용하는 것을 잊어 버렸습니다. –

+0

가장 먼저해야 할 일 : 일단 '선택', '활성화'등을 제거해야합니다. 일단 제거되면 논리 오류가 훨씬 더 분명해집니다 (코드가 훨씬 효율적이 될 것입니다). [이 질문을 참조하십시오] (http://stackoverflow.com/q/10714251/445425) –

답변

1

문제점을 파악했습니다. 두 번째 제품의 첫 번째 루프는 입력 시트로 돌아 가지 않았습니다. 이것은 고정 코드입니다.

Sub Forecast_Products() 
Dim iterations As Integer 
iterations = Cells(68, 1).Value 
Dim i As Integer, j As Integer 
For i = 1 To iterations 
    Cells(69, i).Value = 0 
    For j = 2 To 6 Step 2 
     Dim startCell As String, endCell As String 
     startCell = Col_Letter(j + 6 * (i - 1)) & "9" 
     endCell = Col_Letter(j + 6 * (i - 1)) & "60" 
     Sheets("Input").Activate 
     Range(startCell, endCell).Select 
     Dim salesCount As Integer 
     salesCount = Cells(69).Value 
     Cells(69).Value = salesCount + Application.WorksheetFunction.CountIf(Range(startCell, endCell), ">=0") 
     Selection.Copy 
     Dim productName As String 
     Sheets("Input").Activate 
     productName = Cells(70, i).Value 
     'MsgBox (productName & " 70, " & CStr(i)) 
     Sheets(productName).Activate 
     Dim rowStart As Variant 
     rowStart = CStr(11 + (52 * (j/2 - 1))) 
     Range("B" & rowStart).Select 
     Selection.PasteSpecial xlValue 
     Range("M" & rowStart).Select 
     Selection.PasteSpecial xlValue 
     Sheets("Input").Activate 
    Next 
    Dim rowCount As Integer 
    rowCount = Cells(69, i).Value + 10 
    Sheets(Cells(70, i).Value).Activate 
    For j = 4 To 8 
     Dim formula As Variant 
     formula = Cells(17, j).Copy 
     startCell = Col_Letter(j) & "18" 
     endCell = Col_Letter(j) & CStr(rowCount) 
     Range(startCell, endCell).Select 
     Selection.PasteSpecial xlAll 
    Next 
Next 

End Sub 

Function Col_Letter(lngCol As Integer) As String 
Dim vArr 
vArr = Split(Cells(1, lngCol).Address(True, False), "$") 
Col_Letter = vArr(0) 
End Function 
관련 문제