다른 데이터 소스를 식별하는 여러 탭이있는 워크 시트가 있습니다. 모든 워크 시트를 하나로 결합한 다음 워크 시트 이름이있는 열을 새 결합 시트의 일부로 추가해야합니다.워크 시트를 결합하고 Excel에서 열을 추가하십시오.
다음 코드를 찾았습니다. 내 워크 시트에 잘라내어 붙여 넣기해도 매력적이지만,이 통합 문서가 여러 개 있습니다. 매월이 프로세스를 다시 만들 수 있어야합니다.
내 연구에 따르면이 작업을 수행하려면 com 추가 또는 불러 오기 매크로를 만들어야하지만 시도 할 때마다 프로세스가 실패합니다. somone이 Excel (2013)에서이 작업을 수행하는 단계를 알려주고 코드가 제대로 작동하는지 알려 주시면 감사하겠습니다.
미리 감사드립니다.
Sub Combine()
Dim J As Integer, wsNew As Worksheet
Dim rngCopy As Range, rngPaste As Range
Dim Location As String
On Error Resume Next
Set wsNew = Sheets("Combined")
On Error GoTo 0
'if sheet does not already exist, create it
If wsNew Is Nothing Then
Set wsNew = Worksheets.Add(before:=Sheets(1)) ' add a sheet in first place
wsNew.Name = "Combined"
End If
'copy headings and paste to new sheet starting in B1
With Sheets(2)
Range(.Range("A1"), .Cells(1, Columns.Count).End(xlToLeft)).Copy wsNew.Range("B1")
End With
' work through sheets
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
'save sheet name/location to string
Location = Sheets(J).Name
'set range to be copied
With Sheets(J).Range("A1").CurrentRegion
Set rngCopy = .Offset(1, 0).Resize(.Rows.Count - 1)
End With
'set range to paste to, beginning with column B
Set rngPaste = wsNew.Cells(Rows.Count, 2).End(xlUp).Offset(2, 0)
'copy range and paste to column *B* of combined sheet
rngCopy.Copy rngPaste
'enter the location name in column A for all copied entries
Range(rngPaste, rngPaste.End(xlDown)).Offset(0, -1) = Location
Next J
End Sub
감사합니다 팀을 당신은에 대해 당신이 참조하고 통합 문서 항상 명시 됨으로써이 문제를 해결. 이 기사를 처음 접했기 때문에 "통합 문서 개체 참조로 모든 시트 참조를 정규화"하는 것이 좋습니다. – user3462078
위의 수정 사항을 참조하십시오. –
감사합니다. Tim! 그것은 매력처럼 작동했습니다! 나는 이것들을 더 연구 할 수 있도록 이것을 연구 할 것이다! – user3462078