+ Reply to Thread
Results 1 to 10 of 10

Error Message Visual Basic 400 - help!

Hybrid View

EarlD Error Message Visual Basic... 06-03-2009, 05:44 PM
Keyur Re: Error Message Visual... 06-03-2009, 09:09 PM
Leith Ross Re: Error Message Visual... 06-03-2009, 10:04 PM
EarlD Re: Error Message Visual... 06-04-2009, 12:55 AM
EarlD Re: Error Message Visual... 06-04-2009, 01:04 AM
EarlD Re: Error Message Visual... 06-04-2009, 01:57 AM
Leith Ross Re: Error Message Visual... 06-04-2009, 02:21 AM
EarlD Re: Error Message Visual... 06-04-2009, 02:38 AM
Leith Ross Re: Error Message Visual... 06-04-2009, 02:53 AM
EarlD Re: Error Message Visual... 06-04-2009, 02:57 AM
  1. #1
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Error Message Visual Basic 400 - help!

    I have attached sample files of what I am running. Please forgive me if my lingo is off but what I am trying to accomplish is I have an Excel template of a packing slip called Factory Master Packing Slip and it has an Import button. Once the import button is press it brings up a Import File box. You then select the file to import.. and this case it will be the Shipping Slip 2 file attached. It should take the data from form 1 to form 2. I have run this macro on other forms with luck but not with this one. Can the pros take a look at this and tell me what i am doing wrong. Maybe a setting?

    Should state that I am getting the "400" error when running...
    Attached Files Attached Files
    Last edited by EarlD; 06-04-2009 at 02:58 AM.

  2. #2
    Registered User
    Join Date
    03-31-2004
    Location
    Toronto, Canada
    MS-Off Ver
    2003/2007
    Posts
    36

    Re: Error Message Visual Basic 400 - help!

    Hi,
    the problem is when you select your range before copying. I am not too sure why since I never do it this way.
    try replacing the following part of your code
            Workbooks.Open fImport
                Sheets(1).Select
                Range("A4").Select
                Selection.Copy
            Windows(fnTemplate).Activate
                Sheets(1).Select
                Range("A12").Select
                ActiveSheet.Paste
            'Order Date
            Windows(fnImport).Activate
                Range("B4").Select
                Application.CutCopyMode = False
                Selection.Copy
    ..........
            Windows(fnImport).Activate
            ActiveWindow.Close savechanges:=False
            Windows(fnTemplate).Activate
    instead of the entire copy paste part of your code try something like below

            Workbooks.Open fImport
            Set xlimp = Application.Workbooks(fnImport)
            Set xltemp = Application.Workbooks(fnTemplate)
            xltemp.Sheets(1).Range("A12").Value = xlimp.Sheets(1).Range("A4")
            xltemp.Sheets(1).Range("E12").Value = xlimp.Sheets(1).Range("B4")
            xltemp.Sheets(1).Range("G12").Value = xlimp.Sheets(1).Range("C4")
    '.....
    'Do the same for the rest of the items you need to copy
    
           xlimp.close False  'close the file without saving
    hope it works for you!

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Error Message Visual Basic 400 - help!

    Hello EarlD,

    I partially rewrote your macro and tested it. This will copy the information over from the imported workbook to the template.
    Sub FileToImport()
        Dim chkOKFileName As Boolean
        Dim fImport As Variant
        Dim fnImport As String
        Dim fnTemplate As String
        Dim fnNewName As String
        Dim WksImport As Worksheet
        Dim WksMain As Worksheet
        
        Set WksMain = ThisWorkbook.Worksheets("Sheet1")
        
        fImport = Application.GetOpenFilename("Excel-files,*.xls", 1, "Select File To Import", , False)
        fnImport = GetFileName(CStr(fImport))
        fnTemplate = "Factory Master Packing List.xls"
    
        If TypeName(fImport) = "Boolean" Then
            'the user didn't select a file
            MsgBox ("File was NOT selected for import!")
            Exit Sub
        End If
        
            'Assign Import Worksheet to an Object variable
             Workbooks.Open fImport
             Set WksImport = ActiveWorkbook.Sheets(1)
             
            'Order ID Number
             WksMain.Range("A12") = WksImport.Range("A4")
            
            'Order Date
             WksMain.Range("E12") = WksImport.Range("B4")
            
            'Delivery Service
             WksMain.Range("G12") = WksImport.Range("C4")
            
            'Store Location
             WksMain.Range("A14") = WksImport.Range("B7")
             
            'Store
             WksMain.Range("A15") = WksImport.Range("B8")
             
            'Adress 1
             WksMain.Range("A16") = WksImport.Range("B10")
                
            'Address 2
             WksMain.Range("A17") = WksImport.Range("B11")
            
            'Address 3
             WksMain.Range("A18") = WksImport.Range("B12")
                
            'Address 4
             WksMain.Range("A19") = WksImport.Range("B13")
                
            'Address 5
             WksMain.Range("A20") = WksImport.Range("B14")
            
            'Address 6
             WksMain.Range("A21") = WksImport.Range("B15")
            
           'Close the Import Workbook - Don't Save Changes
            WksImport.Parent.Close savechanges:=False
            
            WksMain.Parent.Activate
            Range("C5").Select
            
           'Rmeove All Macros
            Set VBComps = ActiveWorkbook.VBProject.VBComponents
    
            For Each VBComp In VBComps
               Select Case VBComp.Type
                  Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
                     VBComps.Remove VBComp
                  Case Else
                     With VBComp.CodeModule
                        .DeleteLines 1, .CountOfLines
                     End With
               End Select
            Next VBComp
            
           'Delete "Import" Sheet and prompt for new file name
            Call DeleteSheet("IMPORT")
            chkOKFileName = Application.Dialogs(xlDialogSaveAs).Show("Enter new file name here")
            
           'Save the new workbook
            If chkOKFileName = True Then
                fnNewName = ActiveWorkbook.Name
            Else
                MsgBox ("File was NOT saved!")
            End If
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Error Message Visual Basic 400 - help!

    Leith,

    Here are the errors I get when using your code. Please see attachment. I really appreciate all your help.

    Earl
    Attached Files Attached Files
    Last edited by EarlD; 06-04-2009 at 01:59 AM.

  5. #5
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Error Message Visual Basic 400 - help!

    Keyur,

    I get the same error on your solution. It must be something I just am over looking. I attached the updated file in case you see something I don't. Thank You
    Attached Files Attached Files
    Last edited by EarlD; 06-04-2009 at 01:45 AM.

  6. #6
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Error Message Visual Basic 400 - help!

    Here is my Code..

    Sub Auto_Open()
        Call FileToImport
    End Sub
    
    Sub btnImport_Click()
        Call FileToImport
    End Sub
    
    Sub FileToImport()
        Dim chkOKFileName As Boolean
        Dim fImport As Variant
        Dim fnImport As String
        Dim fnTemplate As String
        Dim fnNewName As String
        Dim WksImport As Worksheet
        Dim WksMain As Worksheet
        
        Set WksMain = ThisWorkbook.Worksheets("Sheet1")
        
        fImport = Application.GetOpenFilename("Excel-files,*.xls", 1, "Select File To Import", , False)
        fnImport = GetFileName(CStr(fImport))
        fnTemplate = "Factory Master Packing List.xls"
    
        If TypeName(fImport) = "Boolean" Then
            'the user didn't select a file
            MsgBox ("File was NOT selected for import!")
            Exit Sub
        End If
        
            'Assign Import Worksheet to an Object variable
             Workbooks.Open fImport
             Set WksImport = ActiveWorkbook.Sheets(1)
             
            'Order ID Number
             WksMain.Range("A12") = WksImport.Range("A4")
            
            'Order Date
             WksMain.Range("E12") = WksImport.Range("B4")
            
            'Delivery Service
             WksMain.Range("G12") = WksImport.Range("C4")
            
            'Store Location
             WksMain.Range("A14") = WksImport.Range("B7")
             
            'Store
             WksMain.Range("A15") = WksImport.Range("B8")
             
            'Adress 1
             WksMain.Range("A16") = WksImport.Range("B10")
                
            'Address 2
             WksMain.Range("A17") = WksImport.Range("B11")
            
            'Address 3
             WksMain.Range("A18") = WksImport.Range("B12")
                
            'Address 4
             WksMain.Range("A19") = WksImport.Range("B13")
                
            'Address 5
             WksMain.Range("A20") = WksImport.Range("B14")
            
            'Address 6
             WksMain.Range("A21") = WksImport.Range("B15")
            
           'Close the Import Workbook - Don't Save Changes
            WksImport.Parent.Close savechanges:=False
            
            WksMain.Parent.Activate
            Range("C5").Select
            
           'Rmeove All Macros
            Set VBComps = ActiveWorkbook.VBProject.VBComponents
    
            For Each VBComp In VBComps
               Select Case VBComp.Type
                  Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
                     VBComps.Remove VBComp
                  Case Else
                     With VBComp.CodeModule
                        .DeleteLines 1, .CountOfLines
                     End With
               End Select
            Next VBComp
            
           'Delete "Import" Sheet and prompt for new file name
            Call DeleteSheet("IMPORT")
            chkOKFileName = Application.Dialogs(xlDialogSaveAs).Show("Enter new file name here")
            
           'Save the new workbook
            If chkOKFileName = True Then
                fnNewName = ActiveWorkbook.Name
            Else
                MsgBox ("File was NOT saved!")
            End If
        
    End Sub

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Error Message Visual Basic 400 - help!

    Hello EarlD,

    Here are you original files with the macro I added.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Error Message Visual Basic 400 - help!

    Leith,

    I ran the macro and the first thing I get is this (attached) error. I click ok and it goes to my form and everything is populated correctly but it completely deletes the code from VB. So I have to close and reopen. ??? Please see attachment. I feel it is sooo close...
    Attached Images Attached Images

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Error Message Visual Basic 400 - help!

    Hello EarlD,

    If you read the comments I inserted into your code, you will find the section that deletes all the macro code from the workbook. It appears you weren't aware that this section of code did that. You can simply delete that section and all the macro code will remain intact. When you copy code, if there is part of it you don't understand then ask someone to explain it.

    Delete these line from the macro
           'Rmeove All Macros
            Set VBComps = ActiveWorkbook.VBProject.VBComponents
    
            For Each VBComp In VBComps
               Select Case VBComp.Type
                  Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
                     VBComps.Remove VBComp
                  Case Else
                     With VBComp.CodeModule
                        .DeleteLines 1, .CountOfLines
                     End With
               End Select
            Next VBComp

  10. #10
    Registered User
    Join Date
    04-15-2009
    Location
    So. Cal
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Error Message Visual Basic 400 - help!

    Quote Originally Posted by Leith Ross View Post
    Hello EarlD,

    If you read the comments I inserted into your code, you will find the section that deletes all the macro code from the workbook. It appears you weren't aware that this section of code did that. You can simply delete that section and all the macro code will remain intact. When you copy code, if there is part of it you don't understand then ask someone to explain it.

    Delete these line from the macro
           'Rmeove All Macros
            Set VBComps = ActiveWorkbook.VBProject.VBComponents
    
            For Each VBComp In VBComps
               Select Case VBComp.Type
                  Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
                     VBComps.Remove VBComp
                  Case Else
                     With VBComp.CodeModule
                        .DeleteLines 1, .CountOfLines
                     End With
               End Select
            Next VBComp
    Leith,

    You are the MAN!! You are right! Its getting late and I didn't even see that down there. It works absolutely perfect now!! Thank You Leith and Thank You excelforum.com!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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