繁体   English   中英

Excel VBA将数据从一个工作表复制到另一个工作表,而不会在另一工作表上进行用户输入

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

我希望我对此进行了彻底的搜索,但是在将所有内容拼凑在一起之后,我看不到解决方案。 我试图根据要开始的行的用户输入将数据从一个工作表复制到另一个工作表。 例如,如果他们输入“ 9”,那么我将在该列上添加一个字母来表示该列,然后开始将数据复制到另一个单元格。 这是代码:

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

谢谢您的帮助

我知道了。 由于'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是包含所有发票信息的表'Sheet 12是用于放置所有信息的表ThisWorkbook.Sheets(“ Sheet12”)。Range(fullRange).Copy目标:= ThisWorkbook.Sheets(“ Sheet11”)。Range (“ B20”)结束子

在您的情况下,您无需将formatedCellBeginformatedCellEnd定义As 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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