简体   繁体   中英

Finding and Copying Cells from One Workbook to Another

I have a code, which mostly works, where I create a load of new sheets in workbook 1 with names from a list in workbook 1. It then copies cells from another sheet in the workbook and put the sheet names in cell D1. This works as expected.

I then want to look in another workbook for the new sheet name and copy some cells relating to its row number. My code opens workbook 2 (set as wbtemps) but then it doesn't seem to go to the specified sheet in workbook 2 and find the cell. I have tried splitting the code into sections and if I select the sheet beforehand it finds the cell, but then it still doesn't seem to copy the cells I want...

Any help would be appreciated. I've clearly missed something somewhere...

Set wb = ActiveWorkbook

InpFolder = ThisWorkbook.Path & "\Matrix_Inputs\"
MyValue1 = InputBox("WP1 Draft Matrix Filename", "Input", "Filename")

For Each cell In wsWithSheetNames.Range(Range("B5"), Range("B5").End(xlDown))
    With wb
      .Sheets.Add after:=.Sheets(.Sheets.Count)
      On Error Resume Next
      ActiveSheet.Name = RemoveSpecialCharactersAndTruncate(cell.Value)
      Sheets("Template").Cells.Copy ActiveSheet.Range("A1")
      ActiveSheet.Range("D1").Value = cell.Value

      Application.ScreenUpdating = False
      Set wbtemp = Workbooks.Open(InpFolder & MyValue1 & ".xls*", True, True) 'The code works up to here
      Set n = wbtemp.Sheets("2_Matrix Likely Pressures").Range("B1").EntireColumn.Find(cell.Value)
      wbtemp.Sheets("2_Matrix Likely Pressures").Range(.Range("E" & n.Row), .Range("E", n.Row).End(xlToRight)).Copy
      wb.ActiveSheet.Range("B21").PasteSpecial Transpose:=True
      wbtemp.Close False
      Set wbtemp = Nothing
      Application.ScreenUpdating = True
      If Err.Number = 1004 Then
         Debug.Print cell.Value & " already used as a sheet name"
      End If
      On Error GoTo 0
    End With
Next cell

So in the end, the only way it seemed to work and find the range I wanted to copy was if I individually selected the worksheet and then the first cell of the range I wanted to copy. I know you shouldn't have to use Select, but nothing else seemed to be working (I did a lot of searching on the internet). I've included the final code I used for the part that was giving me grief.

Set wbtemp = Workbooks.Open(InpFolder & MyValue1 & ".xls*", True, True)

Set n = wbtemp.Sheets("2_Matrix Likely Pressures").Range("B1").EntireColumn.Find(cell.Value)
With wbtemp
    .Sheets("2_Matrix Likely Pressures").Select
    .Sheets("2_Matrix Likely Pressures").Cells(n.Row, "E").Select
    .Sheets("2_Matrix Likely Pressures").Range(Selection, Selection.End(xlToRight)).Copy
End With
.ActiveSheet.Range("B21").PasteSpecial Transpose:=True
Application.CutCopyMode = False

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