Consulting

Results 1 to 3 of 3

Thread: Error 438 in VBA

  1. #1
    VBAX Regular
    Joined
    Nov 2014
    Posts
    10
    Location

    Error 438 in VBA

    Hi All,

    I created a macro to load worksheets from other excel files into a file. However, I get an error 438 saying that the object doesnt support this property etc. Since I am not that familiar with excel VBA I am hoping someone can help me out. Below you find the code. In bold is the part where the bug/error is. Thanks a lot!

    Sub Import_Quoations()

    Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    directory = "D:\VU\Decision Making\Final Model\"
    fileName = Dir(directory & "Carrier*?")

    Do While fileName <> ""

    Workbooks.Open (directory & fileName)

    For Each sheet In Workbooks(fileName).Worksheets
    total = Workbooks("Canon_Final_Model0.1").Worksheets.Count
    Workbooks(fileName).Worksheets(sheet.Name).Copy _
    after:=Workbooks("Canon_Final_Model0.1")(total)

    Next sheet

    Workbooks(fileName).Close

    fileName = Dir()

    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True


    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    donot use reserved words as variable names, ie, sheet.

    when you open a workbook it becomes the active workbook. so i used ActiveWorkbook object.
    it seems you missed "Worksheets" before "total".

    ScreenUpdating and DisplayAlerts automatically sets to TRUE after the code is executed. (Office2007+)

    Sub Import_Quoations()
        Dim directory As String, fileName As String
        Dim ws As Worksheet
        Dim total As Long, calc As Long
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
            calc = .Calculation
            .Calculation = xlCalculationManual
            .AskToUpdateLinks = False
        End With
        
        directory = "D:\VU\Decision Making\Final Model\"
        fileName = Dir(directory & "Carrier*?")
        
        Do While fileName <> ""
            Workbooks.Open (directory & fileName)
            For Each sheet In Workbooks(fileName).Worksheets
                total = Workbooks("Canon_Final_Model0.1").Worksheets.Count
                ActiveWorkbook.Worksheets(ws.Name).Copy _
                    After:=Workbooks("Canon_Final_Model0.1").Worksheets(total)
            Next ws
            ActiveWorkbook.Close False
            fileName = Dir()
        Loop
    
        With Application
            .EnableEvents = True
            .Calculation = calc
            .AskToUpdateLinks = True
        End With
    End Sub
    PS:
    pls use code tags when posting your code here.
    clicking the # button will do it for you.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Please, don't post your problem on different locations on this forum : http://www.vbaexpress.com/forum/show...8-in-VBA-help-)

    Charlize

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •