VBA copy data to activeworksheet

Ztcollins

Board Regular
Joined
Jun 4, 2014
Messages
69
Office Version
  1. 365
Platform
  1. Windows
Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Text Files (*.csv*),*csv*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A1:V45").Copy
Range("T1").PasteSpecial xlPasteValues
OpenBook.Close False


End If

Application.ScreenUpdating = True

End Sub

I am importing a CSV file to cell T1 on a worksheet. But when i copy that worksheet and move it to the end and try to use the button it copies it back to the other worksheet. The data is race results from an iracing csv file. My apologies in advance i am new to VBA and still trying to learn things. I would like to make a default sheet and then just copy it to the end and have it work the active worksheet.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Update the Script works but the button on the page does not keep the proper macro. how to i make it autoassign a macro
 
Upvote 0
But when i copy that worksheet and move it to the end and try to use the button...

A couple of comments.

1. Don't assume we know everything you know. When you say "the button" you are assuming other readers know what you are talking about. No one reading this except you understands anything about the button, like what kind of button it is, where it is located, or what it does.
2. When posting code, please use code tags. It make the code easier to read.

 
Upvote 0
Try this variation and see if you get better results.

VBA Code:
Sub Get_Data_From_File()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim WS As Worksheet

    Set WS = ActiveSheet
    
    Application.ScreenUpdating = False

    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Text Files (*.csv*),*csv*")

    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1:V45").Copy
        WS.Range("T1").PasteSpecial xlPasteValues
        OpenBook.Close False
    End If

    Application.ScreenUpdating = True
End Sub

Assuming you are using a form button located on a worksheet, the subroutine Get_Data_From_File() should be located in a standard code module, and not a worksheet code module.
 
Upvote 0
Solution
@ riv01 Thank you! Its been years since ive posted. I will clean things up going forward Again thanks,
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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