A source file call issue
Solved
-
Hello,
I would like to improve my cell copy macro from one file to another by including a dialog box to select the source file for copying. Having not found what I was looking for in various resources, I am reaching out to this forum which has already provided me with several solutions to my problems.
I would like that when the macro is launched, a dialog box opens and I can insert the name (or the path of the file) inside. This name will then be used in the macro as "Workbook1"
The goal is to be able to use this macro for several files without having to go back into the code each time to change the name of the source file.
Here is a part of my macro
Sub Macro4()
'
' Macro4 Macro
'
'
Windows("Workbook1").Activate
Sheets("Macro").Select
Range("B2").Select
Selection.Copy
Windows("Workbook2").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Thank you in advance.
I would like to improve my cell copy macro from one file to another by including a dialog box to select the source file for copying. Having not found what I was looking for in various resources, I am reaching out to this forum which has already provided me with several solutions to my problems.
I would like that when the macro is launched, a dialog box opens and I can insert the name (or the path of the file) inside. This name will then be used in the macro as "Workbook1"
The goal is to be able to use this macro for several files without having to go back into the code each time to change the name of the source file.
Here is a part of my macro
Sub Macro4()
'
' Macro4 Macro
'
'
Windows("Workbook1").Activate
Sheets("Macro").Select
Range("B2").Select
Selection.Copy
Windows("Workbook2").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Thank you in advance.
1 answer
-
-
Super, thanks for taking the time to respond,
but there's a small problem, the dialog box works but I don't see how to "call it" in the rest of my macro (under what name the variable has been stored)
Currently I have this:
Sub SelectionFichier01()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Title = "Select a file..."
fd.AllowMultiSelect = False
If fd.Show() Then
MsgBox "You have selected the file: " _
& vbCrLf & fd.SelectedItems(1), vbInformation
End If
Set fd = Nothing
End Sub
Sub Macrotestafb()
'
' Macro4 Macro
'
'
Call SelectionFichier01
Windows("fd").Activate
Sheets("Macro").Select
Range("B2").Select
Selection.Copy
Windows("Workbook2").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I'm getting the error "The index does not belong to the selection (it can't find it if I understood correctly)
and so I don't know under what name the imported file is stored (formerly "Workbook1" now "fd").
Thanks in advance -
Re,
This code is more appropriate, but since it doesn't open the file, it won't work.
Moreover, I don't understand what you want to do with your
ability to use this macro for multiple files without having to go back into the code each time to change the name of the source file.
Sub Macrotestafb() ' File = SelectionFichier01 If File = False Then MsgBox "Warning: no file selected!!!!!!!!!" Exit Sub End If End Sub Function SelectionFichier01() Dim strFileToOpen As Variant strFileToOpen = Application.GetOpenFilename _ (Title:="Please choose a file to open", _ FileFilter:="Excel Files *.xls* (*.xls*),") SelectionFichier01 = strFileToOpen End Function
-
Thank you for taking the time to respond to me again.
So, basically,
I have a destination file and a source file (both are different) that rigorously follow the same layout (in the destination file, cell B1 always corresponds to a number of potatoes, for instance, the same principle applies to the source file). I always have the same destination file to fill in. The source files have the same structure but differ only by name (the same structure among them but a different structure from the destination file). In short (maybe I’m dreaming about VBA capabilities), I would like to use my macro that copies my cells from one table to another:
Sub Macrotestafb()
'
' Macro4 Macro
'
'
Call SelectionFichier01
Windows("fichiersource.xlsx").Activate
Sheets("Macro").Select
Range("B2").Select
Selection.Copy
Windows("fichierdestination.xlsx").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Except that when I finish filling in my destination file with one source file, I want to change the source file without having to go into the macro code and manually change "fichiersource.xlsx" to "fichiersource2.xlsx" (open a window to indicate the new source file to use). To make it simple, I want to store the path of the source file in a variable that I could modify on the fly through a dialogue box like the link you sent me.
Thanks in advance. -
-
-