Consulting

Results 1 to 13 of 13

Thread: Solved: Combining macros

  1. #1
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location

    Solved: Combining macros

    Hi Experts

    It is possible to run the below macro code for the following worksheet(s):-

    Allocation (Vol), Allocation (Vol) and Alloc (Sc.1), Alloc (Sc.2), Alloc (Sc.3)

    as you can see i have five macro going to five differnet worksheet(s) the macro is slow and i want is just slight speed it up.......

    [VBA]
    Sub concat_sc1()
    Dim rng As Range
    Dim celle As Range
    Dim str1 As String
    Dim str2 As String
    Dim rowe As Long

    rowe = 6
    str1 = "B"
    str2 = "B"
    With Sheets("Alloc (Sc.1)")
    Set rng = Range(.Cells(rowe, str1), .Cells(.Cells.Rows.Count, str2).End(xlUp))
    End With

    For Each celle In rng
    If celle <> "" Then
    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-3],'Deal Selection'!R9C2:R58C2,1,FALSE)"
    celle.Offset(0, 3) = "Vol_" & CStr(celle.Offset(0, 3))
    If str1 <> celle.Offset(0, 3) Then
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    With celle.Offset(0, 3).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    [/VBA]
    EDIT: Added VBA Tags - Tommy

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub concat_sc1()
    Dim sh As Worksheet
    Dim rng As Range
    Dim celle As Range
    Dim str1 As String
    Dim str2 As String
    Dim rowe As Long

    For Each sh In Worksheets(Array("Allocation (Vol)", _
    "Allocation (Vol) and Alloc (Sc.1)", "Alloc (Sc.2)", "Alloc (Sc.3)"))

    rowe = 6
    str1 = "B"
    str2 = "B"
    With sh
    Set rng = Range(.Cells(rowe, str1), .Cells(.Cells.Rows.Count, str2).End(xlUp))
    End With

    For Each celle In rng

    If celle <> "" Then

    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-3]," _
    'Deal Selection'!R9C2:R58C2,1,FALSE)"
    celle.Offset(0, 3) = "Vol_" & CStr(celle.Offset(0, 3))
    If str1 <> celle.Offset(0, 3) Then

    celle.Borders(xlEdgeTop).LineStyle = xlNone
    With celle.Offset(0, 3).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With


    '...
    Next sh
    [/vba]
    Edit Lucas: Line breaks added.....
    ____________________________________________
    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

  3. #3
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    thanks for the feed back XLD..............
    let me test it and get back you

  4. #4
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    ok get a compile without next error

    [vba]
    Sub concat_sc1()
    Dim sh As Worksheet
    Dim rng As Range
    Dim celle As Range
    Dim str1 As String
    Dim str2 As String
    Dim rowe As Long

    For Each sh In Worksheets(Array("Allocation (Vol)", _
    "Allocation (Vol) and Alloc (Sc.1)", "Alloc (Sc.2)", "Alloc (Sc.3)"))

    rowe = 6
    str1 = "B"
    str2 = "B"
    With sh
    Set rng = Range(.Cells(rowe, str1), .Cells(.Cells.Rows.Count, str2).End(xlUp))
    End With

    For Each celle In rng

    If celle <> "" Then

    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-3],'Deal Selection'!R9C2:R58C2,1,FALSE)"
    celle.Offset(0, 3) = "Vol_" & CStr(celle.Offset(0, 3))
    If str1 <> celle.Offset(0, 3) Then

    celle.Borders(xlEdgeTop).LineStyle = xlNone
    With celle.Offset(0, 3).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    str1 = celle.Offset(0, 3)
    End If
    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-2],'Deal Selection'!R9C9:R58C9,1,FALSE)"
    celle.Offset(0, 3) = str1 & " _" & CStr(celle.Offset(0, 3))
    End If
    Next celle
    End Sub
    [/vba]

    EDIT: Added the VBA tags Tommy

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    shouldn't this be next sh[vba]
    Next celle
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    changed it

    Say "invaild Next control varibale reference"

  7. #7
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    add a Next before the End Sub

  8. #8
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Per everone's comments
    [vba]
    Sub concat_sc1()
    Dim sh As Worksheet
    Dim rng As Range
    Dim celle As Range
    Dim str1 As String
    Dim str2 As String
    Dim rowe As Long
    For Each sh In Worksheets(Array("Allocation (Vol)", _
    "Allocation (Vol) and Alloc (Sc.1)", "Alloc (Sc.2)", "Alloc (Sc.3)"))
    rowe = 6
    str1 = "B"
    str2 = "B"
    With sh
    Set rng = Range(.Cells(rowe, str1), .Cells(.Cells.Rows.Count, str2).End(xlUp))
    End With
    For Each celle In rng
    If celle <> "" Then
    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-3],'Deal Selection'!R9C2:R58C2,1,FALSE)"
    celle.Offset(0, 3) = "Vol_" & CStr(celle.Offset(0, 3))
    If str1 <> celle.Offset(0, 3) Then
    celle.Borders(xlEdgeTop).LineStyle = xlNone
    With celle.Offset(0, 3).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    str1 = celle.Offset(0, 3)
    End If
    celle.Offset(0, 3).FormulaR1C1 = "=VLookup(RC[-2],'Deal Selection'!R9C9:R58C9,1,FALSE)"
    celle.Offset(0, 3) = str1 & " _" & CStr(celle.Offset(0, 3))
    End If
    Next celle
    Next sh
    End Sub

    [/vba]

    It helps to post the whole sub to begin with so there is no confusion on anyone's part.

    EDIT: Fixed my own stuff LOL Tommy

  9. #9
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    error with this line:

    For Each sh In Worksheets(Array("Allocation (Vol)", _
    "Allocation (Vol) and Alloc (Sc.1)", "Alloc (Sc.2)", "Alloc (Sc.3)"))

  10. #10
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    If the error is subscript out of range it is because one of the sheets does not exist.

    Otherwise tells us what the error is and we may be able to help more.

  11. #11
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    syntax error
    For Each sh In Worksheets(Array("Allocation (Vol)", _

  12. #12
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I don't get the error, I did test it so try this
    [VBA]For Each sh In Worksheets(Array("Allocation (Vol)", "Allocation (Vol) and Alloc (Sc.1)", "Alloc (Sc.2)", "Alloc (Sc.3)")) [/VBA]

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I am unable to name a sheet 'Allocation (Vol) and Alloc (Sc.1)', it is too long. How did you manage?
    ____________________________________________
    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

Posting Permissions

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