简体   繁体   English

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

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

相关问题 Excel使用VBA将数据从一张纸复制到工作表上的另一张 - Excel copy data from one sheet to another on worksheet refresh with vba Excel VBA:从一个工作表复制到另一个 - Excel VBA: Copy from one worksheet to another Excel VBA-通过循环将数据从一个工作表复制到另一个工作表 - Excel VBA - Copy data from one worksheet to another via loop Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA Excel-VBA将数据从一个工作表复制到另一工作表并粘贴到新行 - Excel-VBA Copy data from one worksheet to another worksheet and paste in new row 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python VBA-将数据从一张纸复制到另一张纸 - VBA - Copy data from one sheet to another VBA将数据从一个工作表复制到另一个工作表 - VBA Copy data from one sheet to another Excel VBA,将数据从一个工作表复制并粘贴到另一个工作表,然后删除复制数据源 - Excel VBA, Copy and paste data from one worksheet to another then delete copy data source 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM