+ Reply to Thread
Results 1 to 10 of 10

VBA To Update Column In Workbook

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    VBA To Update Column In Workbook

    I am iterating a directory and want to update the data in column E to have a percent sign. I used the macro recorder and it spit out the below, which works for my current workbook, but my issue is that the number of rows with data vary from workbook to workbook. So in certain instances each row is not being updated.

    How should this be changed to take ALL rows with data in row E, and perform this update?
    **And if any more proficient devs see a way to reduce clutter I'm all for it **

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: VBA To Update Column In Workbook

    You could use of these two...

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    HTH
    Regards, Jeff

  3. #3
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA To Update Column In Workbook

    Hi @jeffreybrown - if I run that syntax it converts the data from
    4.2
    to 420%

    My desired output would be
    4.2% = essentially just add the % sign to the value that already exists in E

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: VBA To Update Column In Workbook

    How about...

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA To Update Column In Workbook

    @jeffreybrowne -
    That did it! One slight tweak. If we have 2 in the cell we only want it to show 2% and not 2.0%

    Is there a way to slightly alter that so it can account for only single digits being in the cell?

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: VBA To Update Column In Workbook

    This will display no decimal portion, but beware that if the underlying number does have a decimal portion, it will be rounded only in the display:

    Please Login or Register  to view this content.
    That is, 2.0% will look like 2% and 2.2% will also look like 2%.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  7. #7
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA To Update Column In Workbook

    @6StringJazzer - Can I conditionally format the cell based off the value? Meaning if the cell contains 2.2 then it is formatted to 2.2% but if it only contains 2 then it is formatted to 2%

  8. #8
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA To Update Column In Workbook

    Quote Originally Posted by 6StringJazzer View Post
    This will display no decimal portion, but beware that if the underlying number does have a decimal portion, it will be rounded only in the display:

    Please Login or Register  to view this content.
    That is, 2.0% will look like 2% and 2.2% will also look like 2%.
    I have tried this - but it is giving me the 2.0% regardless.
    Please Login or Register  to view this content.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: VBA To Update Column In Workbook

    I am starting to see what you want now. You had kinda sorta the right idea with your code but it is almost always preferable to manipulate numbers arithmetically rather than managing them as strings.

    There are two ways to do this.

    1. Use jeffreybrown's code from post #4 and also use conditional formatting, as you suggest:

    =E1=MROUND(E1,0.01)

    format is Percent with no decimal places

    (apply to column E)

    2. Do it all in the code. Modify jeffreybrown's code as shown:

    Please Login or Register  to view this content.

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: VBA To Update Column In Workbook

    I see you have marked this as SOLVED, but I do want to add one more thing. The way you want to display this number is contrary to the rules of significant digits and precision. The number 2% and the number 2.0% are not the same number. If you display 2%, it implies that you only know the precision to one significant digit. That is, the number could fall between 2.0% and 2.9%, but the number in the first decimal place is uncertain. Except in your case, it is in fact certain, so the displayed precision is misleading.

+ 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. [SOLVED] VBA code to update query from a password protected excel workbook and then close workbook
    By MrChipper in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2017, 07:41 AM
  2. Auto update all dates in a column to todays date on workbook open
    By smorri25 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-01-2016, 03:12 AM
  3. Replies: 3
    Last Post: 07-16-2016, 08:29 PM
  4. Replies: 1
    Last Post: 07-16-2016, 11:13 AM
  5. Replies: 1
    Last Post: 07-16-2016, 11:11 AM
  6. Replies: 0
    Last Post: 07-21-2015, 02:35 AM
  7. Replies: 2
    Last Post: 06-21-2013, 08:59 PM

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