+ Reply to Thread
Results 1 to 9 of 9

RunTime Error 438

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    RunTime Error 438

    Hi Guys,

    I need your help, on VBA code below. My aim is to be able to copy a range from one workbook to another and I get the Run Time Error 438 on the line colored red.

    Can this code be corrected or is there an easier way to get this done?

    Thanks.

    Sub CopytoAnotherWorkbook()
    
       Dim wbTarget            As Workbook 'workbook where the data is to be pasted
       Dim wbThis              As Workbook 'workbook from where the data is to copied
       Dim strName             As String   'name of the source sheet/ target workbook
        
       Set wbThis = ActiveWorkbook
       strName = ActiveSheet.name
       Set wbTarget = Workbooks.Open("C:\Users\asdf\Desktop\" & strName & ".xlsx")
       wbTarget.Activate
       wbTarget.Range("A1").Select
       wbTarget.Range("A1:C10").ClearContents
       wbThis.Activate
     Application.CutCopyMode = False
      wbThis.Range("A1:C10").Copy
      wbTarget.Range("A1").PasteSpecial
     Application.CutCopyMode = False
     wbTarget.Save
    wbTarget.Close
    wbThis.Activate
    Set wbTarget = Nothing
       Set wbThis = Nothing
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: RunTime Error 438

    wbTarget.Range("A1").Select
    Should have a worksheet name.
    wbTarget is a workbook, so need to add the sheet within in the workbook
    Like
    wbTarget.sheets("Sheet1").Range("A1").Select

  3. #3
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: RunTime Error 438

    Thanks AB33,

    It worked perfectly. I have one more question though. What if I want to modify this code in such a way that when it runs, I would have the open dialog box to select the excel workbook I want to paste my data into. This would help in the sense that I would want to copy a range to separate workbooks at different times.

    Thanks again.
    Last edited by akynyemi; 02-17-2014 at 03:38 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: RunTime Error 438

    Why not replace this code,
    wbTarget.Activate
       wbTarget.Range("A1").Select
       wbTarget.Range("A1:C10").ClearContents
       wbThis.Activate
    with this?
    wbTarget.ActiveSheet.Range("A1:C10").ClearContents
    And this,
    wbThis.Activate
     Application.CutCopyMode = False
      wbThis.Range("A1:C10").Copy
      wbTarget.Range("A1").PasteSpecial
    with this.
    wbThis.ActiveSheet.Range("A1:C10").Copy wbTarget.ActiveSheet.Range("A1")
    Application.CutCopyMode = False
    As for a dialog for selecting the file, you could use Application.GetOpenFilename.
    Sub CopytoAnotherWorkbook()
    
    Dim wbTarget As Workbook    'workbook where the data is to be pasted
    Dim wbThis As Workbook             'workbook from where the data is to copied
    Dim strName As String             'name of the source sheet/ target workbook
    Dim strFileName As String
    
        Set wbThis = ActiveWorkbook
        strName = ActiveSheet.Name
    
        ChDir "C:\Test\" ' change as needed
    
        strFileName = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", , "Choose destination file", , False)
    
        Set wbTarget = Workbooks.Open(strFileName)
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: RunTime Error 438

    Thanks Norie for taking time out, it worked perfectly.

    Regards.

  6. #6
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: RunTime Error 438

    Hi Norie,

    I modified my code further in such a way that if no file is selected, it displays a message and then exits. But I have another Runtime Error 13 "Type Mismatch on "If strFileName = False Then"

    My full code is below.


    Thanks.
    Sub ExportToOtherLocations()
       Application.ScreenUpdating = False
    
       
       Dim wbTarget            As Workbook 'workbook where the data is to be pasted
       Dim wbThis              As Workbook 'workbook from where the data is to copied
       Dim strName             As String   'name of the source sheet/ target workbook
       Dim SD                  As String
       Dim strFileName         As String
       
       Set wbThis = ActiveWorkbook
       strName = ActiveSheet.name
        Set wbThis = ActiveWorkbook
        strName = ActiveSheet.name
    
        ChDir "C:\Users\" ' change as needed
    
        strFileName = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", , "Please Select the File for the Location You want to Export Data to", , False)
    
    If strFileName = False Then
    MsgBox "No file specified. You must choose a Location File to Export To", vbExclamation, "Duh!!!"
    Exit Sub
    Else
    
    Set wbTarget = Workbooks.Open(strFileName)
    
       wbTarget.Activate
       wbTarget.Sheets("Test").Select
       Range("A1").Select
       SD = InputBox("Pls enter", "Duh")
       If SD = "" Then
       MsgBox "This Value Cannot be NULL", Title, "Duh"
       wbTarget.Close
       Exit Sub
       
       End If
       If SD = "Fresh" Then
       wbTarget.Sheets("Test").Range("A2:F100000").ClearContents
       wbThis.Activate
       Application.CutCopyMode = False
       
        wbThis.Sheets("Stock").Range("A7:F7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        
       wbTarget.Sheets("Test").Range("A2").PasteSpecial Paste:=xlPasteValues
       Application.CutCopyMode = False
       Else
       wbThis.Activate
       Application.CutCopyMode = False
       
        wbThis.Sheets("Stock").Range("A7:F7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        
       wbTarget.Activate
       wbTarget.Sheets("Test").Select
       Range("A" & Rows.Count).End(xlUp).Offset(1).Select
       Selection.PasteSpecial Paste:=xlPasteValues
       Application.CutCopyMode = False
       wbTarget.Sheets("Test").Range("A1").Select
       End If
       wbTarget.Save
       wbTarget.Close
       wbThis.Activate
       wbThis.Sheets("Stock").Range("A7").Select
       
       Set wbTarget = Nothing
       Set wbThis = Nothing
       End If
    End Sub

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: RunTime Error 438

    strFileName is string therefore you must compare to "False" not False. or preferably use a Variant rather than String.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: RunTime Error 438

    Izandol is right you need to use "False".

    By the way, all that activating/selecting isn't needed.

  9. #9
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: RunTime Error 438

    Thanks Guys.

    Regards.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Error: Runtime Error 1004: AutoFilter method of Range class failed
    By jl22stac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 07:27 PM
  2. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  3. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  4. runtime error 3265 error in a VBA Query to pull info from ERP Database
    By NBVC in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-12-2010, 03:59 PM
  5. Excel xmlHTTP object error message - system/runtime error
    By Porky2007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2007, 09:36 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1