뭔가 당신이 시작할 수 있어야합니다. 매크로에서 무슨 일이 일어나는지 설명하기 위해 꽤 철저히 설명하려고 시도했습니다.
Sub CopySomeCells()
Dim targetSheet As Worksheet 'destination for the copied cells'
Dim sourceSheet As Worksheet 'source of data worksheet'
Dim rng As Range 'range variable for all data'
Dim rngToCopy As Range 'range to copy'
Dim r As Long 'row counter'
Dim x As Long 'row finder'
Dim clientCode As String
Dim clientSheet As String
Set sourceSheet = Worksheets("Sheet1") '## The source data worksheet, modify as needed ##
With sourceSheet
'## the sheet may have data between rows 4 and 1000, modify as needed ##'
Set rng = .Range("A4", Range("A1000").End(xlUp))
'## iterate over the rows in the range we defined above ##'
For r = 1 To rng.Rows.Count
'## Set the range to copy ##'
Set rngToCopy = Range(rng.Cells(r, 1), rng.Cells(r, 12))
'## ignore rows that don't have a value in column F ##
If Not rng.Cells(r, 6).Value = vbNullString Then
'## Set the targetSheet dynamically, based on the code in column F ##'
' e.g., "ab1" --> Client 1, "ab2" --> Client 2, etc. '
'## Set the client code ##"
clientCode = rng.Cells(r, 6).Value
'## determine what sheet to use ##'
' I do this by finding the client code in the lookup table, which
' is in range "O24:O37", using the MATCH function.
' Then, offset it -1 rows (the row above) which will tell us "Client Code 1", etc.
clientSheet = .Range("O23").Offset(_
Application.Match(clientCode, .Range("O24:O37"), False), 0).Offset(-1, 0).Value
' take that value "Client Code 1" and replace "Code " with nothing, so that
' will then give us the sheet name, e.g., "Client Code 1" --> "Client 1", etc. ##'
clientSheet = Replace(clientSheet, "Code ", vbNullString)
Set targetSheet = Worksheets(clientSheet)
'## Find the next empty row in this worksheet ##'
x = Application.WorksheetFunction.CountA(targetSheet.Range("A:A")) + 1
'## Copy the selected sub-range, ##'
rngToCopy.Copy
'## Paste values only to the target sheet ##'
targetSheet.Cells(x, 1).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Next '## proceed to process the next row in this range ##'
End With
End Sub
빠른 답변과 자세한 설명을 보내 주셔서 감사합니다. 내가 분명히하지 않은 한 가지가 있습니다. 클라이언트 코드는 사람들의 머리 글자와 같은 변수를 기반으로하며 동일한 머리 글자가 둘 이상인 경우 숫자를 가질 수 있습니다. 'code' '## F #' '열의 숫자를 기준으로 targetSheet를 동적으로 설정합니다. 예 : "ab1"-> 클라이언트 1, "ab2"-> 클라이언트 2 등. Set targetSheet = 워크 시트 ("클라이언트"& _ 대체 (rng.Cells (r, 6) .Value, "ab", vbNullString))'code' – onerockscott
이전 답글을 엉망으로 만든 것으로 보입니다 ... 죄송합니다. :) 내 생각을 계속하기 위해, 나는 클라이언트 1, 클라이언트 2, ect로 미리 만들어진 다섯 개의 클라이언트 시트를 가질 계획이었습니다. 동적으로 생성 된 시트를 참조하는 스크립트 라인을 변경하려면 무엇이 필요합니까? 시트 오른쪽에있는 클라이언트 코드는 내가 첨부 한 견본 시트를 만들 때 검색 할 사용자가 입력 한 클라이언트 코드입니다. 감사합니다 Scott – onerockscott
나는 데이터가 각 클라이언트 시트의 행 하나에서 시작하여 쓰여지는지 테스트를 통해 발견했습니다. 각 클라이언트 페이지의 4 번 행으로 시작하는 행을 작성하는 방법이 있습니까? 이렇게하면 같은 머리글을 추가 할 수 있고 시트 1에는 마음에 들지 않는 부분을 추가 할 수 있습니다. 다시 도움을 청하십시오. 원래 제공 한 코드는 이전에 원래의 질문에서 설명한 방식대로 작동합니다. Scott – onerockscott