ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA Select File (https://www.excelbanter.com/excel-programming/440082-vba-select-file.html)

L.Mathe

VBA Select File
 
I am using Excel 2003. I have a workbook that runs a macro to search through
all files in a folder for specific text, and extract that text into the
worksheet. I need to create another macro where just a single file can be
selected then extract the data from the one file only. I tried to modify the
VBA, but run into an error code 'Object Required'. What I attempted to do
was:

Sub GetSingleFile()

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Dim fd As FileDialog
Set fd = Application.GetOpenFilename
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
Call ReadCSV(Folder, SearchData, DestSht)

End If
End With

Set fd = Nothing
End Sub

I have not included the Sub routine here, possibly that is where the problem
is. Could someone please help with this VBA?

Thank you!
--
Linda

Barb Reinhardt

VBA Select File
 
You're going to need to provide the code for ReadCSV.
--
HTH,

Barb Reinhardt



"L.Mathe" wrote:

I am using Excel 2003. I have a workbook that runs a macro to search through
all files in a folder for specific text, and extract that text into the
worksheet. I need to create another macro where just a single file can be
selected then extract the data from the one file only. I tried to modify the
VBA, but run into an error code 'Object Required'. What I attempted to do
was:

Sub GetSingleFile()

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Dim fd As FileDialog
Set fd = Application.GetOpenFilename
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
Call ReadCSV(Folder, SearchData, DestSht)

End If
End With

Set fd = Nothing
End Sub

I have not included the Sub routine here, possibly that is where the problem
is. Could someone please help with this VBA?

Thank you!
--
Linda


Dave Peterson

VBA Select File
 
Maybe...

Sub GetSingleFile()
dim DestSht as string
dim myFileName as variant

myfilename = application.getopenfilename
if myfilename = false then
'user hit cancel
exit sub
end if

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Call ReadCSV(myfilename, SearchData, DestSht)

End Sub

myFileName will be False (the boolean) if the user hits cancel. It will include
the path and filename if the user selects a file.


L.Mathe wrote:

I am using Excel 2003. I have a workbook that runs a macro to search through
all files in a folder for specific text, and extract that text into the
worksheet. I need to create another macro where just a single file can be
selected then extract the data from the one file only. I tried to modify the
VBA, but run into an error code 'Object Required'. What I attempted to do
was:

Sub GetSingleFile()

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Dim fd As FileDialog
Set fd = Application.GetOpenFilename
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
Call ReadCSV(Folder, SearchData, DestSht)

End If
End With

Set fd = Nothing
End Sub

I have not included the Sub routine here, possibly that is where the problem
is. Could someone please help with this VBA?

Thank you!
--
Linda


--

Dave Peterson

Chip Pearson

VBA Select File
 
Try some code like

Dim FileName As Variant
FileName = Application.GetOpenFilename
If FileName = False Then
Debug.Print "user cancelled"
Else
Debug.Print "file selected: " & FileName
End If

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




On Mon, 1 Mar 2010 05:59:01 -0800, L.Mathe
wrote:

I am using Excel 2003. I have a workbook that runs a macro to search through
all files in a folder for specific text, and extract that text into the
worksheet. I need to create another macro where just a single file can be
selected then extract the data from the one file only. I tried to modify the
VBA, but run into an error code 'Object Required'. What I attempted to do
was:

Sub GetSingleFile()

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Dim fd As FileDialog
Set fd = Application.GetOpenFilename
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
Call ReadCSV(Folder, SearchData, DestSht)

End If
End With

Set fd = Nothing
End Sub

I have not included the Sub routine here, possibly that is where the problem
is. Could someone please help with this VBA?

Thank you!


L.Mathe

VBA Select File
 
Much appreciated. I had to do a minor change to the sub routine it was
calling to deal with a single file instead of a group. But this works!

THANK YOU!
--
Linda


"Dave Peterson" wrote:

Maybe...

Sub GetSingleFile()
dim DestSht as string
dim myFileName as variant

myfilename = application.getopenfilename
if myfilename = false then
'user hit cancel
exit sub
end if

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Call ReadCSV(myfilename, SearchData, DestSht)

End Sub

myFileName will be False (the boolean) if the user hits cancel. It will include
the path and filename if the user selects a file.


L.Mathe wrote:

I am using Excel 2003. I have a workbook that runs a macro to search through
all files in a folder for specific text, and extract that text into the
worksheet. I need to create another macro where just a single file can be
selected then extract the data from the one file only. I tried to modify the
VBA, but run into an error code 'Object Required'. What I attempted to do
was:

Sub GetSingleFile()

DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
End With

Dim fd As FileDialog
Set fd = Application.GetOpenFilename
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
Call ReadCSV(Folder, SearchData, DestSht)

End If
End With

Set fd = Nothing
End Sub

I have not included the Sub routine here, possibly that is where the problem
is. Could someone please help with this VBA?

Thank you!
--
Linda


--

Dave Peterson
.



All times are GMT +1. The time now is 06:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com