Consulting

Results 1 to 4 of 4

Thread: Trying to write code in a shorter way

  1. #1
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location

    Trying to write code in a shorter way

    Hi
    I have the following code in a small macro.
    I need help, if possible, to write it in a shorter and more functional way, specially the bold part.
    Many thanks in advance.

    [vba]...
    Dim rng1 As Range, rng2 As Range
    Dim i As Integer, k As Integer

    Set rng1 = Range("I2:J10")
    Set rng2 = Range("27:106")

    For k = 27 To 106
    If Cells(k, 9).Value > 0 Then
    Cells(k, 10).Value = Application.WorksheetFunction.VLookup(Cells(k, 9), rng1, 2, False)
    End If

    If Cells(2, 11) = Cells(k, 1) Or Cells(3, 11) = Cells(k, 1) Or Cells(4, 11) = Cells(k, 1) _
    Or Cells(5, 11) = Cells(k, 1) Or Cells(6, 11) = Cells(k, 1) Or Cells(7, 11) = Cells(k, 1) _
    Or Cells(8, 11) = Cells(k, 1) Or Cells(9, 11) = Cells(k, 1) Or Cells(10, 11) = Cells(k, 1) Then
    Cells(k, 11) = Cells(19, 8)
    Else
    Cells(k, 11) = Application.WorksheetFunction.SumIf(WS4.Range("C15:C10000"), Cells(k, 1), WS4.Range("AO15:AO10000"))
    End If


    Next k
    ...[/vba]
    Ioncila

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

    For k = 27 To 106
    If Cells(k, 9).Value > 0 Then
    Cells(k, 10).Value = Application.WorksheetFunction.VLookup(Cells(k, 9), rng1, 2, False)
    End If

    With Cells(k, 1)

    If Not IsError(Application.Match(.Value2, Cells(2, 11).Resize(9), 0)) Then
    Cells(k, 11).Value2 = Cells(19, 8).Value2
    Else
    Cells(k, 11) = Application.WorksheetFunction.SumIf(ws4.Range("C15:C10000"), .Value2, ws4.Range("AO15:AO10000"))
    End If
    End With
    Next k
    [/vba]
    ____________________________________________
    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 Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    As always, your help is great.
    Thank you very much

    Ioncila

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Please remember to mark your threads Solved.
    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'

Posting Permissions

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