ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Command Button to be used as Browse... for selecting a file to be used... (https://www.excelbanter.com/excel-programming/344120-command-button-used-browse-selecting-file-used.html)

RPIJG[_71_]

Command Button to be used as Browse... for selecting a file to be used...
 

The command button is part of a userform. I need the command button to
open a browse files window so that the user can select the file. This
file will then be opened, certain data ranges will be selected, and
then copied, and then the workbook will be closed, and those copied
values will be pasted into the original workbook. How can I do this, I
have a pretty good idea about the selecting, copying, pasting part, but
I can't figure out how to get the browse files window, and then have
the program do those actions on the user selected file. Thanks.


--
RPIJG
------------------------------------------------------------------------
RPIJG's Profile: http://www.excelforum.com/member.php...fo&userid=9285
View this thread: http://www.excelforum.com/showthread...hreadid=480140


Tom Ogilvy

Command Button to be used as Browse... for selecting a file to be used...
 
Dim fName as String
Dim bk as Workbook

fName = Application.GetOpenFileName(Excel Files (*.xls),*.xls")

if fName = "False" then
' user hit cancel'
exit sub
End if

set bk = workbooks.Open(fName)

bk.Worksheets("Data").Range("A1:F20").Copy _
Destination:=ThisWorkbook.worksheets("Master") _
.Cells(rows.count,1).End(xlup).Offset(1,0)
bk.Close SaveChanges:=False

--
Regards,
Tom Ogilvy

"RPIJG" wrote in
message ...

The command button is part of a userform. I need the command button to
open a browse files window so that the user can select the file. This
file will then be opened, certain data ranges will be selected, and
then copied, and then the workbook will be closed, and those copied
values will be pasted into the original workbook. How can I do this, I
have a pretty good idea about the selecting, copying, pasting part, but
I can't figure out how to get the browse files window, and then have
the program do those actions on the user selected file. Thanks.


--
RPIJG
------------------------------------------------------------------------
RPIJG's Profile:

http://www.excelforum.com/member.php...fo&userid=9285
View this thread: http://www.excelforum.com/showthread...hreadid=480140




RPIJG[_72_]

Command Button to be used as Browse... for selecting a file to be used...
 

Ok, so I'm getting there... I think... slowly... this is what I've got
so far... However, I need to make it so that the code for the button
stops if you exit the open dialogue instead of selecting a file to open
(otherwise it closes the workbook).


Code:
--------------------

Private Sub CommandButton1_Click()
'Open Dialogue
Application.FindFile
'Select Data from Opened File
ActiveWorkbook.Sheets("Sheet1").Range("A14:L21").S elect
'Copy Data from Opened File
Selection.Copy
'Close Opened File
ActiveWorkbook.Close
'Paste Data into Template File
ActiveWorkbook.Sheets("Sheet1").Range("A28:L35").S elect
ActiveSheet.Paste
End Sub

--------------------


--
RPIJG
------------------------------------------------------------------------
RPIJG's Profile: http://www.excelforum.com/member.php...fo&userid=9285
View this thread: http://www.excelforum.com/showthread...hreadid=480140


RPIJG[_74_]

Command Button to be used as Browse... for selecting a file to be used...
 

Fantastic thanks Tom


--
RPIJG
------------------------------------------------------------------------
RPIJG's Profile: http://www.excelforum.com/member.php...fo&userid=9285
View this thread: http://www.excelforum.com/showthread...hreadid=480140


Tom Ogilvy

Command Button to be used as Browse... for selecting a file to be used...
 
You have received an answer that does what you asked.

--
Regards,
Tom Ogilvy

"RPIJG" wrote in
message ...

Ok, so I'm getting there... I think... slowly... this is what I've got
so far... However, I need to make it so that the code for the button
stops if you exit the open dialogue instead of selecting a file to open
(otherwise it closes the workbook).


Code:
--------------------

Private Sub CommandButton1_Click()
'Open Dialogue
Application.FindFile
'Select Data from Opened File
ActiveWorkbook.Sheets("Sheet1").Range("A14:L21").S elect
'Copy Data from Opened File
Selection.Copy
'Close Opened File
ActiveWorkbook.Close
'Paste Data into Template File
ActiveWorkbook.Sheets("Sheet1").Range("A28:L35").S elect
ActiveSheet.Paste
End Sub

--------------------


--
RPIJG
------------------------------------------------------------------------
RPIJG's Profile:

http://www.excelforum.com/member.php...fo&userid=9285
View this thread: http://www.excelforum.com/showthread...hreadid=480140





All times are GMT +1. The time now is 01:42 PM.

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