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
206 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

776 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