Assigning macro to each button to zip files dependent on button name

mduff3

New Member
Joined
Jan 28, 2014
Messages
2
I'm very new to VBA and programming and so I have no real idea how I can get around this problem that I have. Any help would be greatly appreciated! Sorry if it's difficult to understand exactly what I'm trying to do. If you need clarification on anything I will be online to answer any questions straight away.


Context: I have a spreadsheet that contains a list of employee names and their certifications. I want to be able to assign a button to each employee in column B with a macro that is able to zip files from a folder that contains that employees name.


The following code assigns buttons to each employee in column B. At the moment the code I have is able to assign the macro "Zip" to each button.

Code:
Dim Btn As Button
Dim rng As Range


For I = 2 To RowCount + 1


With Worksheets("Sheet1")
Set rng = .Range("B" & I)
Set Btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
With Btn
.Name = Range("B" & I)        ' Names button as employee name
.Caption = Range("B" & I)    ' Employee name appears on button as text
.OnAction = "Zip"        ' Assigns macro "Zip" to each button


End With
End With

The following code is my Zip macro:

Code:
Sub Zip()
    Dim strDate As String, SavePath As String, sFName As String
    Dim oApp As Object, iCtr As Long, I As Integer
    Dim vArr, FileNameZip
    Dim FName() As Variant
    
    SavePath = "C:\Users\MDuff3\Desktop\" 'save zip location
    strDate = Format(Now, " dd-mmm-yy h-mm-ss")
    FileNameZip = SavePath & "Anthony Tran " & strDate & ".zip"
         
    
    FName = Array("Y:\Administration\Personnel\Certifications And Identification\CSTP\Anthony Tran_CSTP.pdf\", _
    "Y:\Administration\Personnel\Certifications And Identification\Dangerous Goods Security Cards\Anthony Tran_DGSC_08-Mar-2017.pdf\", _
    "Y:\Administration\Personnel\Certifications And Identification\DG Transportation Training\DGBR\Anthony Tran_DGBR_30-Oct-2014.pdf\", _
    "Y:\Administration\Personnel\Certifications And Identification\Working at Height\Anthony Tran_WorkingAtHeights.pdf\", _
    "Y:\Administration\Personnel\Certifications And Identification\Licence to use Radioactive Material-Engineer\Anthony Tran_RA_10-Feb-2015.pdf\", _
    "Y:\Administration\Personnel\Certifications And Identification\HUET Course Cards\Anthony Tran_HUET_03-Sep-2015_BOSIET.pdf\")
....



Now, this Zip macro is able to zip the file for the employee "Anthony Tran". However I need it to be able to recognise which employee's button I've clicked and search the same files as above except with that employees name instead of "Anthony Tran".


If it makes things easier, the code for creating buttons for each employee is able to name that button as the employees name that it represents.
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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