+ Reply to Thread
Results 1 to 15 of 15

Need help combining two codes

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Need help combining two codes

    I have two codes, one that auto selects a file based on a partial title, and the other that copies and pastes.

    Sub Foo()
    Dim vFile As Variant
    Dim wbCopyTo As Workbook
    Dim wsCopyTo As Worksheet
    Dim wbCopyFrom As Workbook
    Dim wsCopyFrom As Worksheet
    Dim Fnd As Range
    Dim Ary As Variant
    Dim i As Long
    
    Set wbCopyTo = ActiveWorkbook
    Set wsCopyTo = ActiveSheet
    Ary = Array("Total", 24, "t-4", 4, "t-3", 5, "t-2", 6, "t-1", 7, "Behr SOP = t0", 8, "t1", 9, "t2", 10, "t3", 11, "t4", 12, "t5", 13, "t6", 14, "t7", 15, "t8", 16, "t9", 17, "t10", 18, "t11", 19, "t12", 20, "t13", 21, "t14", 22, "t15", 23)
    
        '-------------------------------------------------------------
        'Open file with data to be copied
        
    Set Fld = Application.FileDialog(msoFileDialogFolderPicker)
    With Fld
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show = -1 Then
            spath = .SelectedItems(1)
            Filename = Dir(spath & "\*NAME*.xl*")
            Set wbCopyFrom = Workbooks.Open(spath & "\" & Filename)
        End If
    End With
        
        '--------------------------------------------------------------
     'Copy Range
       For i = 0 To UBound(Ary) Step 2
          Set Fnd = wsCopyFrom.Range("5:5").Find(Ary(i), , , xlWhole, , , False, , False)
          If Not Fnd Is Nothing Then
             wsCopyFrom.Range(Fnd.Offset(1), wsCopyFrom.Cells(wsCopyFrom.Rows.Count, Fnd.Column).End(xlUp)).Copy
             wsCopyTo.Cells(7, Ary(i + 1)).PasteSpecial xlPasteValues
          End If
       Next i
       Application.CutCopyMode = False
       wbCopyFrom.Close SaveChanges:=False
    
    End Sub
    After running the program I keep getting this message, "Run-time error 91: Object variable or with block variable not set"

  2. #2
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Need help combining two codes

    Hi, on what line of code does it error?

  3. #3
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    None of the lines shows any indication there is an error, but I would imagine somewhere after the "copy range" line. The file opens automatically, but after it opens this message opens, "Run-time error 91: Object variable or with block variable not set"

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    At a guess it's failing on this line
    Set Fnd = wsCopyFrom.Range("5:5").Find(Ary(i), , , xlWhole, , , False, , False)
    because the variable in blue has no value

  5. #5
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    any recommendations to remedy this?

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    Give it a value
    ie
    Set wsCopyFrom=????

  7. #7
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    ohhh "wsCopyFrom" shouldn't be highlighted. My code is showing only the "Set" is highlighted

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    You have used Set a number of times in your code, which one is getting highlighted?

  9. #9
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    The "set" from the same line: Set Fnd = wsCopyFrom.Range("5:5")

  10. #10
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    Have you set wsCopyFrom before that line?

  11. #11
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    hmmm no but for a previous code I didn't and it worked fine

  12. #12
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    If you haven't assigned a worksheet to the variable, then the code will not work.

  13. #13
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    this is my original code that allows me to open a file and select a file:

    Sub Foo()
    Dim vFile As Variant
    Dim wbCopyTo As Workbook
    Dim wsCopyTo As Worksheet
    Dim wbCopyFrom As Workbook
    Dim wsCopyFrom As Worksheet
    Dim Fnd As Range
    Dim Ary As Variant
    Dim i As Long
    
    Set wbCopyTo = ActiveWorkbook
    Set wsCopyTo = ActiveSheet
    Ary = Array("Total", 24, "t-4", 4, "t-3", 5, "t-2", 6, "t-1", 7, "Behr SOP = t0", 8, "t1", 9, "t2", 10, "t3", 11, "t4", 12, "t5", 13, "t6", 14, "t7", 15, "t8", 16, "t9", 17, "t10", 18, "t11", 19, "t12", 20, "t13", 21, "t14", 22, "t15", 23)
    
        '-------------------------------------------------------------
        'Open file with data to be copied
        
        vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
        "*.xl*", 1, "Select Excel File", "Open", False)
        
        'If Cancel then Exit
        If TypeName(vFile) = "Boolean" Then
            Exit Sub
        Else
        Set wbCopyFrom = Workbooks.Open(vFile)
        Set wsCopyFrom = wbCopyFrom.Worksheets(1)
        End If
        
        '--------------------------------------------------------------
     'Copy Range
       For i = 0 To UBound(Ary) Step 2
          Set Fnd = wsCopyFrom.Range("5:5").Find(Ary(i), , , xlWhole, , , False, , False)
          If Not Fnd Is Nothing Then
             wsCopyFrom.Range(Fnd.Offset(1), wsCopyFrom.Cells(wsCopyFrom.Rows.Count, Fnd.Column).End(xlUp)).Copy
             wsCopyTo.Cells(7, Ary(i + 1)).PasteSpecial xlPasteValues
          End If
       Next i
       Application.CutCopyMode = False
       wbCopyFrom.Close SaveChanges:=False
    
    End Sub
    I am trying to integrate this portion of it:

    set Fld = Application.FileDialog(msoFileDialogFolderPicker)
    With Fld
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show = -1 Then
            spath = .SelectedItems(1)
            Filename = Dir(spath & "\*BitOfFileName*.xl*")
            Set WBCopyFrom = Workbooks.Open(spath & "\" & Filename)
        End If
    End With
    Together is what my original post was about, how would one go about combing the two because I am completely lost.

  14. #14
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Need help combining two codes

    Just add this line from
    Set wsCopyFrom = wbCopyFrom.Worksheets(1)
    your original code to your new code to the new code after this section
            Set wbCopyFrom = Workbooks.Open(spath & "\" & Filename)
        End If
    End With

  15. #15
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Re: Need help combining two codes

    omg for some reason one of the lines were missing that's why it wasn't working, thank you so much!! is there one more thing you could help me with? I have to do the same thing with 3 workbooks (in the same file), is there anyway I can make workbook one auto select 1 file and copy and paste, then workbook 2 auto select a different file then copy and paste, etc.. with one click of a button?

+ 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. Combining two codes into one
    By Aqmas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2017, 03:46 PM
  2. Call Two Codes from ComboBox
    By zplugger in forum Excel General
    Replies: 2
    Last Post: 01-24-2015, 03:02 PM
  3. Combining vba codes makes the previous codes broken !
    By MDPLUS in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-26-2013, 10:00 AM
  4. [SOLVED] Combining 2 codes?
    By bjcowen9000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-19-2013, 02:48 PM
  5. [SOLVED] Help! combining 2 VBA codes
    By iamreese in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-29-2012, 07:24 AM
  6. combining codes
    By tweety127 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-18-2008, 06:48 PM
  7. [SOLVED] combining 2 codes
    By steve in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2006, 01:15 PM

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