[英]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. 例如,如果他们输入“ 9”,那么我将在该列上添加一个字母来表示该列,然后开始将数据复制到另一个单元格。 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. 由于'formatedCellBegin'和其他声明为String的字符串不需要在前面设置'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
'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 'Sheet 12是包含所有发票信息的表'Sheet 12是用于放置所有信息的表ThisWorkbook.Sheets(“ Sheet12”)。Range(fullRange).Copy目标:= ThisWorkbook.Sheets(“ Sheet11”)。Range (“ B20”)结束子
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
. 在您的情况下,您无需将formatedCellBegin和formatedCellEnd定义As Range
,但是在您的情况以及如何使用它们的情况下,可以将其定义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
). 另外, cellValue是从单元格获取的值,表示一行,因此您需要将其定义As Long
(或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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.