Importing CSV issue/ find and activate


New Member
Aug 5, 2016
I am trying to design a macro to automate importing a csv file. I currently put the filename without the extension in column A.

I currently have the macro so it lets you choose the file first, then loops through column A to find that file name without extension. I want to offset by one column (to column B) and import the information starting there.

The macro runs, however, column A is is moved to column U and the import happens at A1? Just can't seem to figure out what I am doing wrong.

Any help would be most appreciated.

Sub CSVauto()
' CSVauto Macro
' Keyboard Shortcut: Option+Cmd+x
'   Declaring and setting variables for choosing CSV to import
    Dim csvFileName As Variant
'Prompt window to choose csv file
csvFileName = Application.GetOpenFilename(FileFilter:="")
If csvFileName = False Then Exit Sub

'Setting a variable to find Experimental form name in Data Summary
Dim whatToFind As String

'Declaring that variable
csvNoExt = Replace(csvFileName, ".csv", "")
whatToFind = csvNoExt

'Looping through A column to find csvFileName without .csv extension
        Set cell = Selection.Find(What:=whatToFind, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
'Selecting cell in column B for csv input
    If Not cell Is Nothing Then
    End If
Dim newRange As Range
Set newRange = Range(ActiveCell, ActiveCell.Offset(0, 1))
'Formatting for CSV and input
With newRange.Parent.QueryTables.Add(Connection:="TEXT;" & csvFileName, Destination:=newRange)
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlMacintosh
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .Refresh BackgroundQuery:=False
        .UseListObject = False
    End With
 'Formatting DataSummary sheet to fit "requirements" :)
    Cells.Replace What:=">=", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False
    Cells.Replace What:="C121", Replacement:="C2", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False
    Cells.Replace What:="P1211", Replacement:="P21", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False

End Sub

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...