Copy and Paste Selected File in a Cell

Jeridfruge

New Member
Joined
Mar 21, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a piece of code used to on a button to grab an excel file, copy a few bits of data from it, and paste to my current open file and sheet to particular cells. How do I take the path of the selected file and paste it as a link in my currently open workbook and sheet in cell A35. I just started trying to learn some VBA code last week and I'm kind of stuck here, so any advise I would appreciate!

VBA Code:
Sub Get_Data_From_File()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename(Title:="Browse for PDL File", FileFilter:="Excel Files(*.xls*),*xls*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(11).Range("B34").Copy
        ThisWorkbook.ActiveSheet.Range("B23").PasteSpecial xlPasteValues
        OpenBook.Sheets(11).Range("B35").Copy
        ThisWorkbook.ActiveSheet.Range("B24").PasteSpecial xlPasteValues
        OpenBook.Close False
    
    End If
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Does this work for you?

VBA Code:
Sub Get_Data_From_File()

Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim strPath As String

Application.ScreenUpdating = False

FileToOpen = Application.GetOpenFilename _
  (Title:="Browse for PDL File", FileFilter:="Excel Files(*.xls*),*xls*")

If FileToOpen <> False Then

  Set OpenBook = Application.Workbooks.Open(FileToOpen)

  OpenBook.Sheets(11).Range("B34").Copy
  ThisWorkbook.ActiveSheet.Range("B23").PasteSpecial xlPasteValues

  OpenBook.Sheets(11).Range("B35").Copy
  ThisWorkbook.ActiveSheet.Range("B24").PasteSpecial xlPasteValues

  ThisWorkbook.ActiveSheet.Range("A35").Select
  ThisWorkbook.Hyperlinks.Add Anchor:=Selection, Address:=OpenBook.Path

  OpenBook.Close False

End If

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top