K
KeriM
I'm trying to open a file form the file dialog box and then store th
opened file as a variable so that I can reference it in the rest of m
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 fil
(sFileName) after the loop is done, but maybe we can activate it in thi
sub somewhere after the loop? I'm still new at VBA, so I'm not sure wha
can be done. Any help is greatly appreciated
opened file as a variable so that I can reference it in the rest of m
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 fil
(sFileName) after the loop is done, but maybe we can activate it in thi
sub somewhere after the loop? I'm still new at VBA, so I'm not sure wha
can be done. Any help is greatly appreciated