Consulting

Results 1 to 15 of 15

Thread: Solved: Is there a shortcut for this code?

  1. #1

    Solved: Is there a shortcut for this code?

    I'm just wondering, since all of my coulmns have the same formula, is there a shortcut for this?

    The only criteria that is changing is the index array. If range is at A coulmn then the index array is also at A column.

    Thanks!

    [VBA]Sub CFTMAL()
    Dim lRow As Long

    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
    Range("A7:A" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!A:A,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("B7:B" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!B:B,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("C7:C" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!C:C,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("D7" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!D,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("E7:E" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!E:E,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("F7:F" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!F:F,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("G7:G" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!G:G,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("H7:H" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!H:H,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("I7:I" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!I:I,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("J7:J" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!J:J,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("K7:K" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!K:K,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("L7:L" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!L:L,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("M7:M" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!M:M,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("N7:N" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!N:N,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("O7:O" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!O:O,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("P7:P" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!P:P,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("Q7:Q" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!Q:Q,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("R7:R" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!R:R,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("S7:S" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!S:S,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("T7:T" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!T:T,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("U7:U" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!U:U,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("V7:V" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!V:V,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("W7:W" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!W:W,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("X7:X" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!X:X,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("Y7:Y" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!Y:Y,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    Range("Z7:Z" & lRow).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!Z:Z,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    End sub[/VBA]

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

    Sub CFTMAL()
    Dim lRow As Long

    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
    Range("A7").Resize(lRow, 26).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!A:A,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    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
    Thanks XLD!!!

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Oh well, having done the work, xld beat me to it, but I'll post anyway, it's slightly different; xld's places the formulae in a different range:
    [VBA]Sub blah()
    Dim lRow As Long
    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row
    Range("A7:A" & lRow).Resize(, 26).FormulaR1C1 = "=INDEX('[Sample CFTMAL.xls]Sheet1'!C,MATCH(RC5,'[Sample CFTMAL.xls]Sheet1'!C5,0))"
    End Sub
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Thanks p45cal!

    I'll try it too!!!

  6. #6
    I tried both of the code.

    The code from XLD is okay, but it only populate A7 to D12 and H7 to Z12

    I need to populate all rows and my criteria is at E7 until the last row of E.

    Thanks!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is the last row of E? What does lRow return? ARe you saying it doesn't populate columns E,F & G?

    Maybe

    [vba]

    Sub CFTMAL()
    Dim lRow As Long

    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row
    Range("A7").Resize(lRow + 6, 26).Formula = "=INDEX('[Sample CFTMAL.xls]Sheet1'!A:A,MATCH($E7,'[Sample CFTMAL.xls]Sheet1'!$E:$E,0))"
    End Sub
    [/vba]

    If not, post your workbook.
    ____________________________________________
    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

  8. #8
    It does populate E F and G but, it sometimes stopped in the middle of the file.

    Example, my file is until row 2425, but it only calculate until row 23.

    It doesn't continue till the last row.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post the workbook for us?
    ____________________________________________
    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

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Perhaps a gap in the data in column E?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    I created a dummy workbook with my formula on it.

    Thanks!

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Presuming column E is pre-populated on the Output sheet, the code for your sample workbook is probably:
    [vba]Sub blah()
    Dim lRow As Long
    With Sheets("Output")
    lRow = .Range("E:E").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row
    .Range("A7" & lRow & ",F7:I" & lRow).FormulaR1C1 = "=INDEX(Data!C,MATCH(RC5,Data!C5,0))"
    End With
    End Sub
    [/vba]
    Which for your original question translates to the following, but first make sure that the active sheet is the one that you want the formulae to appear on and that its column E is populated:
    [vba]Sub blah2()
    Dim lRow As Long
    lRow = Range("E:E").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row
    Range("A7" & lRow & ",F7:Z" & lRow).FormulaR1C1 = "=INDEX('[Sample CFTMAL.xls]Sheet1'!C,MATCH(RC5,'[Sample CFTMAL.xls]Sheet1'!C5,0))"
    End Sub
    [/vba]Have I got this right?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    Hi XLD!

    It's perfect! Thank you so much!

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    xld?!

    Quote Originally Posted by genracela
    Hi XLD!

    It's perfect! Thank you so much!
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    Oooppps sorry, pasted the wrong name!

    Thanks p45cal! The codes are perfect!

Posting Permissions

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