簡體   English   中英

使用 VBA 將值從一個工作簿復制到另一個工作簿

[英]Copying values from one workbook into another using VBA

所以我試圖將一個工作簿中的值復制到另一個工作簿中,並不斷收到語法編譯錯誤。 如果有人知道為什么會很有幫助

Sub findsomething()
Dim rng As Range
Dim account As String
Dim rownumber As Long
Dim dehyp As Long

dehyp = Replace(Range("A5").Value, "-", "")
account = Sheet.Cells(dehyp)


Set rng = sheet1.List-of-substances-in-the-third-phase-of-CMP-(2016- 
2021).xlsx.Columns("A:A").Find(What:=account,
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
rownumber = rng.Row
Sheet1.Cells(2, 2).Value = Sheet1.List-of-substances-in-the-third- 
phase-of-CMP-(2016-2021).xlsx.Cells(rownumber, 
3).Value

End Sub

單元格 A5 包含

帶有連字符的數字,例如 279-01-2。 但要在其他文檔中搜索需要采用 279012 的形式

您的某些代碼不清楚,但它更像是:

Sub findsomething()

    Dim rng As Range
    Dim account As String
    Dim rownumber As Long
    Dim dehyp As Long
    Dim wb As Workbook

    dehyp = Replace(Range("A5").Value, "-", "") '<< be more specific here about workbook/sheet
    account = Sheet.Cells(dehyp)  '<< and here

    Set wb = Workbooks.Open( _
      "L:\PRS\CEPA\Chemicals Management Plan\!Overviews and Summaries\" & _
      "List-of-substances-in-the-third-phase-of-CMP-(2016-2021).xlsx")

    Set rng = wb.Sheets("sheet1").Columns(1).Find(What:=account, _
        LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If not rng is nothing then
        thisworkbook.sheets("Sheet1").Cells(2, 2).Value = _
               wb.Sheets("sheet1").Cells(rng.Row, 3).Value
    End If

End Sub

不過,這作為Vlookup會更整潔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM