+ Reply to Thread
Results 1 to 7 of 7

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

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

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

    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.

    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:

    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 by mduff3; 01-28-2014 at 08:58 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

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

    Hi -

    Welcome to the board.

    change the zip macro to something like this;
    Sub Zip()
    to
    Sub Zip(empName as string)
    then change the
    FileNameZip = SavePath & "Anthony Tran " & strDate & ".zip"
    to
    FileNameZip = SavePath & empName & " " & strDate & ".zip"
    finally change the onaction
    .OnAction = "Zip" ' Assigns macro "Zip" to each button
    to
    .OnAction = "'Zip""" & Range("b" & I) & """'" ' Assigns macro "Zip" to each button
    event

  3. #3
    Registered User
    Join Date
    01-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

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

    Thanks a lot for your reply event. I actually managed to get the code to do exactly what I need it to do. The problem I have now is that the macro keeps looping and creating the same zip file over and over and over until I force quit Excel. Here's the code:

    Sub WhichButton()
    
    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
    
             ' Assign the calling object to a variable.
    ButtonName = Application.Caller
    RowCount = Cells(Cells.Rows.Count, "c").End(xlUp).Row       ' Value being searched is in column c
    
    For I = 2 To RowCount + 1
        Select Case ButtonName          ' Display the name of the button that was clicked.
            Case Range("B" & I)
    
                SavePath = "C:\Users\MDuff3\Desktop\" 'save zip location
                strDate = Format(Now, " dd-mmm-yy h-mm-ss")
                FileNameZip = SavePath & ButtonName & strDate & ".zip"
    
                FName = Array("Y:\Administration\Personnel\Certifications And Identification\CSTP\" & ButtonName & "_CSTP.pdf\")
    
                    If IsArray(FName) = False Then
                            'do nothing
                    Else
                            'Create empty Zip File
                        NewZip (FileNameZip)
                        Set oApp = CreateObject("Shell.Application")
                        I = 0
                        For iCtr = LBound(FName) To UBound(FName)
                        vArr = Split97(FName(iCtr), "\") 'splits raw directory into array at each "/"
                        sFName = vArr(UBound(vArr)) 'picks final part of array which is the filename
                    If bIsBookOpen(sFName) Then
                    MsgBox "You can't zip a file that is open!" & vbLf & _
                           "Please close it and try again: " & FName(iCtr)
                    Else
                    'Copy the file to the compressed folder
                    I = I + 1
                    oApp.Namespace(FileNameZip).CopyHere FName(iCtr)
                    'Keep script waiting until Compressing is done
                    On Error Resume Next
                    Do Until oApp.Namespace(FileNameZip).items.Count = I
                        Application.Wait (Now + TimeValue("0:00:01"))
                    Loop
                    On Error GoTo 0
                End If
            Next iCtr
            MsgBox "You find the zipfile here: " & FileNameZip
        End If
    
             End Select
    Next
          End Sub
    
    Sub NewZip(sPath)
    'Create empty Zip File
        If Len(Dir(sPath)) > 0 Then Kill sPath 'If the zip file name already exists
        
        Open sPath For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1
    End Sub
    
    Function bIsBookOpen(ByRef szBookName As String) As Boolean
    ' Rob Bovey
        On Error Resume Next
        bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
    End Function
    
    Function Split97(sStr As Variant, sdelim As String) As Variant
    'Tom Ogilvy
        Split97 = Evaluate("{""" & _
                           Application.Substitute(sStr, sdelim, """,""") & """}")
    End Function
    Any idea how I can change this code to make it create the zipped file only once?

  4. #4
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

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

    Hi -
    Thanks a lot for your reply event. I actually managed to get the code to do exactly what I need it to do.
    Good for you.

    event

  5. #5
    Registered User
    Join Date
    01-28-2014
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

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

    Any idea how I can get my macro to stop repetitively creating the zipped files?

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

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


  7. #7
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

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

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Assigning a macro to a button
    By Nigel Drinkwater in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 08:10 AM
  2. [SOLVED] assigning a macro to a button
    By Darby in forum Excel General
    Replies: 3
    Last Post: 02-10-2006, 01:10 PM
  3. [SOLVED] Assigning macro to button
    By d in forum Excel General
    Replies: 0
    Last Post: 08-22-2005, 09:05 AM
  4. [SOLVED] Assigning a macro to a button
    By Junkyard Engineer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2005, 11:05 AM
  5. Assigning a Macro to a Button
    By cybercab in forum Excel General
    Replies: 3
    Last Post: 04-11-2005, 03:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1