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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...