Consulting

Results 1 to 18 of 18

Thread: Solved: Sorten Recorded Macro

  1. #1

    Solved: Sorten Recorded Macro

    Hi
    I have recorded the macro below and removed Activecell and Select. I have about 50 line upto Range(D50) is there a another way it can be done?

    [VBA]
    sub cOUNT()
    Sheets("Test").select
    Range("D5").FormulaR1C1 = "='Data'!R[13]C[14]"
    Range("D6").FormulaR1C1 = "='Data'!R[24]C[14]"
    Range("D7").FormulaR1C1 = "='Data'!R[26]C[14]"
    Range("D8").FormulaR1C1 = "='Data'!R[31]C[14]"
    Range("D9").FormulaR1C1 = "='Data'!R[33]C[14]"
    Range("D10").FormulaR1C1 = "='Data'!R[47]C[14]"

    End Sub[/VBA]

    Thanking for your time

    Nurofen

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not unless there is some pattern
    ____________________________________________
    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
    Hi Xld,

    No there is no pattern.

    No problem

    Thank you for your time

    Nurofen

  4. #4
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    try

    [vba]
    sub cOUNT()
    With Sheets("Test").Range("D5").Resize(6)
    .FormulaR1C1 = [{"='Data'!R[13]C[14]";"='Data'!R[24]C[14]";"='Data'!R[26]C[14]";"='Data'!R[31]C[14]";"='Data'!R[33]C[14]";"='Data'!R[47]C[14]"}]
    End With

    End Sub[/vba]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    By the time he adds in 50, this will become a maintenance nightmare.
    ____________________________________________
    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

  6. #6
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Quote Originally Posted by xld
    By the time he adds in 50, this will become a maintenance nightmare.
    Who cares?

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by jindon
    Who cares?
    With a stupid, arrogant attitude like that, I think you are showing why you come up with a less than helpful solution.
    ____________________________________________
    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
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    OP is not asking about MAINTAINANCE, Shortnen the code, so I replied.

    Have I done something wrong???

  9. #9
    Hi jindon,

    I did ask to shorten, but as pointed out by Xld there is no pattern so using the recorded code and your code would make no difference.

    Thank you for your input tho

    Nurofen

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could use arrays to store the corresponding numbers
    [vba]Sub Nurofen()
    Dim arr
    arr1 = Array(5, 6, 7, 8, 9, 10)
    arr2 = Array(13, 24, 26, 31, 33, 47)

    For i = 0 To UBound(arr1)
    Sheets("Test").Select
    Range("D" & arr1(i)).FormulaR1C1 = "='Data'!R[" & arr2(i) & "]C[14]"
    Next

    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'

  11. #11
    Hi mdmackillop,

    I'm not sure how that will work only cause I don't understand. I will post a example tomorrow. If you don't mind to show me what you mean please.

    Thank you for you time



    Nurofen

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    You could use arrays to store the corresponding numbers
    [vba]Sub Nurofen()
    Dim arr
    arr1 = Array(5, 6, 7, 8, 9, 10)
    arr2 = Array(13, 24, 26, 31, 33, 47)

    For i = 0 To UBound(arr1)
    Sheets("Test").Select
    Range("D" & arr1(i)).FormulaR1C1 = "='Data'!R[" & arr2(i) & "]C[14]"
    Next

    End Sub

    [/vba]
    I considered that but dismissed it because it offered no significant advantage, and would also be more dificult to maibtain that what he already had (not as difficult as jindon's offering, but still not as simple as what he had).
    ____________________________________________
    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

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If column D entries are to be sequential then only one array is required.
    [vba]
    Sub Nurofen()
    Dim arr1
    arr1 = Array(13, 24, 26, 31, 33, 47)
    For i = 0 To UBound(arr1)
    Sheets("Test").Select
    Range("D" & i + 5).FormulaR1C1 = "='Data'!R[" & arr1(i) & "]C[14]"
    Next
    End Sub

    [/vba]

    Another consideration; are the cells between those selected on Data blank? ie, are you looking for the items in the same order, but with spaces removed? This looks for Constants and may need to be changed.

    [vba]
    Sub Nurofen2()
    Dim rng as range, r as range, i as long
    Set rng = Sheets("Data").Columns("R:R").SpecialCells(xlCellTypeConstants)
    For Each r In rng
    Sheets("Test").Range("D" & i + 5).Formula = "='Data'!" & r.Address
    i = i + 1
    Next
    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'

  14. #14
    Hi mdmackillop,

    I have attached a example for a better understanding. I'm looking to send the values of the cells in black font over to the other sheet the red fonts will not be visible to the user.

    I can now see how your method works.

    Thanks for your time



    Nurofen

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's a udf solution, but it very much depends on your real data being consistent with your sample
    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'

  16. #16
    Hi mdmackillop,

    That is prefect, thank you so much





    Thank you for your time

    Nurofen

  17. #17
    How do i run the macro?

    Do i have to call the function?

    Thank you again for the help mdmackillop


    Nurofen

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You use this as any other Excel function, as shown in the table.
    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
  •