Excel VBA Code - Open File Explorer and Copy then Paste

Anonymous
2018-08-07T13:03:47+00:00

I am trying to write a code that allows me to open file explorer and import a .csv file, but I only need the data from cells A57 and B57 down until there is no more data. I would need that data imported to the active macro into cells A31 and B31. I have attached my current code that allows me to open file explorer, but I cannot copy and paste the data.

Thanks for the help!

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2018-08-07T13:20:53+00:00

    Try this:

    Sub ImportData()

        Dim strFile As String

        Dim wbk As Workbook

        Dim lngLastRow As Long

        strFile = Application.GetOpenFilename(FileFilter:="CSV files (*.csv),*.csv")

        If strFile = "False" Then

            Beep

            Exit Sub

        End If

        Application.ScreenUpdating = False

        Set wbk = Workbooks.Open(Filename:=strFile)

        With wbk.Worksheets(1)

            lngLastRow = .Range("A:B").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

            .Range("A57:B" & lngLastRow).Copy Destination:=ThisWorkbook.Worksheets(1).Range("A31")

        End With

        wbk.Close SaveChanges:=False

        Application.ScreenUpdating = True

    End Sub

    Also see https://social.msdn.microsoft.com/Forums/en-US/0f93a0a7-8db8-4b46-af30-50ab7001ad19/open-file-explorer-code

    3 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-08-07T13:28:09+00:00

    this worked perfectly, thank you!

    0 comments No comments