Extracting & Manipulating Data from CSV

MJaspering

New Member
Joined
Oct 2, 2023
Messages
8
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hey All,

I am still pretty early in learning VBA and am wanting to build a reporting tool for my teams that help them to better understand their salary-to-revenue percentages. The general outline of what I am trying to build is as follows:
  1. A table is exported to CSV from Workday.
  2. A teammate can then click on an ActiveX button that opens the File Explorer dialogue and allows them to select the CSV export.
  3. Once selected, the VBA sub would sum the numerical values from two specific columns in the CSV.
  4. These sum values would then be directed to specific cells on the worksheet.
I found the below that gets step 2 done but does not offer functionality beyond that.

Any help is greatly appreciated - thank you!

VBA Code:
Private Sub CommandButton1_Click()
Dim z As FileDialog
Dim zPath As String
Dim csv As String
Dim wsheet As String
Application.DisplayAlerts = False
    Application.StatusBar = True
    wsheet = ActiveWorkbook.Name
    Set z = Application.FileDialog(msoFileDialogFilePicker)
    z.Title = "File Selection:"
    If z.Show = -1 Then
        zPath = z.SelectedItems(1)
    Else
    Exit Sub
    End If
        If Right(zPath, 1) <> "\" Then Path = Path + "\"
        csv = Dir(Path & "*.csv")
    Do While csv <> ""
        Application.StatusBar = "Converting: " & csv
        Workbooks.Open Filename:=Path & csv
        ActiveWorkbook.SaveAs Replace(Path & csv, ".csv", ".xlsx", vbTextCompare), _
        xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(wsheet).Activate
        csv = Dir
    Loop
        Application.StatusBar = False
        Application.DisplayAlerts = True
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I found the below that gets step 2 done but does not offer functionality beyond that.
I disagree. I found that code to be broken. Here is something that will get you as far as selecting and opening the CSV file in excel. Not enough info about steps 3 & 4 to comment.

VBA Code:
Private Sub CommandButton1_Click()
    Dim z As FileDialog
    Dim zPath As String
    Dim NewWB As Workbook
    
    Set z = Application.FileDialog(msoFileDialogFilePicker)
    z.Filters.Clear
    z.Filters.Add "CSV Files", "*.csv", 1
    z.Title = "File Selection:"
    z.AllowMultiSelect = False

    If z.Show = -1 Then
        zPath = z.SelectedItems(1)
    Else
        Exit Sub
    End If

    Set NewWB = Workbooks.Open(Filename:=zPath)
    ActiveSheet.UsedRange.Columns.AutoFit
    
    '-- Begin code for steps 3 & 4
    
    '(Your code here)
    
    '-- end code for steps 3  & 4
    
End Sub
 
Upvote 0
This is fantastic, thank you!

As far as step three, it seems like I may potentially want to use a WorksheetDataConnection object to extract desired column ranges (i.e., D:D and F:F) into another form that I could sum and assign to variable?
 
Upvote 0
I would suggest power query. You can use a cell value as the file path parameter and set everything else up in the query. The all your code needs to do is update the file path in the cell and refresh the query.
 
Upvote 0
This is fantastic, thank you!

As far as step three, it seems like I may potentially want to use a WorksheetDataConnection object to extract desired column ranges (i.e., D:D and F:F) into another form that I could sum and assign to variable?

I'm not sure that the WorksheetDataConnection object confers any advantage over just using VBA to work with the range(s) directly to do what you want (sum, count, etc). But it is certainly another way to go if you spend the time to dive in and learn about it. And as @RoryA suggested, Power Query can also extract and manipulate.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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