2013-08-21 2 views
0

서로 다른 두 가지 주문 데이터 원본을 통합하는 매크로 작업을하고 있습니다. 첫 번째 소스에는 이전 주문뿐만 아니라 새로운 주문이 포함되며 두 번째 소스에는 이전 주문 만 포함되며 수동으로 업데이트 된 열에는 추가 데이터가 포함됩니다.배열의 문자열을 검색 할 때 형식 불일치 오류가 발생했습니다.

내 아이디어는 두 번째 소스에서 주문 합계를 가져 와서 첫 번째 소스의 주문 합계 다음에 시트에 붙여 넣은 다음 새 파일의 모든 주문 번호를 기존 추적기. 나는 추적기에 아직없는 새 파일의 주문 번호를 찾은 다음 해당 주문 세부 정보가있는 행을 삽입해야하는 for 루프를 가지고 있습니다. 문자열이 배열에 있는지 검사하는 if 문에 형식 불일치 오류가 발생합니다. 이 코드에서 봐 주시기 바랍니다 : 나는 배열을 설정하는 몇 가지 방법을 시도

Dim r As Integer 
For r = 1 To 1000 
    Dim NewOrd As String 
    NewOrd = Range(Cells(r, 1), Cells(r, 1)).Value 
    Dim ExistArray As Variant 
    ExistArray = Range("a1", Range("a1").End(xlUp)) 
    Sheets("Sheet2").Select 

    If IsEmpty(NewOrd) Then 
     Exit For 
    End If 

    If Not UBound(Filter(ExistArray, NewOrd)) >= 0 And NewOrd <> "" Then 
     Rows(r).Select 
     Selection.Copy 
     Sheets("Sheet3").Select 
     Rows(r).Select 
     Selection.Insert Shift:=xlDown 
     Application.CutCopyMode = False 
    End If 
    r = r + 1 
Next r 

, 명시 적으로 옵션을 추가했는데, 루프 (나의 밝은 효율성 모멘트)에 대한 중첩했습니다. 눈의 또 다른 세트를 크게 감사하게 될 것입니다!

감사합니다.

답변

2

Range 개체를 배열에 할당하면 항상 2 차원 배열이 만들어져 오류가 발생합니다.

는이 작업을 수행 :

ExistArray = Application.Transpose(Range("a1", Range("a1").End(xlUp))) 

나는 그런 당신을 위해 그것을 해결한다고 생각합니다.

업데이트

당신이해야 할 수도 있습니다 : "시트 1"에서 시트 이름을

ExistArray = Application.Transpose(Array(Range("A1"))) 
+0

불행히도 그렇지 않았습니다. 코드는 같은 줄에서 (UBound가 아닌 경우 ...) 동일한 오류로 중단되었습니다. – ExcelMacroStudent

+1

예, 'Range ("a1", Range ("a1"). End (xlUp))는 단일 값을 생성합니다. "a1"의 두 번째 사용은 tigeravatar의 코드 에서처럼 "a1000"또는 ".Rows.Count"를 사용하여 더 아래쪽으로 시작해야합니다. –

+0

범위 개체가 문제입니다. –

0

변경 :

Dim ExistArray() As Variant 

귀하의 범위 객체는 단일 셀되고, 또한 문제가있다 및 "Sheet2"를 필요에 따라 입력하십시오 :

Sub tgr() 

    Dim wsNew As Worksheet 
    Dim wsTracker As Worksheet 
    Dim rIndex As Long 

    'This is the sheet that contains the new data that needs to be added 
    Set wsNew = Sheets("Sheet1") 

    'This sheet contains the old data 
    Set wsTracker = Sheets("Sheet2") 

    'Go through each row in the new data 
    For rIndex = 1 To wsNew.Cells(Rows.Count, "A").End(xlUp).Row 

     'Verify that the row isn't blank and that it doesn't already exist in wsTracker 
     If Len(wsNew.Cells(rIndex, "A").Value) > 0 And WorksheetFunction.CountIf(wsTracker.Columns("A"), wsNew.Cells(rIndex, "A").Value) = 0 Then 

      'This is a new item that needs to be added 
      'Copy the row to the next available row in wsTracker 
      wsNew.Rows(rIndex).Copy wsTracker.Cells(Rows.Count, "A").End(xlUp).Offset(1) 

     End If 
    Next rIndex 

    Set wsNew = Nothing 
    Set wsTracker = Nothing 

End Sub 
관련 문제