VBA openCopyandClose

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
Hi, found the below code which is almost what i am after. I need the code to be flexible where the code will allow me to choose the file and when i choose look for sheet "Current", if found then Copy A1:D to Thisworkbook sheet("Test").Range("A1") Else Exit Sub



Code:
Sub OpenCopyAndClose()
 Dim excelFile As String

 excelFile = "myfile.xls"

  'open excel file "myfile.xls" in drive D:\
  Workbooks.Open "D:\" & excelFile

  'copy value of range A1 from "myfile.xls"
  ThisWorkbook.Sheets(1).Range("D1").Value = _
    Workbooks(excelFile).Sheets(1).Range("A1").Value

  'close "myfile.xls" file and don't save any changes
  Workbooks(excelFile).Close SaveChanges:=False
End Sub
</pre>
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The below (untested) code should work:

Sub OpenCopyAndClose()
Dim excelFile As String

excelFile = "myfile.xls"

'open excel file "myfile.xls" in drive D:\
Workbooks.Open "D:\" & excelFile

'copy value of range A1 from "myfile.xls"
If Len(Sheets("Current").Name) > 0 Then ThisWorkbook.Sheets(1).Range("D1").Value = _
Workbooks(excelFile).Sheets(1).Range("A1").Value
'close "myfile.xls" file and don't save any changes
Workbooks(excelFile).Close SaveChanges:=False
End Sub
 
Upvote 0
But i dont want to hardcode the file name as this changes everymonth

excelFile = "myfile.xls"
 
Upvote 0
This will allow you to select an Excel file and store its name in your existing variable excelFile:-
Code:
  excelFile = Application.GetOpenFilename(FileFilter:="Excel workbooks (*.xl*), *.xl*")
  If excelFile = "False" Then Exit Sub
If you want the dialog box to start in a specific place, change the drive and/or directory first. For example:-
Code:
  ChDrive Left(ThisWorkbook.Path, 2)
  ChDir Mid(ThisWorkbook.Path & "\", 3)
 
Upvote 0
So you saying something like this? Cos when i run the code i get an error on the copy part of the code

Code:
Sub OpenCopyAndClose()
 Dim excelFile As String

  excelFile = Application.GetOpenFilename(FileFilter:="Excel workbooks (*.xl*), *.xl*")
  If excelFile = "False" Then Exit Sub

  'copy value of range A1 from "myfile.xls"
  ThisWorkbook.Sheets("Test").Range("A6").Value = _
    Workbooks(excelFile).Sheets("Current").Range("B6:D1000").Value

  'close "myfile.xls" file and don't save any changes
  Workbooks(excelFile).Close SaveChanges:=False
End Sub
 
Upvote 0
Hi Donai,
Try:
Rich (BB code):

Sub OpenCopyAndClose1()
  
  '--> Settings, change to suit
  Const SourceSheet$ = "Current"  ' Source sheet name
  Const SourceRange$ = "B6:D6"    ' 1st row of the source data
  Const DestSheet$ = "Test"       ' Destination sheet name
  Const DestRange$ = "A6"         ' 1st cell of the destination sheet
  ' <-- End of settings
  
  Dim Rng As Range
  
  ' Load source workbook via dialog
  With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = ThisWorkbook.Path
    .AllowMultiSelect = False
    .Filters.Clear
    .Filters.Add "Excel workbooks (*.xl*)", "*.xl*"
    On Error Resume Next
    If .Show Then .Execute Else Exit Sub
    If Err Then MsgBox "Canceled": Exit Sub
  End With
  
  ' Copy & paste data
  On Error GoTo exit_
  Application.ScreenUpdating = False
  With ActiveWorkbook
    ' Copy source data
    With .Sheets(SourceSheet)
      Set Rng = .Range(SourceRange).Resize(.UsedRange.Rows.Count)
    End With
    Rng.Copy
    ' Paste data to the destination
    With ThisWorkbook.Sheets(DestSheet)
      .Range(DestRange).Resize(Rng.Rows.Count, Rng.Columns.Count).PasteSpecial xlPasteValuesAndNumberFormats
    End With
    Application.CutCopyMode = False
    ' Close source workbook
    .Close False
  End With
  
exit_:
  Application.ScreenUpdating = True
  If Err Then MsgBox Err.Description Else MsgBox "Well done"
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top