Solved

How do I change this VBA code to reference the data in a cell instead of the fixed path?

Posted on 2014-07-31
4
204 Views
Last Modified: 2014-08-03
I’m need to change the source of the file from this:
Call UnZip("C:\Users\NC\Desktop\Vishesh\Test", "C:\Users\NC\Desktop\Vishesh\Test\TestZipFile.Zip")

To:
Range("D12") in work sheet "Main Menu", Zip file name: "TestZipFile)


Sub TestRun()
 
    'Change this as per your requirement

    Call UnZip("C:\Users\NC\Desktop\Vishesh\Test", "C:\Users\NC\Desktop\Vishesh\Test\TestZipFile.Zip")
 
End Sub
 
 
 
Sub UnZip(strTargetPath As String, Fname As Variant)
 
    Dim oApp As Object
 
    Dim FileNameFolder As Variant
 
 
 
    If Right(strTargetPath, 1) <> Application.PathSeparator Then
 
        strTargetPath = strTargetPath & Application.PathSeparator
 
    End If
 
 
 
    FileNameFolder = strTargetPath
 
 
 
    Set oApp = CreateObject("Shell.Application")
 
    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
 
End Sub

Open in new window

0
Comment
Question by:kbay808
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40233187
If D12 only has the file name then change line 5 to this:
 Call UnZip("C:\Users\NC\Desktop\Vishesh\Test", "C:\Users\NC\Desktop\Vishesh\Test\" & Sheets("Main Menu").Range("D12").Value & ".zip")

Open in new window


You could make this a little easier to handle by assigning the filepath to a variable and using that in the statement like so:
Dim strFilePath as String
strFilePath = "C:\Users\NC\Desktop\Vishesh\Test"
Call UnZip(strFilePath, strFilePath & Sheets("Main Menu").Range("D12").Value & ".zip")

Open in new window


Regards,
-Glenn
0
 

Author Comment

by:kbay808
ID: 40233419
My question must be misleading.  I currently have a button for the user to click that will allow the user to use a windows explorer to navigate to the folder.  That path is then populated into cell D12.  I need to modify the code to accept the path given in D12.  The zip file will always be the same.

Example:
Path= Range (“D12”)
Filename= “TestZipFile.Zip”
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40233438
Okay, here's the modified code for what you described:
Dim strFilePath as String
strFilePath = Sheets("Main Menu").Range("D12").Value
Call UnZip(strFilePath, strFilePath & "TestZipFile.zip")

Open in new window


Hope that helps!
-Glenn
0
 

Author Closing Comment

by:kbay808
ID: 40237069
Thank you very much
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now