PDA

View Full Version : fixing show message when open workbook from dialog to copy data



maghari
06-16-2020, 01:38 PM
hello
this is my code doesn't work very well i no know it works until this line

1 MsgBox "name of the file is not correct", vbCritical

i'm sure about the name of opened file it supposes open the file and copy data but the code open the file but not copy data because show the message
i hope some body help


Sub Test()
Dim file_name As Variant
Dim open_wk As Workbook
On Error GoTo 1
file_name = Application.GetOpenFilename(Title:="choose the file ", FileFilter:="Excel Files *.xlsx (*.xlsx),")
If file_name = False Then1 MsgBox "name of the file is not correct", vbCritical
Exit Sub
Else
Set open_wk = Workbooks.Open(file_name)
EndIf
Windows("TT1.xlsx").Activate
Sheets("sheet1").Select
Range("a2:E15").Select
Selection.CopyThisWorkbook.Activate
Sheets("sheet1").Select
Range("b2").Select
Selection.PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Close
Application.CutCopyMode = False
End Sub

Paul_Hossler
06-16-2020, 01:50 PM
Get rid of 'Then1' and make it just 'Then'



If file_name = False Then1 MsgBox "name of the file is not correct", vbCritical

maghari
06-16-2020, 01:55 PM
it gives me error in this line
On Error GoTo 1 i delete 1 also gives error

Paul_Hossler
06-16-2020, 04:13 PM
I don't think you need the GoTo




Option Explicit


Sub Test()
Dim file_name As Variant
Dim open_wk As Workbook


file_name = Application.GetOpenFilename(Title:="choose the file ", FileFilter:="Excel Files *.xlsx (*.xlsx),")
If file_name = False Then
MsgBox "name of the file is not correct", vbCritical
Exit Sub
Else
Set open_wk = Workbooks.Open(file_name)
End If


Windows("touati1.xlsx").Activate
Sheets("sheet1").Select
Range("a2:d15").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("sheet1").Select
Range("b2").Select
Selection.PasteSpecial Paste:=xlPasteValues
ThisWorkbook.Close

Application.CutCopyMode = False
End Sub

maghari
06-16-2020, 04:57 PM
it gives me error run time 1004

Paul_Hossler
06-16-2020, 06:59 PM
I could only look to see if it would open a workbook without error


I'm not a mind reader

Which line gives you the 1004 error?

It could be that "touati1.xlsx" doesn't exist, or there is no "Sheet1", or something else

maghari
06-16-2020, 08:05 PM
the error in this line

Range("a2:d15").Select