VBA - Excel Drop csv file

Hi

I'm trying to upload a CSV file inside an EXCEL just by doing sort of a drag and drop action.

I would drag the csv file in a TextBox inside an Excel file and it would automatically convert text to column in the excel flle as TEXT format.

The thing is that in the CSV file, i can get values like: 0001 for example. when it will be transferred in the excel file, it still need to show 0001.

This is the excel file with drop box:
drop-box-option.jpg
Once dragged in, the result would be:
drag-and-drop-result.jpg

Is that possible to do? the csv file can get multiple rows also.
How can i do that?

I'm joining the 2 files in attachment.

Thanks for your help.
cvs-data-set.csv
Drop-box-form.xlsm
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wilder1626Author Commented:
So far, what i did was to use the FileDialog. But when it paste in D4, it converts the data in general and not text, and because of that, i'M loosing the leading ZEROs .

So far, still not working the way i would like to have it.

  Dim fStr As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        If .SelectedItems.Count = 0 Then
            MsgBox "Cancel Selected"
            Exit Sub
        End If
        'fStr is the file path and name of the file you selected.
        fStr = .SelectedItems(1)
    End With

    With ThisWorkbook.Sheets("Sheet1").QueryTables.Add(Connection:= _
    "TEXT;" & fStr, Destination:=Range("$D$4"))
        .Name = "CAPTURE"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    End With

Open in new window

Rgonzo1971Commented:
Hi,

to keep the text you have to use 2 instead of 1 in the .TextFileColumnDataTypes Array
2 equals xlTextFormat

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
It also helps to format the columns as text before you put any data into them.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Wilder1626Author Commented:
Perfect now it works. But my main goal is really to be able to drag the csv file into the excel file so that it convert automatically.

Is that possible?
aikimarkCommented:
I don't see any drag/drop related events for worksheets or workbooks.  If this can be done, I would expect that you will need to drop some control on your Excel worksheet that does have a drop event.
Rgonzo1971Commented:
the problem is if you drop a csv in Excel it will automatically open but not with your requirements
Wilder1626Author Commented:
Thanks for your help. i will continue to upload the csv manually.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.