LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Member
 
Posts: 70
Default Open File Dialog and store opened file as variable

I'm trying to open a file form the file dialog box and then store the opened file as a variable so that I can reference it in the rest of my code. Here is what I've got so far:

Code:
Sub selectiveCopy()
Dim bottom As Range, headerRow As Range, cell As Range
Dim targetCell As Range, targetSheet As Worksheet
Dim lastcell As Integer

    'Here is where I want to select the file and store it as "sFileName". This works fine, but doesn't open the actual file, just gets the path.

   sFileName = Application.GetOpenFilename 

   'In order to open the file, I have to run this:

    Application.FileDialog(msoFileDialogOpen)
       .Show
       .Execute

   ' Any way I can just use one? 

Set headerRow = ActiveSheet.Range("1:1")

'Here is where I want to start calling the file variable "sFileName" I get an "object required" error. 
    Set targetSheet = sFileName
    sFileName.Activate

  targetSheet.Cells(1, 1).Select
   lastcell = targetSheet.Range("A1").End(xlDown).Row
Set targetCell = targetSheet.Cells(1, 1)
For Each cell In headerRow
Select Case cell.Value
Case "value1 to copy", "value2 to copy", "value3 to copy"
Set bottom = Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, _
cell.Column)
If bottom.Value < "" Then
Range(cell.Address & ":" & bottom.Address).Copy
Else
Range(cell.Address & ":" & Cells(bottom.End(xlUp).Row, _
cell.Column).Address).Copy
End If
targetSheet.Paste Destination:=targetCell
Set targetCell = targetCell.Offset(0, 1)
targetSheet.Paste Destination:=targetCell
End Select
Next
End Sub
I also have another sub after this one to select that opened file (sFileName) after the loop is done, but maybe we can activate it in this sub somewhere after the loop? I'm still new at VBA, so I'm not sure what can be done. Any help is greatly appreciated!

Last edited by KeriM : July 23rd 12 at 05:17 PM
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't open a file I just opened the day before. DJ Excel Discussion (Misc queries) 0 October 28th 07 11:41 PM
cannot open the excel file, the file is already opened cannot open the excel document Excel Discussion (Misc queries) 1 May 19th 06 07:45 AM
File Browse Function (Open dialog box for choosing file) b-123 Excel Programming 1 August 25th 05 10:06 PM
How set file open path to filepath of file opened with Explorer ? RandyDtg1 Excel Programming 0 May 14th 04 02:05 AM
open file dialog-select file-import worksheet Divinedar Excel Programming 1 January 16th 04 07:13 PM


All times are GMT +1. The time now is 07:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"