2016-09-06 11 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

...'、formatedCellBegin '以来:

は、以下の変更されたコードを試してみてください。アドレスは 'J12 'のように解決する必要があります。 'F8'を使用してマクロをステップ実行し、イミディエイトウィンドウ(Ctrl + Gキーを押す)に2つの範囲を設定すると、'?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 

シート11の全ての情報 ThisWorkbook.Sheets( 『Sheet12』)範囲(フルレンジ).Copy先置くためのシートである「シート12は、すべての請求書情報 のシートは」:=はThisWorkbookをあなたのケースでは.Sheets( "Sheet11")。レンジ( "B20") End Subの

0

、あなたはAs RangeformatedCellBeginformatedCellEndを定義する必要がありますが、あなたのケースでは、どのようにあなたがそれらを使用していませんAs Stringと定義します。

また、cellValueは、あなたがそれを定義する必要があるので、あなたが行を表すセルから取得した値であるAs Long(又はInteger)。 。私はあなたが最後の行は `シート( "Sheet12")範囲(formatedCellBegin、FormatedCellEnd).Copyすることをしたいと思います

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 
関連する問題