简体   繁体   中英

Excel VBA copy data from one worksheet to another off of user input on another sheet

I hope I did a thorough search for answers to this however, after trying piece things together I couldn't see the solution. I'm trying to copy data from one worksheet to another based on user input of the the row to start on. For example, If they enter "9" then I will add a letter signifying the column to that and start copying over data up to another cell. Here is the code:

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

Thank you for your help

I got it. Since 'formatedCellBegin' and the others declared as Strings didn't need to have 'Set' in front.

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

'Sheet 12 is the sheet with all the invoice info 'Sheet 11 is the sheet to put all the info ThisWorkbook.Sheets("Sheet12").Range(fullRange).Copy Destination:=ThisWorkbook.Sheets("Sheet11").Range("B20") End Sub

In your case, you don't need to define formatedCellBegin and formatedCellEnd As Range , but in your case and how you use them you define them As String .

Also, cellValue is a value you get from a cell, representing a row, so you need to define it As Long (or Integer ).

Try the modified code below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM