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

  • Thread starter Thread starter RPIJG
  • Start date Start date
R

RPIJG

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.
 
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
 
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").Select
'Copy Data from Opened File
Selection.Copy
'Close Opened File
ActiveWorkbook.Close
'Paste Data into Template File
ActiveWorkbook.Sheets("Sheet1").Range("A28:L35").Select
ActiveSheet.Paste
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top