+ Reply to Thread
Results 1 to 6 of 6

VBA Lookup errors

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2012
    Location
    Batlimore, MD
    MS-Off Ver
    Excel 2007
    Posts
    8

    Angry VBA Lookup errors

    I am writing a macro that needs to look up data in a different workbook in the first sheet with a particular name. I need to copy all the data from the sheet into a select worksheet in a select workbook.

    I have the following code so far.

    Sub CopyCells(smallname As String, sheetname As String, LocoName As String, cells As Range, Bookinto As String, Sheetinto As String)
        Workbooks.Open Filename:=LocoName
        Workbooks(smallname).Activate
        Worksheets(sheetname).Activate
        Range("A1:Z1000").Select
        ActiveSheet.Copy
    
        Workbooks(Bookinto).Activate 'activate book
        Worksheets(Sheetinto).Activate
        Range("A1").Select
        ActiveSheet.Paste
        Workbooks(smallname).Close
    End Sub
    It works fine with debugging however when I try to copy the information from the files it does not copy but makes a new version of it called Book3. then it closes the original. this is fine but not really useful...
    I am sure i am making a silly mistake can anyone find it?
    Last edited by Leith Ross; 06-18-2012 at 11:05 AM. Reason: Added Code Tags

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: VBA Lookup errors

    What do the different parameters represent? It looks like you are referring to the active workbook/worksheet and the workbook/worksheet you are opening, there might be a better way to reference them.

    Something like:

    dim wbkLoco as workbook
    
    set wbkloco = application.workbooks.open filename:=loconame
    wbkloco.worksheets(SheetInto).Range("A1:F1000").value = workbooks(smallname).worksheets(Sheetname).range("A1:F1000").value
    workbooks(smallname).close
    
    set wbkloco = nothing

  3. #3
    Registered User
    Join Date
    06-18-2012
    Location
    Batlimore, MD
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Lookup errors

    Quote Originally Posted by wallyeye View Post
    What do the different parameters represent? It looks like you are referring to the active workbook/worksheet and the workbook/worksheet you are opening, there might be a better way to reference them.

    Something like:

    dim wbkLoco as workbook
    
    set wbkloco = application.workbooks.open filename:=loconame
    wbkloco.worksheets(SheetInto).Range("A1:F1000").value = workbooks(smallname).worksheets(Sheetname).range("A1:F1000").value
    workbooks(smallname).close
    
    set wbkloco = nothing
    Hmm this does not work. The line
    set wbkloco = application.workbooks.open filename:=loconame
    returns an error that end of line is exspected, I think it is because I can not set a workbook when I am opening it?

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

    Re: VBA Lookup errors

    Hello dontich,

    Welcome tot he Forum!

    Try this revision of your macro and let me know the results.
    
    Sub CopyCells(smallname As String, sheetname As String, LocoName As String, cells As Range, Bookinto As String, Sheetinto As String)
    
        Dim DstWkb As Workbook
        Dim DstWks As Worksheet
        
        Dim SrcWkb As Workbook
        Dim SrcWks As Worksheet
    
            Set SrcWkb = Workbooks(smallname)
            Set SrcWkb = SrcWkb.Worksheets(sheetname)
            
            Set DstWkb = Workbooks(Bookinto)
            Set DstWks = DstWkb.Worksheets(Sheetinto)
            
            SrcWks.Range("A1:Z1000").Copy Destination:=DstWks.Range("A1")
            
            SrcWkb.Close
            
    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!)

  5. #5
    Registered User
    Join Date
    06-18-2012
    Location
    Batlimore, MD
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Lookup errors

    Quote Originally Posted by Leith Ross View Post
    Hello dontich,

    Welcome tot he Forum!

    Try this revision of your macro and let me know the results.
    
    Sub CopyCells(smallname As String, sheetname As String, LocoName As String, cells As Range, Bookinto As String, Sheetinto As String)
    
        Dim DstWkb As Workbook
        Dim DstWks As Worksheet
        
        Dim SrcWkb As Workbook
        Dim SrcWks As Worksheet
    
            Set SrcWkb = Workbooks(smallname)
            Set SrcWkb = SrcWkb.Worksheets(sheetname)
            
            Set DstWkb = Workbooks(Bookinto)
            Set DstWks = DstWkb.Worksheets(Sheetinto)
            
            SrcWks.Range("A1:Z1000").Copy Destination:=DstWks.Range("A1")
            
            SrcWkb.Close
            
    End Sub
    Hmm this would work if the other file is open beforehand I am having trouble adding that into your code

  6. #6
    Registered User
    Join Date
    06-18-2012
    Location
    Batlimore, MD
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Lookup errors

    Also, Thanks alot guys!,, i know alot of excel formula stuff but am not amazing at VBA yet so I will try to post on the forums section and help other people out there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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