Advertisement

08.02.2004 at 07:26AM PDT, ID: 21079222
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

VB macro for Excel to open a file and save it's name in a variable for later use

Tags: excel, macro, open, file, vb
I asked the same question earlier, but perhaps this question belongs in this section.

I am writing a small piece of VB code for Excel to prompt the user to open the file he needs to work with, and for the vb code to save the name of the file in a variable. If in case the file that the user will work with is already open then ask him to identify the file that he will be working with and then save the name of the file in that variable.

So now I will have to two active windows. How can I switch between the two windows?
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: amitscorp
Solution Provided By: brettdj
Participating Experts: 2
Solution Grade: A
Views: 469
Translate:
Loading Advertisement...
08.02.2004 at 07:59AM PDT, ID: 11694296

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 08:17AM PDT, ID: 11694510

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 09:01AM PDT, ID: 11694959

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 10:01AM PDT, ID: 11695684

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 01:09PM PDT, ID: 11697600

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 01:32PM PDT, ID: 11697850

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 01:40PM PDT, ID: 11697960

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 01:51PM PDT, ID: 11698143

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 03:58PM PDT, ID: 11699170

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 04:05PM PDT, ID: 11699211

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.02.2004 at 04:36PM PDT, ID: 11699390

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
08.02.2004 at 07:59AM PDT, ID: 11694296

Rank: Genius

This code stores the file name the user returns in the public variable Fname. If the file is already open then the sub will not open the file again and if the user cancels the file name procedure then the sub exits

Cheers

Dave

Public Fname

Sub OpenF()
    Dim wb As Workbook
    Fname = Application.GetOpenFilename("Excel-files,*.xls", , "Please open your file")
    'no file selected
    If Fname = False Then Exit Sub
    For Each wb In Application.Workbooks
        If wb.Path & "\" & wb.Name = Fname Then
            MsgBox "File " & wb.Name & " is already open"
            Exit Sub
        End If
    Next
    Workbooks.Open (Fname)
End Sub
Accepted Solution
 
08.02.2004 at 08:17AM PDT, ID: 11694510
Dave,
Fantastic this is a very efficient way of writing the code. Can you also tell me what changes I need to make if the file were a *.csv file instead?

Thanks,
Amit
 
08.02.2004 at 09:01AM PDT, ID: 11694959
Dave,
How can I call this file name at a different place in the vbcode (in the same sub though). How should I define this filename as a global variable?

Thanks.
Amit
 
08.02.2004 at 10:01AM PDT, ID: 11695684

Rank: Genius

Hi Amit,
Dave is probably asleep now, so I will try to help you in his absence. The points are his though, please.

To adapt to a csv file, just change the .xls to .csv
Fname = Application.GetOpenFilename("Excel-files,*.csv", , "Please open your file")

To refer to the file elsewhere, use
Workbooks(Fname)

Fname is declared as Public, and so is available to all of your other subs and functions.

Cheers!

Brad
 
08.02.2004 at 01:09PM PDT, ID: 11697600
Dave,
Thanks a lot. It really helped.

Brad,
Thanks for the suggestions with csv file. That was kind of stupid to ask about that, I had later figured that out too...
However, Workbooks(Fname) did not work and I tried Workbooks(wb.name) and that worked. Fname had the entire path stored in it.

Thanks a lot. I really appreciate you guys helping out.
Amit
 
08.02.2004 at 01:32PM PDT, ID: 11697850
Brad,
If you are still online, can you help me with this thing please.

I am writing a VB macro to work between two files, switching back and forth between the two files. I am trying to select multiple columns in one of the files. After I recorded the macro individually, it worked fine. But when I included this into the bigger macro it fails. The vb code is below:

Windows(wb.Name).Activate
Columns("P:Q").Select

I don't think the macro activates the file, even though it says that the window is active (the file window does not turn blue!)
Below is the error message I get at the first line above...
Run-time error 1004:
Application defined or object defined error

I also posted this question under the title "Activate window to perform select function". Thanks. Amit
 
08.02.2004 at 01:40PM PDT, ID: 11697960

Rank: Genius

Amit,
I prefer:
1) to keep workbooks in a single window rather than deal with two windows
2) use two workbook variables, wb and wb2 which I set as soon the workbook is opened
3) use With...End With blocks to make sure I am referring to the proper workbook

If I had to select a range (and I would work very hard to avoid it), I would do so like:

Dim wb as Workbook
Dim ws as Worksheet
Workbooks.Open FName
Set wb=ActiveWorkbook
Set ws=ActiveSheet
'Your code
wb.Activate    'Activates a particular workbook
ws.Activate     'Activates a particular worksheet
Columns("P:Q").Select     'Select columns P & Q on that worksheet and workbook

Brad
 
08.02.2004 at 01:51PM PDT, ID: 11698143
Brad,
I sent you the file with the macro to your vbaexpress id that is on the webpage. Can you please take a look into it.
Thanks
Amit
 
08.02.2004 at 03:58PM PDT, ID: 11699170

Rank: Genius

Thanks for the grade Amit and thanks for the additional assistance Brad, much appreciated.

Cheers

Dave
 
08.02.2004 at 04:05PM PDT, ID: 11699211

Rank: Genius

Amit,
This is what I had in mind:

'This function goes in regular module sheet:
Function GetFileName(str As String) As String
Dim i As Long
If InStr(1, str, "\") = 0 Then
    GetFileName = "False"
    Exit Function
End If
For i = Len(str) To 1 Step -1
    If Mid(str, i, 1) = "\" Then
        GetFileName = Right(str, Len(str) - i)
        Exit Function
    End If
Next i
End Function


'Your sub in Sheet1 code pane
Sub Vlookupp()  'It's not good practice to use a keyword as the name of a sub, function or variable

           
' User workbook
    Dim wb As Workbook, wb2 As Workbook
    Dim fName As String, fName2 As String
    Dim ws As Worksheet, ws2 As Worksheet
    Dim lRows As Long, lRows2 As Long
   
    fName = GetFileName(Application.GetOpenFilename("Excel-files,*.xls", , "Please open your file"))
    'no file selected
           If fName = "False" Then Exit Sub
    On Error Resume Next
    Workbooks(fName).Activate
    If Err <> 0 Then
        Workbooks.Open (fName)
        Err.Clear
    End If
    On Error GoTo 0
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet
   
'OpenCompAnalysisPortfolio
    fName2 = GetFileName(Application.GetOpenFilename("Excel-files,*.xls", , "Please open your Comp Analysis Portfolio"))
    'no file selected
    If fName2 = "False" Then Exit Sub
    On Error Resume Next
    Workbooks(fName).Activate
    If Err <> 0 Then
        Workbooks.Open (fName2)
        Err.Clear
    End If
    On Error GoTo 0
    Set wb2 = ActiveWorkbook
    Set ws2 = ActiveSheet
 
    lRows = ws.Cells(Rows.Count, 1).End(xlUp).Row 'change 1 to your column number
    lRows2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row 'change 1 to your column number
     
' Read the last row in the file
     
' Move columns in comp analysis portfolio

'    wb2.ActiveSheet(Cells(1, 1)).Select
    'Cells(1, 1).Select
 Windows(wb.Name).Activate
    ws.Range("P:Q").Copy    'This doesn't look too useful without a Paste Special
     Columns("A:A").Insert

' Vlook up
    Windows("HPAWW-Macro_ver3.xls").Activate     'I get error here because no file by that name
'Balance of your code not debugged

 
08.02.2004 at 04:36PM PDT, ID: 11699390

Rank: Genius

Amit,
Not enough testing. Try this instead:

Sub Vlookupp()  'It's not good practice to use a keyword as the name of a sub, function or variable
           
' User workbook
    Dim wb As Workbook, wb2 As Workbook
    Dim fName As String, fName2 As String, str As String
    Dim ws As Worksheet, ws2 As Worksheet
    Dim lRows As Long, lRows2 As Long
   
    str = Application.GetOpenFilename("Excel-files,*.xls", , "Please open your file")
    fName = GetFileName(str)
    'no file selected
           If fName = "False" Then Exit Sub
    On Error Resume Next
    Workbooks(fName).Activate
    If Err <> 0 Then
        Workbooks.Open (str)
        Err.Clear
    End If
    On Error GoTo 0
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet
   
'OpenCompAnalysisPortfolio
    str = Application.GetOpenFilename("Excel-files,*.xls", , "Please open your Comp Analysis Portfolio")
    fName2 = GetFileName(str)
    'no file selected
    If fName2 = "False" Then Exit Sub
    On Error Resume Next
    Workbooks(fName2).Activate
    If Err <> 0 Then
        Workbooks.Open (str)
        Err.Clear
    End If
    On Error GoTo 0
    Set wb2 = ActiveWorkbook
    Set ws2 = ActiveSheet
 
    lRows = ws.Cells(Rows.Count, 1).End(xlUp).Row 'change 1 to your column number
    lRows2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row 'change 1 to your column number
     
' Read the last row in the file
   
   
' Move columns in comp analysis portfolio

'    wb2.ActiveSheet(Cells(1, 1)).Select
    'Cells(1, 1).Select
 Windows(wb.Name).Activate
    ws.Range("P:Q").Copy    'This doesn't look too useful without a Paste Special
    'Range(Cells(1, 16), Cells(lRows2, 16)).Select
    'Selection.Cut
    'Application.CutCopyMode = False
    'Selection.Copy
    Columns("A:A").Insert

' Vlook up
    Windows("HPAWW-Macro_ver3.xls").Activate
'Your code follows here

Brad
 
 
20080236-EE-VQP-29