Consulting

Results 1 to 7 of 7

Thread: Reduce code and return empty if empty

  1. #1
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location

    Reduce code and return empty if empty

    hello

    I'd like to shorten this code can anyone in judar?
    When the cells are empty the row must be empty!

    For better understanding see the attachment!
    Attached Files Attached Files

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

    Sub ReturFunction()
    Application.ScreenUpdating = False

    Call Lookups(Range("D20"), Range("E20:J20"))
    Call Lookups(Range("D20"), Range("E21:J21"))
    Call Lookups(Range("D20"), Range("E22:J22"))
    Call Lookups(Range("D20"), Range("E23:J23"))
    Call Lookups(Range("D20"), Range("E24:J24"))
    Call Lookups(Range("D20"), Range("E25:J25"))
    Call Lookups(Range("D20"), Range("E26:J26"))

    Application.ScreenUpdating = True
    End Sub

    Private Sub Lookups(ByVal LookupValue As Variant, ByRef Target As Range)

    With Target

    .Cells(1, 1).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 6, False) 'Item
    .Cells(1, 2).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 7, False) 'Código
    .Cells(1, 3).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 8, False) 'Descrição
    .Cells(1, 4).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 9, False) 'Quantidade
    .Cells(1, 5).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 10, False) 'Unidade
    .Cells(1, 6).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 11, False) 'Preço Unitário
    End With
    End Sub
    [/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 Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    Thank you for responding, but when I got up D23 E23 J23 empty should return empty.

    in other words, if the cells D20 or D21 to D26 corresponding rows are empty should return empty

    Muto thank you!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Option Explicit

    Sub ReturFunction()
    Dim i As Long
    Application.ScreenUpdating = False

    For i = 0 To 6
    If Range("D20").Offset(i) <> "" Then
    Call Lookups(Range("D20").Offset(i), Range("E20:J20").Offset(i))
    Else
    Range("E20:J20").Offset(i).ClearContents
    End If
    Next
    Application.ScreenUpdating = True
    End Sub

    Private Sub Lookups(ByVal LookupValue As Variant, ByRef Target As Range)

    With Target

    .Cells(1, 1).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 6, False) 'Item
    .Cells(1, 2).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 7, False) 'Código
    .Cells(1, 3).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 8, False) 'Descrição
    .Cells(1, 4).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 9, False) 'Quantidade
    .Cells(1, 5).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 10, False) 'Unidade
    .Cells(1, 6).Value = Application.VLookup(LookupValue, Range("RelVenda!$A$1:$R$5000"), 11, False) 'Preço Unitário
    End With
    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'

  5. #5
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    It was perfect you are a great genius! Thank you very much thank you

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    XLD did the hard work!
    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'

  7. #7
    VBAX Tutor
    Joined
    Jan 2011
    Posts
    272
    Location
    So thank you very much to both geniuses

Posting Permissions

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