Consulting

Results 1 to 11 of 11

Thread: Solved: I want to make code smaller and more optomized

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: I want to make code smaller and more optomized

    I have code that I use multiple times in my program and they all work perfectly but I want to know if there is shorter ways or more optimized ways to do the same thing.

    First one: I create a border completely around a cell.

    [VBA]With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With[/VBA]

    Second One: I use these to add Pivot Tables many times in my program

    [VBA]pt.PivotFields("Customer").Subtotals = Array(False, False, False, False, False, False, _
    False, False, False, False, False, False)[/VBA]

    Is it possible to make either statement shorter?

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hello,

    You can shorten your borders to something like...

    [vba] Selection.BorderAround xlContinuous, xlThin, 1
    Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    Selection.Borders(xlInsideHorizontal).Weight = xlThin
    Selection.Borders(xlInsideHorizontal).ColorIndex = 1
    Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
    Selection.Borders(xlInsideVertical).Weight = xlThin
    Selection.Borders(xlInsideVertical).ColorIndex = 1[/vba]

    Can't help you with the other, it looks fairly short to me.

    HTH

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    the second one is fairly short but I use it like 100 different times - so if I can shorten it to one line it would save me 100 lines of code.

    Thank you

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Just put it all on one line, don't know how much shorter you can make it...

    [vba]pt.PivotFields("Customer").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False) [/vba]

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    thank you

  6. #6
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    If the line of code is repeated, can you make it a subroutine, and pass the string "Field" through to the sub..? Seems that would be a lot easier than having 100 identical lines..

    [vba]

    Sub addfieldstoPivot( strField as string)

    pt.PivotFields(strField).Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)


    End Sub

    [/vba]

    When you need to add a field

    Just:
    [VBA]
    addfieldstoPivot "Customers"
    [/vba]

    For example
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  7. #7
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    For the 1st part, try this variation...
    [VBA]Option Explicit

    Sub FormatSelection()
    With Selection
    .BorderAround xlContinuous, xlThin, 1
    With .Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    With .Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
    End With
    End With
    End Sub[/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    See the "Styles Collection" in VBA help for a speedier, more concise way of using the same formats for multiple ranges. In essence, you should create a style once and then apply it to your various ranges as need to.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Good call Tom!

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    The Only problem is it isn't exact. It is always a different field name. that is the problem

  11. #11
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Then you should be able to use Pete's suggestion from post #6 . Just call an input box to name the field
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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