VBA to Export Tab to .CSV with date

DK27

New Member
Joined
Feb 13, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a tab in a workbook with the the following table containing labour hours per staff member. Columns A to F need to be exported to a .csv file to be loaded into the payroll system. Can someone kindly help me with coding to do this, the trigger being changing cell I2 to YES. Also, can the exported file be dated YYYY-MM-DD and even saved to a specific folder? or is that asking too much?

Greatly appreciate any help

Thanks

WeeklyTimesheetReport-HK (2).xlsx
ABCDEFGHI
1Line StyleEmployee IDPay Rate IDUnit (hours)Dept Quick RefDays PaidExport
2#50AdamLynch137.58No
3#50AdamLynch205
4#50AllenZ144.8
5#50AllenZ205
6#50AmandeepS147.33
7#50AmandeepS206
8#50AngeloM140.08
9#50AngeloM205
10#50BronsonC144.24
11#50BronsonC205
12#50BryanG139.75
13#50BryanG205
14#50CareyC145
15#50CareyC205
16#50CaydenA135.75
17#50CaydenA205
18#50CMasina133.5
19#50CMasina204
20#50dsteph140.32
21#50dsteph205
22#50hardeeps151.08
23#50hardeeps24.56
24#50dougo145
25#50dougo205
26#50GerardH143.75
27#50GerardH205
28#50HaydenWard141.92
29#50HaydenWard205
30#50HuiaM140.5
31#50HuiaM205
32#50JagdeepS148.26
33#50JagdeepS21.177
34#50JamahlO142.35
35#50JamahlO205
36#50JamesMoa145.5
37#50JamesMoa206
38#50JaredK139.84
39#50JaredK205
3. Upload
Cell Formulas
RangeFormula
F3,F39,F37,F35,F33,F31,F29,F27,F25,F23,F21,F19,F17,F15,F13,F11,F9,F7,F5F3=SUM(--(ISNUMBER(UNIQUE(FILTER('1. Data Dump'!$C:$C,'1. Data Dump'!$A:$A=B3)))))
D2,D4,D6,D8,D10,D12,D14,D16,D18,D20,D22,D24,D26,D28,D30,D32,D34,D36,D38D2=VLOOKUP(B2,'2. Hours Worked'!$A$4:$D$84,2,FALSE)
D3,D5,D7,D9,D11,D13,D15,D17,D19,D21,D23,D25,D27,D29,D31,D33,D35,D37,D39D3=VLOOKUP(B3,'2. Hours Worked'!$A$5:$D$85,3,FALSE)
Named Ranges
NameRefers ToCells
'1. Data Dump'!_FilterDatabase='1. Data Dump'!$A$1:$H$1394F3, F5, F7, F9, F11, F13, F15, F17, F19, F21, F23, F25, F27, F29, F31, F33, F35, F37, F39
Cells with Data Validation
CellAllowCriteria
I2List=Worktype!$A$6:$A$7
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,
untested
here is @John_w's code I adapted
if I2=YES then will export range , if not will not export range
just make sure the file should be in the same folder should export range.
VBA Code:
Public Sub Save_Range_CSV()
    Dim path As String
    Dim cellData As Variant, i As Long, j As Long
    Dim lines() As String
    
    With ActiveSheet
        cellData = .Range("A1:F1").Resize(.Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
    If ActiveSheet.Range("I2") = "YES" Then
    
    ReDim lines(1 To UBound(cellData))
    i = 1
    For j = 1 To 6
        lines(i) = lines(i) & cellData(i, j) & ";"
    Next
    lines(i) = lines(i) & ""
    For i = 2 To UBound(cellData)
        For j = 1 To 6
            lines(i) = lines(i) & cellData(i, j) & ";"
        Next
        lines(i) = lines(i)
    Next
    Else: Exit Sub
    End If
    Open ThisWorkbook.path & "\" & Format(Now, "DD-MM-YYYY HH MM") & ".csv" For Output As #1
    Print #1, Join(lines, vbCrLf)
    Close #1
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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