Consulting

Results 1 to 17 of 17

Thread: Solved: Combining 2 macros

  1. #1

    Solved: Combining 2 macros

    Good morning / afternoon / evening,

    I am trying to combine a couple of perfectly functioning macros kindly supplied by some VBA Express members.

    The problem is that when I attempt to combine the subs, I reach an error. I have tried a few things unsuccessfully, so I will just post my first attempt.

    I hope someone can help. I don't think it is too difficult, but what would I know.

    [vba]Sub CopyWorksheets()
    Dim thisWB As Workbook
    Dim newWB As Workbook
    Dim numSheets As Long
    Dim TargetSheet As Worksheet, SourceSheet As Worksheet

    Set thisWB = ActiveWorkbook
    numSheets = thisWB.Sheets.Count
    Application.SheetsInNewWorkbook = numSheets
    Set newWB = Workbooks.Add

    numSheets = 1

    For Each SourceSheet In thisWB.Sheets
    Set TargetSheet = newWB.Sheets(numSheets)
    Call CopyData(thisWB, newWB, SourceSheet.Name)
    numSheets = numSheets + 1
    Next SourceSheet

    'start of other sub

    Dim mpDate As Long
    With ActiveSheet

    'receiving error here
    mpDate = .Evaluate("=MIN(IF((MOD(ROW(A1:A230),3)=0)*(A2:A230<>""""),ROW(A1:A230)))")
    If mpDate <> 0 Then
    newWB.SaveAs "C:\Working Folder" & Format(.Range("A1:A230").Cells(mpDate, 1), "yyyy-mm-dd")
    End If
    End With

    'end of other sub

    Set newWB = Nothing
    Set thisWB = Nothing
    Set TargetSheet = Nothing
    Set SourceSheet = Nothing
    End Sub[/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Change A2:A230 to A1:A230 in the offending row. The ranges need to be the same size.
    BTW, it's often better to call other routines as in
    Call CopyData(thisWB, newWB, SourceSheet.Name)
    It keeps code from getting overcomplicated and so easier to maintain.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Whoops. That was a stupid mistake, however I am still receiving an error at the same point.

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    It looks like you are trying to Evaluate an array function.

  5. #5
    I gather that is not possible. Dammit.

  6. #6
    Does anyone know if this is possible?

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a workbook with sample data?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Certainly. I have made it as simple as possible.

    Btw, I tried calling the sub, instead of pasting it the middle of another one, but to no avail. I thought it might be easier to read for this purpose by leaving it in there.

    Thanks for your help.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    On your sample, I'm getting a value of 3 for mpDate (Excel 2007; maybe its a version problem?)
    Try writing the formula to a cell and reading the result if the Evaluate won't work.
    [VBA]
    Range("I1").FormulaArray = _
    "=MIN(IF((MOD(ROW(RC[-8]:R[229]C[-8]),3)=0)*(RC[-8]:R[229]C[-8]<>""""""""),ROW(RC[-8]:R[229]C[-8])))"
    mpDate = Range("I1")
    MsgBox mpDate

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Hmm. Ok. I am indeed receiving a 3 there, but what does that mean? I am lost.

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You substitute my code for your erroring code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by jacksonworld
    I gather that is not possible. Dammit.
    Yes it is.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Sorry to be a pain, but I am failing at substituting your code for the old code. Whatever I seem to do, I receive an error somewhere or other.

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Sub CopyWorksheets()
    Dim thisWB As Workbook
    Dim newWB As Workbook
    Dim numSheets As Long
    Dim TargetSheet As Worksheet, SourceSheet As Worksheet
    Set thisWB = ActiveWorkbook
    numSheets = thisWB.Sheets.Count
    Application.SheetsInNewWorkbook = numSheets
    Set newWB = Workbooks.Add
    numSheets = 1
    For Each SourceSheet In thisWB.Sheets
    Set TargetSheet = newWB.Sheets(numSheets)
    Call CopyData(thisWB, newWB, SourceSheet.Name)
    numSheets = numSheets + 1
    Next SourceSheet
    'start of other sub
    Dim mpDate As Long
    With ActiveSheet
    'receiving error here
    Range("I1").FormulaArray = _
    "=MIN(IF((MOD(ROW(RC[-8]:R[229]C[-8]),3)=0)*(RC[-8]:R[229]C[-8]<>""""""""),ROW(RC[-8]:R[229]C[-8])))"
    mpDate = Range("I1")
    'mpDate = .Evaluate("=MIN(IF((MOD(ROW(A1:A230),3)=0)*(A1:A230<>""""),ROW(A1:A230)))")
    If mpDate <> 0 Then
    newWB.SaveAs "C:\Working Folder" & Format(.Range("A1:A230").Cells(mpDate, 1), "yyyy-mm-dd")
    End If
    End With
    'end of other sub
    Set newWB = Nothing
    Set thisWB = Nothing
    Set TargetSheet = Nothing
    Set SourceSheet = Nothing
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    That's exactly what I tried but I receive an error at [VBA]newWB.SaveAs "C:\Working Folder" & Format(.Range("A1:A230").Cells(mpDate, 1), "yyyy-mm-dd") [/VBA]

    It's weird.

    Thanks for continuing effort, by the way.

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you checked the string value of that line? It doesn't look like a proper file path to me.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    Aagh, I forgot the backslash after the folder.

    Thank you so much. It works perfectly.


Posting Permissions

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