Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 469
Default Help with activating window from Application.GetOpenFilename

I am new to VBA and I have spent hours trying to solve the following macro.

Currently, this code works how I want it except I would like to change the
of the file names of "Book1.xls" and "Book2.xls" to file paths for variables
"sFile1" and "dFile".

For example, if I selected "Book4.xls" and "Book5.xls", I would like the
activewindow to be reflected these file paths instead.

thanks for any help!

Curt J

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

Windows("Book1.xls").Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

Windows("Book1.xls").Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Help with activating window from Application.GetOpenFilename

I'd use something like:

Option Explicit
Sub CopyPaste()

Dim sFileName As Variant
Dim dFileName As Variant
Dim sFile As Workbook
Dim dFile As Workbook
Dim RngToCopy As Range
Dim DestCell As Range

sFileName = Application.GetOpenFilename
If sFileName = False Then
'user hit cancel
Exit Sub
End If

dFileName = Application.GetOpenFilename
If dFileName = False Then
Exit Sub
End If

Set sFile = Workbooks.Open(sFileName)
Set dFile = Workbooks.Open(dFileName)

'the entire sheet
Set RngToCopy = sFile.Worksheets("sheet1").Cells
'let excel resize as necessary
Set DestCell = dFile.Worksheets("sheet1").Range("A1")

RngToCopy.Copy _
Destination:=DestCell

Set RngToCopy = sFile.Worksheets("sheet2").Cells
Set DestCell = dFile.Worksheets("sheet2").Range("A1")

RngToCopy.Copy _
Destination:=DestCell

'and maybe...
sFile.Close savechanges:=False
dFile.Close savechanges:=True

End Sub


========
You could even loop through the worksheets if you wanted:

Option Explicit
Sub CopyPaste()

Dim sFileName As Variant
Dim dFileName As Variant
Dim sFile As Workbook
Dim dFile As Workbook
Dim RngToCopy As Range
Dim DestCell As Range
Dim wCtr As Long

sFileName = Application.GetOpenFilename
If sFileName = False Then
'user hit cancel
Exit Sub
End If

dFileName = Application.GetOpenFilename
If dFileName = False Then
Exit Sub
End If

Set sFile = Workbooks.Open(sFileName)
Set dFile = Workbooks.Open(dFileName)

For wCtr = 1 To 2
'the entire sheet
Set RngToCopy = sFile.Worksheets("sheet" & wCtr).Cells
'let excel resize as necessary
Set DestCell = dFile.Worksheets("sheet" & wCtr).Range("A1")

RngToCopy.Copy _
Destination:=DestCell
Next wCtr

sFile.Close savechanges:=False
dFile.Close savechanges:=True

End Sub

Curt wrote:

I am new to VBA and I have spent hours trying to solve the following macro.

Currently, this code works how I want it except I would like to change the
of the file names of "Book1.xls" and "Book2.xls" to file paths for variables
"sFile1" and "dFile".

For example, if I selected "Book4.xls" and "Book5.xls", I would like the
activewindow to be reflected these file paths instead.

thanks for any help!

Curt J

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

Windows("Book1.xls").Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

Windows("Book1.xls").Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Help with activating window from Application.GetOpenFilename

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

sFile.Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

dFile.Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

sFile.Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

dFile.Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub





"Curt" wrote in message
...
I am new to VBA and I have spent hours trying to solve the following macro.

Currently, this code works how I want it except I would like to change the
of the file names of "Book1.xls" and "Book2.xls" to file paths for
variables
"sFile1" and "dFile".

For example, if I selected "Book4.xls" and "Book5.xls", I would like the
activewindow to be reflected these file paths instead.

thanks for any help!

Curt J

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

Windows("Book1.xls").Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

Windows("Book1.xls").Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 469
Default Help with activating window from Application.GetOpenFilename

thanks!

"JLGWhiz" wrote:

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

sFile.Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

dFile.Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

sFile.Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

dFile.Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub





"Curt" wrote in message
...
I am new to VBA and I have spent hours trying to solve the following macro.

Currently, this code works how I want it except I would like to change the
of the file names of "Book1.xls" and "Book2.xls" to file paths for
variables
"sFile1" and "dFile".

For example, if I selected "Book4.xls" and "Book5.xls", I would like the
activewindow to be reflected these file paths instead.

thanks for any help!

Curt J

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

Windows("Book1.xls").Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

Windows("Book1.xls").Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub



.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 469
Default Help with activating window from Application.GetOpenFilename

thanks this was great!

"Dave Peterson" wrote:

I'd use something like:

Option Explicit
Sub CopyPaste()

Dim sFileName As Variant
Dim dFileName As Variant
Dim sFile As Workbook
Dim dFile As Workbook
Dim RngToCopy As Range
Dim DestCell As Range

sFileName = Application.GetOpenFilename
If sFileName = False Then
'user hit cancel
Exit Sub
End If

dFileName = Application.GetOpenFilename
If dFileName = False Then
Exit Sub
End If

Set sFile = Workbooks.Open(sFileName)
Set dFile = Workbooks.Open(dFileName)

'the entire sheet
Set RngToCopy = sFile.Worksheets("sheet1").Cells
'let excel resize as necessary
Set DestCell = dFile.Worksheets("sheet1").Range("A1")

RngToCopy.Copy _
Destination:=DestCell

Set RngToCopy = sFile.Worksheets("sheet2").Cells
Set DestCell = dFile.Worksheets("sheet2").Range("A1")

RngToCopy.Copy _
Destination:=DestCell

'and maybe...
sFile.Close savechanges:=False
dFile.Close savechanges:=True

End Sub


========
You could even loop through the worksheets if you wanted:

Option Explicit
Sub CopyPaste()

Dim sFileName As Variant
Dim dFileName As Variant
Dim sFile As Workbook
Dim dFile As Workbook
Dim RngToCopy As Range
Dim DestCell As Range
Dim wCtr As Long

sFileName = Application.GetOpenFilename
If sFileName = False Then
'user hit cancel
Exit Sub
End If

dFileName = Application.GetOpenFilename
If dFileName = False Then
Exit Sub
End If

Set sFile = Workbooks.Open(sFileName)
Set dFile = Workbooks.Open(dFileName)

For wCtr = 1 To 2
'the entire sheet
Set RngToCopy = sFile.Worksheets("sheet" & wCtr).Cells
'let excel resize as necessary
Set DestCell = dFile.Worksheets("sheet" & wCtr).Range("A1")

RngToCopy.Copy _
Destination:=DestCell
Next wCtr

sFile.Close savechanges:=False
dFile.Close savechanges:=True

End Sub

Curt wrote:

I am new to VBA and I have spent hours trying to solve the following macro.

Currently, this code works how I want it except I would like to change the
of the file names of "Book1.xls" and "Book2.xls" to file paths for variables
"sFile1" and "dFile".

For example, if I selected "Book4.xls" and "Book5.xls", I would like the
activewindow to be reflected these file paths instead.

thanks for any help!

Curt J

Sub CopyPaste()

Dim sFile As Workbook
Set sFile = Workbooks.Open(Application.GetOpenFilename)


Dim dFile As Workbook
Set dFile = Workbooks.Open(Application.GetOpenFilename)

Windows("Book1.xls").Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet1").Select
Cells.Select
ActiveSheet.Paste

Windows("Book1.xls").Activate
Sheets("Sheet2").Select
Cells.Select
Selection.Copy

Windows("Book2.xls").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste


End Sub


--

Dave Peterson
.

Reply
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
Application.GetOpenFileName Chris Excel Programming 7 August 19th 06 12:37 AM
Application.GetOpenFilename vs Application.Dialogs(xlDialogsOpen) Paul Martin Excel Programming 5 August 5th 05 04:44 PM
Application.GetOpenFilename Greg[_19_] Excel Programming 3 February 20th 05 01:55 AM
Application.GetOpenFilename Philipp Oberleitner[_2_] Excel Programming 2 July 9th 04 07:29 PM
Application.GetOpenFileName ptrowe Excel Programming 2 September 11th 03 12:54 PM


All times are GMT +1. The time now is 07:33 AM.

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

About Us

"It's about Microsoft Excel"