Code stopped working

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I have been updating a workbook by adding some new sheets and some code that only works on these sheets but the existing code to import the data no longer works. I have tried this code on a new workbook and it works fine, the errors that I have got so far are a Compile error and run time erro 438. Can anyone tell me why this is doing this?

here is the import code from the existing workbook, it is currently stopping on the red text.

Rich (BB code):
Sub Import()
Dim Finfo As String
Dim Title As String
Dim FileName As Variant
Dim CurrentFile As Variant
Dim CurrentFileName As String
Dim NewFileName As String
'Import 71mm
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Sheets("71mm").Select
    CurrentFileName = ActiveWorkbook.Name
    Title = "Select 71mm Profile"
    FileName = Application.GetOpenFilename(Finfo, FilterIndex, Title)
    If FileName = False Then
    MsgBox "No file was selected."
    Exit Sub
    End If
    Workbooks.Open FileName, ReadOnly:=1, UpdateLinks:=False
    NewFileName = ActiveWorkbook.Name
    Sheets("profiles").Select
    Range("a1:n1809").Select
    Selection.Copy
    Workbooks(CurrentFileName).Activate  'Go back to the original file
    Sheets("71mm").Select
    Range("a1").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False 'Paste the copied cells
    Range("a1").Select
    Workbooks(NewFileName).Activate 'Go back to the new file
    Application.CutCopyMode = False
    ActiveWorkbook.Close False 'close new file
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Maybe like this:

Code:
Sub Import()
    Dim wks         As Worksheet
    Dim sFile       As String
 
    Application.ScreenUpdating = False
    Application.EnableEvents = False
 
    Set wks = ActiveWorkbook.Worksheets("71mm")
    sFile = Application.GetOpenFilename(FilterIndex:="Excel files, *.xlsx; *.xlsm", _
                                        Title:="Select 71mm Profile")
    If sFile = "False" Then
        MsgBox "No file was selected."
    
    Else
        With Workbooks.Open(FileName:=sFile, ReadOnly:=True, UpdateLinks:=False)
            .Worksheets("Profiles").Range("A1:N1809").Copy
            wks.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            .Close SaveChanges:=False
        End With
    End If
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
 
Upvote 0
I think you are overwriting your original selection that you want copied , "Range("a1:n1809").select"

with "Range("a1").Select.Selection.PasteSpecial"


Try it without selecting the sheet or range A1

Code:
    Workbooks(CurrentFileName).Activate  'Go back to the original file
    Sheets("71mm").Activate
    Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False 'Paste the copied cells
 
Last edited:
Upvote 0
Assuming the Finfo and the FilterIndex are variables provided with answers from elsewhere:
Code:
Sub Import()
Dim wbCurr  As Workbook
Dim wb71mm  As Workbook
Dim fName   As String

'Import 71mm
    Title = "Select 71mm Profile"
    Set wbCurr = ActiveWorkbook
    fName = Application.GetOpenFilename(Finfo, FilterIndex, Title)
    
    If FileName = False Then
        MsgBox "No file was selected."
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Set wb71mm = Workbooks.Open(fName, ReadOnly:=1, UpdateLinks:=False)
    
    Sheets("profiles").Range("A1:N1809").Copy
    wbCurr.Sheets("71mm").Range("a1").PasteSpecial xlPasteValuesAndNumberFormats
    
    wb71mm.Close False 'close new file

    Application.ScreenUpdating = True
    Application.EnableEvents = True


The reason your code probably stopped working was you turned off EnableEvents, then later exited the sub without turning them back on.
 
Upvote 0
Hi,

Thanks for your replys, I have tried SHG's code and get an error shown below and from jbeaucaire code it does not select any file, the file window opens but says no file was selected.

Sub Import()
Dim wks As Worksheet
Dim sFile As String

Application.ScreenUpdating = False
Application.EnableEvents = False

Set wks = ActiveWorkbook.Worksheets("71mm")
sFile = Application.GetOpenFilename(FilterIndex:="Excel files, *.xlsx; *.xlsm", _
Title:="Select 71mm Profile")
If sFile = "False" Then
MsgBox "No file was selected."

Else
With Workbooks.Open(FileName:=sFile, ReadOnly:=True, UpdateLinks:=False)
.Worksheets("Profiles").Range("A1:N1809").Copy
wks.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.Close SaveChanges:=False
End With
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 
Upvote 0
My bad:

Code:
sFile = Application.GetOpenFilename([COLOR=red]FileFilter[/COLOR]:="Excel files, *.xlsx; *.xlsm", _
                                    Title:="Select 71mm Profile")
 
Upvote 0
Lucky guess. You're welcome.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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