簡體   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