2016-09-06 2 views
0

나는 이것에 대한 답변을 철저히 조사했으면 좋겠다. 시작하는 행의 사용자 입력을 기반으로 한 워크 시트의 데이터를 다른 워크 시트로 복사하려고합니다. 예를 들어, "9"를 입력하면 해당 열을 나타내는 문자를 추가하고 다른 셀까지 데이터를 복사하기 시작합니다. Excel VBA 한 시트의 데이터를 다른 시트의 사용자 입력에서 다른 시트로 복사

Sub Transfer() 

    Dim shSource As Worksheet 
    Dim cellValue As Range 

    Dim formatedCellBegin As Range 
    Dim formatedCellEnd As Range 


    Set shSource = ThisWorkbook.Sheets("Sheet1") 'Get info from user input  sheet 
    Set cellValue = shSource.Cells(4, "H") 'User input taken from "H4" on sheet1 received in regards to what row to start transfer 


    Set formatedCellBegin = "J" & cellValue 'Add J to the that row to get the cell to start at 
    Set formatedCellEnd = "K" & cellValue 'End at cell K - (whatever they pick) 


'Sheet 12 is the sheet with all the invoice info 
'Sheet 11 is the sheet to put all the info 

Sheets("Sheet12").Range("formatedCellBegin:formatedCellEnd").Copy Destination:=Sheets("Sheet11").Range("B20") 
End Sub 

내가 그것을 가지고

+0

난 당신이 마지막 줄은'("Sheet12") 시트가 될 것을 원한다고 생각 범위 (formatedCellBegin, FormatedCellEnd) .Copy ...', formatedCellBegin'가입일 :

아래의 수정 된 코드를 사용해보십시오.. 주소'는'J12'와 같이 해결되어야한다. 'F8'을 사용하여 매크로를 단계별로 실행하면,이 두 범위를 설정할 때 직접 실행 창 (Ctrl + G를 눌러)에'? formatedCellBegin.address'를 입력하여 제대로 설정되었는지 확인하십시오. – BruceWayne

+0

@ 케빈 스미스는 아래의 내 대답에 코드와 설명을 참조하십시오. –

답변

0

당신의 도움을 주셔서 감사합니다 : 여기에 코드입니다. 'formatedCellBegin'과 Strings로 선언 된 다른 것들은 앞에 'Set'을 가질 필요가 없었습니다.

Sub Transfer() 

    Dim shSource As Worksheet 
    Dim cellValue As Range 

    Dim formatedCellBegin As String 
    Dim formatedCellEnd As String 
    Dim fullRange As String 


    Set shSource = ThisWorkbook.Sheets("Sheet10") 'Get info from user input sheet 
    Set cellValue = shSource.Cells(4, "H") 'User input received in regards to what row to start transfer 


    formatedCellBegin = ("J" & CStr(cellValue)) 'Add J to the that row to get the cell to start at 
    formatedCellEnd = ("K" & CStr(cellValue)) 'End at cell K - (whatever they pick) 

    fullRange = formatedCellBegin & ":" & formatedCellEnd 

가 '시트 (12)는 모든 송장 정보 와 시트는'시트 (11)는 모든 정보 ThisWorkbook.Sheets ("Sheet12")를 넣어 시트는 범위 (fullRange) .Copy 목적지 :. =에서 ThisWorkbook .Sheets ("Sheet11"). 범위 ("B20") 귀하의 경우에는 최종 하위

0

, 당신은 formatedCellBegin를 정의 할 필요가 없습니다 귀하의 경우 formatedCellEndAs Range,하지만 당신은 어떻게 사용 As String을 정의하십시오.

또한, cellValue 당신이 그것을 정의 할 필요가있다, 그래서 당신이 행을 나타내는 셀에서 얻을 값입니다 As Long (또는 Integer).

Sub Transfer() 

Dim shSource As Worksheet 
Dim cellValue As Long  
Dim formatedCellBegin As String 
Dim formatedCellEnd As String 

Set shSource = ThisWorkbook.Sheets("Sheet1") ' Get info from user input sheet 
cellValue = shSource.Cells(4, "H") ' User input taken from "H4" on sheet1 received in regards to what row to start transfer 

formatedCellBegin = "J" & cellValue ' Add J to the that row to get the cell to start at 
formatedCellEnd = "K" & cellValue ' End at cell K - (whatever they pick) 

'Sheet 12 is the sheet with all the invoice info 
'Sheet 11 is the sheet to put all the info  
Sheets("Sheet12").Range(formatedCellBegin & "," & formatedCellEnd).Copy Sheets("Sheet11").Range("B20") 

End Sub 
관련 문제