Add File Name to Cell B1 and Date in A1 when importing File

jrjobe

New Member
Joined
Feb 3, 2012
Messages
38
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hi All!

Is there a way to modify the code below so that when I import a new file, it starts the import on row 2 and adds the file name of the import to cell B1 and the date of import to cell A1? There isn't a header row in the file, it just imports the raw data from a csv file.

I've been messing around with it to at least get it to start the import on row 2, but keep getting a Type Mismatch error. As indicated from the comment in the code, I did find this code from MrExcel and modified it to work a while back....so far it's been working excellent, but now I need to include the file name so I know I'm working with the latest file.

VBA Code:
Sub Import()
'info came from MrExcel.com and modified to work for this workbook

Dim strFile As String, wb As Workbook
'Open the File Dialog
    With Application.FileDialog(3)
         .AllowMultiSelect = False
         'Show the dialog box
         If .Show Then
            'Store in fullpath variable
            fullpath = .SelectedItems.Item(1)
            
            'open the file
            Set wb = Workbooks.Open(fullpath)
         End If
         If wb Is Nothing Then Exit Sub
        'copy range from selected item into current worksheet
        wb.Sheets(1).Range("A:L").COPY
        ThisWorkbook.Worksheets("Import").Activate
        ActiveSheet.Range("A:L").PasteSpecial
        Columns("A:L").EntireColumn.AutoFit
        Application.CutCopyMode = False
        wb.Close False
    End With
End Sub

Thanks, everyone!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about
VBA Code:
Sub Import()
'info came from MrExcel.com and modified to work for this workbook

Dim strFile As String, wb As Workbook
'Open the File Dialog
    With Application.FileDialog(3)
         .AllowMultiSelect = False
         'Show the dialog box
         If .Show Then
            'Store in fullpath variable
            fullpath = .SelectedItems.Item(1)
            
            'open the file
            Set wb = Workbooks.Open(fullpath)
         End If
         If wb Is Nothing Then Exit Sub
        'copy range from selected item into current worksheet
        With wb.Sheets(1)
            .Range("A1:L" & .Range("A" & Rows.Count).End(xlUp).Row).Copy ThisWorkbook.Worksheets("Import").Range("A2")
        End With
        With ThisWorkbook.Worksheets("Import")
            .Range("A1:B1").Value = Array(Date, wb.Name)
            .Columns("A:L").EntireColumn.AutoFit
        End With
        Application.CutCopyMode = False
        wb.Close False
    End With
End Sub
This assumes that col A will always have a value.
 
Upvote 0
Solution
@Fluff That did the trick! I really appreciate the quick reply. Have a great day!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
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