简体   繁体   中英

Copying values from one workbook into another using VBA

so I am trying to copy a value from one workbook into another, and keep getting syntax compilation errors. If anyone knows why it would be very helpful

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

Cell A5 contains

numbers with hypens such as 279-01-2. but to be searchable in the other document needs to be in the form of 279012

Some of your code is unclear, but it would be something more like:

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

This would be tidier as a Vlookup though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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