W.E.B
asked on
VBA to open file
Hello,
I'm using below code to open excel file , copy paste special.
The file name is not always the same,
is there a way that I can pick the file and then do the copy / paste special.
or, pick up the file automatically from specific folder.
the file name will always start with QQ Order
Sub Macro3()
Workbooks.Open Filename:="C:\Users\Wassim \Desktop\Q Q Order Master May 11.xlsx"
Range("A2").Select
Range(Selection, Selection.End(xlToRight)). Select
Range("A2:AT2").Select
Selection.Copy
Windows("- Client.xlsm").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Windows("QQ Order Master May 11.xlsx").Activate
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("A4").Select
ActiveWorkbook.Save
ActiveWindow.Close
Range("A1").Select
ActiveWorkbook.Save
End Sub
I'm using below code to open excel file , copy paste special.
The file name is not always the same,
is there a way that I can pick the file and then do the copy / paste special.
or, pick up the file automatically from specific folder.
the file name will always start with QQ Order
Sub Macro3()
Workbooks.Open Filename:="C:\Users\Wassim
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).
Range("A2:AT2").Select
Selection.Copy
Windows("- Client.xlsm").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Windows("QQ Order Master May 11.xlsx").Activate
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("A4").Select
ActiveWorkbook.Save
ActiveWindow.Close
Range("A1").Select
ActiveWorkbook.Save
End Sub
ASKER
I get error
Workbooks.Open Filename:=Trim(.SelectedIt ems.Item(1 ))
Invalid or unqualified reference.
thanks
Workbooks.Open Filename:=Trim(.SelectedIt
Invalid or unqualified reference.
thanks
Try it this way.
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.InitialFileName = "C:\Users\Wassim\Desktop\QQ Orders*.*"
.Filters.Clear
If .Show = -1 Then
Workbooks.Open Filename:=Trim(.SelectedItems.Item(1))
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range("A2:AT2").Select
Selection.Copy
Windows("- Client.xlsm").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Windows("QQ Order Master May 11.xlsx").Activate
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("A4").Select
ActiveWorkbook.Save
ActiveWindow.Close
Range("A1").Select
ActiveWorkbook.Save
End If
End With
Set fd = Nothing
ASKER
it opens the folder, but will not display any file.
it displays the folders.
thanks
it displays the folders.
thanks
ASKER
OK i can see the file, (I had the wrong file path name)
but I get an error
on this line
Windows("QQ Order Master May 11.xlsx").Activate
The file name start with QQ Order xxxx
I tried to change it to Windows("QA Order*.*").Activate
I get error,
substrict out of range.
thanks
but I get an error
on this line
Windows("QQ Order Master May 11.xlsx").Activate
The file name start with QQ Order xxxx
I tried to change it to Windows("QA Order*.*").Activate
I get error,
substrict out of range.
thanks
I'm sorry. remove the "s" from the file name in line 6. In other words
.InitialFileName = "C:\Users\Wassim\Desktop\Q Q Order*.*"
.InitialFileName = "C:\Users\Wassim\Desktop\Q
ASKER
Thanks,
I get error on line
Windows("QQ Order Master May 11.xlsx").Activate
I tried to change it to Windows("QA Order*.*").Activate
I get error,
substrict out of range.
When I change it to exact same file name
Windows("QQ Order Master May 24.xlsx").Activate
I don't get any error, but no copy or paste special is done.
it just opens the file, and close it.
thanks
I get error on line
Windows("QQ Order Master May 11.xlsx").Activate
I tried to change it to Windows("QA Order*.*").Activate
I get error,
substrict out of range.
When I change it to exact same file name
Windows("QQ Order Master May 24.xlsx").Activate
I don't get any error, but no copy or paste special is done.
it just opens the file, and close it.
thanks
Did the macro3 work for you before you asked this question?
ASKER
yes,
thanks
thanks
Without a file to test with it's hard to know what's wrong, but if there's only one workbook involved with your code then try changing these two lines
Windows("- Client.xlsm").Activate
Windows("QQ Order Master May 11.xlsx").Activate
to
Sheets("Sheet1").Activate ' Change Sheet1 to the name of the sheet
Sheets("Sheet2").Activate ' Change Sheet2 to the name of the sheet
Windows("- Client.xlsm").Activate
Windows("QQ Order Master May 11.xlsx").Activate
to
Sheets("Sheet1").Activate ' Change Sheet1 to the name of the sheet
Sheets("Sheet2").Activate ' Change Sheet2 to the name of the sheet
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for your time and help.
it's working.
it's working.
You're welcome and I'm glad I was able to help, I'm just sorry it took so long.
In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
Open in new window