+ Reply to Thread
Results 1 to 13 of 13

Converting code from static to dynamic

  1. #1
    Registered User
    Join Date
    10-19-2017
    Location
    Poland
    MS-Off Ver
    Office '16
    Posts
    4

    Question Converting code from static to dynamic

    Hello All! I started writing Macros a couple weeks ago.
    Currently I've managed to write lots of code without using "range" at all, using for example Cells(row, col) references instead.
    Now I wish to transform my code from working on defined columns to whichever is selected.

    I'm having a hard time transforming the following code ..

    Range("L:L") = [index(Upper(L:L),)]

    .. to using a Range defining the active(cell.)column but, with an offset of +1 (ie, I wish to target the column to the right of the currently selected one)
    its the combo of 'range', 'activecell' and 'offset' which is throwing me off as I haven't made use of them previously.

    Thank you very much for your time and patience !
    Best regards,
    Kris

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Converting code from static to dynamic

    What are you trying to achieve with that code snippet?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Converting code from static to dynamic

    Hi,
    What is the code supposed to do?

  4. #4
    Registered User
    Join Date
    10-12-2015
    Location
    Bristol, UK
    MS-Off Ver
    Office 365
    Posts
    39

    Re: Converting code from static to dynamic

    This will target the entire column immediately to the right of the ActiveCell:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    .Offset(0,1) means Offset by 0 rows and 1 column.

  5. #5
    Registered User
    Join Date
    10-19-2017
    Location
    Poland
    MS-Off Ver
    Office '16
    Posts
    4

    Re: Converting code from static to dynamic

    @the two forum guru's, that line is supposed to change the entire column to Uppercase. Its a line I found on a site somewhere and it worked, I kept it as it saved me making a For loop.
    In my quest to making the code "dynamic", this was the only line of my code that I didn't know how to "translate".

    If you have another (vba) formula that can achieve the same result (make the column next to the selected one ALL CAPS) I'd be more than willing to welcome your input :D

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Converting code from static to dynamic

    It might be best to count the rows, then the code would not take so long.

    Please Login or Register  to view this content.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Converting code from static to dynamic

    Hi,

    The literal equivalent would be
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Registered User
    Join Date
    10-19-2017
    Location
    Poland
    MS-Off Ver
    Office '16
    Posts
    4

    Re: Converting code from static to dynamic

    hey! thanks so much for all your replies. Unfortunately I can't get it to work. the best i achieve is getting #VALUE! as cell value
    I know I've got the correct left-hand-side of the equation, its something with the right-hand-side which is not wanting to cooperate.
    There has to be a way to write "[index(Upper(L:L),)]" generically (as reminder, the L:L range should be replaced by something resembling activecell.entirecolumn to have it use the selected column)

    Also, still on topic, does anyone have any input on UCASE vs UPPER methods? I tried looking for it online but couldn't find a site which combined both.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Converting code from static to dynamic

    The code I suggested works here.

    Ucase is VBA, UPPER is a worksheet function.

  10. #10
    Registered User
    Join Date
    10-19-2017
    Location
    Poland
    MS-Off Ver
    Office '16
    Posts
    4

    Re: Converting code from static to dynamic

    Capture11.PNG
    Well for some reason it just won't want to work with me, please see screenshot, and it won't even do the whole column.

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Converting code from static to dynamic

    You need to select all the values in column A for which you want the code to transform values in Column B.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: Converting code from static to dynamic

    ianquiksilver
    Change to
    Please Login or Register  to view this content.

  13. #13
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Converting code from static to dynamic

    Ok, I see what you are doing, in your sample, you want column C to have the upper value of column B. Then you would not offset the actual formula.
    Here's a longer version.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 12-28-2015, 08:30 AM
  2. [SOLVED] VBA - Static code to dynamic
    By gan_xl in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2014, 11:51 AM
  3. [SOLVED] Changing code from Static to Dynamic
    By JamesFletcher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2013, 04:33 AM
  4. [SOLVED] Changing static code to dynamic code
    By JamesFletcher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2013, 07:31 AM
  5. Dynamic Print Area converting to Static range
    By bnl552 in forum Excel General
    Replies: 1
    Last Post: 06-15-2012, 01:14 PM
  6. [SOLVED] Make Static code Dynamic
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2012, 11:09 AM
  7. Converting formula to static code
    By gavster in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-28-2009, 09:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1