2014-10-08 3 views
1

그래, 그와 관련된 다른 주제가 있다는 것을 알고 있습니다.하지만 다른 것을 만들면 더 좋을 것이라고 생각했습니다. 어쨌든 누군가 내가 포럼 규칙을 따르지 않는다고 생각하면해야 할 일을하십시오.VBA를 사용하여 두 엑셀 워크 북을 비교하십시오

나는이 post 다음 두 통합 문서를 비교하는 것에 대해 이야기하고있었습니다. 두 개의 Excel 파일을 같은 내용으로 비교하기 위해 비슷한 코드를 만들었습니다. 그러나 내 코드가 두 파일을 비교하는 대신 파일 A를 파일 A와 비교하거나 파일 B를 파일 B와 비교하는 것 같습니다.

코드에서 수행하는 작업은 두 개의 통합 문서를 가져와 워크 시트를 가져 오는 것입니다 대차 대조표라는 이름을 지정하고 두 통합 문서에서 대차 대조표의 값이 같은지 비교합니다. 따라서 우리는 모든 셀을 루프 할 필요가 없으며 시트가 Variant 배열로로드됩니다.

Sub CompareWorkbooks() 

Dim varSheetA As Variant 
Dim varSheetB As Variant 
Dim strRangeToCheck As String 
Dim iRow As Long 
Dim iCol As Long 

nlin = 1 
ncol = 1 

'Get the worksheets from the workbooks 
Set wbkA = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet.xls") 
Set varSheetA = wbkA.Worksheets("Balance sheet") ' or whatever sheet you need 

Set wbkB = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet_old.xls") 
Set varSheetB = wbkB.Worksheets("Balance sheet") ' or whatever sheet you need 

strRangeToCheck = "B6:D49" 
' If you know the data will only be in a smaller range, reduce the size of the ranges above. 
Debug.Print Now 
varSheetA = Worksheets("Balance Sheet").Range(strRangeToCheck) 
varSheetB = Worksheets("Balance Sheet").Range(strRangeToCheck) ' or whatever your other sheet is. 
Debug.Print Now 

For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1) 
    For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2) 
     If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then 
      ' Cells are identical. 
      ' Do nothing. 
     Else 
      ' Cells are different. Let's fill our main template with the information 
      Windows(MainTemplate.xlsm).Activate 
      Cells(nlin, ncol) = varSheetA(iRow, 2) 'Gives the name of the changed field 
      Cells(nlin, ncol + 1) = varSheetA(iRow, iCol) 'Gives me the value in workbookA 
      Cells(nlin, ncol + 2) = varSheetB(iRow, iCol) 'Gives me the value in workbookB 
      Cells(nlin, ncol + 3) = nlin 'Gives me the row location 
      Cells(nlin, ncol + 4) = ncol 'Gives me the column location 
      nlin = nlin + 1 
     End If 
    Next 
Next 

End Sub 

사람이 오류가 어디 있는지의 추측을 수행 할 수 있습니다 http://i.stack.imgur.com/tc8Nr.png

내 코드는이 하나입니다 : 당신은 아이디어를 가지고 대차 대조표의 사진을 볼 수 있습니까? 왜 다른 세포를 얻지 못하는 걸까요?

또한 새로운 문제를 발견했습니다. 특정 이름을 지정하지 않아도 모든 시트를 검토 할 수 있습니까? 내 코드에서 "대차 대조표"를 시트 이름으로 삽입해야하지만 여러 워크 시트가있는 경우 어떻게해야합니까? 루프를 만들지 만, 누구에게도이 탁월한 아이디어가있어 내 엑셀이 추락하거나 너무 느려지지 않을 것입니다.

답변

2

통합 문서 A를 사용하고 있으므로 시트 B를 설정하십시오.

Set varSheetB = wbkA.Worksheets("Balance sheet") ' or whatever sheet you need 

은 다음과 같아야합니다

varSheetA = Worksheets("Balance Sheet").Range(strRangeToCheck) 
varSheetB = Worksheets("Balance Sheet").Range(strRangeToCheck) 

가 있어야한다 : 다음 Workshe의

varSheetA = varSheetA.Range(strRangeToCheck) 
varSheetB = varSheetB.Range(strRangeToCheck) 

직접 통화

Set varSheetB = wbkB.Worksheets("Balance sheet") ' or whatever sheet you need 

죄송합니다 나는이 주변 처음 놓친 ets 함수는 항상 ActiveWorkbook을 참조합니다. 대신 항상 관련 개체 (wkbA.Worksheets ("...") 또는 wkbB.Worksheets ("..."))에서 호출하십시오.

Btw. 당신은 또한 하나에 모두 할당을 결합 할 수 있습니다 :

strRangeToCheck = "B6:D49" 
Set wbkA = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet.xls") 
Set varSheetA = wbkA.Worksheets("Balance sheet").Range(strRangeToCheck) 
Set wbkB = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet_old.xls") 
Set varSheetB = wbkB.Worksheets("Balance sheet").Range(strRangeToCheck) 
+0

세상에, 지금 난 정말 바보 같은 롤 – dekio

+0

대기 느낌, 난 그냥 코드의이 부분을 변경했습니다 그리고 난 여전히 다른 세포를 받고 아닙니다. – dekio

+0

@dekio : 좋아, 내 대답을 – marg