Results 1 to 9 of 9

Thread: [Resolved] Applying a formula to a highlighted region

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    [Resolved] Applying a formula to a highlighted region

    I hope this makes sense. I'd like to write code to apply the round function to each cell in a user selected region. For example if i highlight with my mouse from A1 to C3 i want all of those cells to be rounded. I found some code in here that uses hard coded values for the region, but that's not exactly what i'm looking for.

    I thought i could do it like this:

    Dim CellValue As Double

    For Each c In ActiveSheet.CurrentRegion.Cells
    CellValue = ActiveCell.Value
    ActiveCell.Value = Round(CellValue, 0)
    Next

    But it only rounds the first Cell in the region.

    Thanks
    Last edited by PhalSe; Apr 8th, 2004 at 05:35 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This will help you by showing you some different ways to do what you want.
    VB Code:
    1. Private Sub cmdRoundRegion_Click()
    2.  
    3.     Dim i As Integer
    4.     Dim ii As Integer
    5.    
    6.     areaCount = Application.Selection.Areas.Count
    7.     If areaCount <= 1 Then
    8.         MsgBox "The selection contains " & Application.Selection.Columns.Count & " columns."
    9.         MsgBox "The selection contains " & Application.Selection.Rows.Count & " rows."
    10.     Else
    11.         For i = 1 To areaCount
    12.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Columns.Count & " columns."
    13.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Rows.Count & " rows."
    14.         Next i
    15.     End If
    16.     Set xRange = Application.ActiveCell.CurrentRegion
    17.     c = xRange.Columns(xRange.Columns.Count).Column
    18.     c = Chr(64 + c)
    19.     b = xRange.Columns(1).Column
    20.     b = Chr(64 + b)
    21.     MsgBox "First column = " & b
    22.     MsgBox "First row = " & xRange.Rows(1).Row
    23.     MsgBox "Last column = " & c
    24.     MsgBox "Last row = " & xRange.Rows(xRange.Rows.Count).Row
    25.     For i = xRange.Rows(1).Row To xRange.Rows(xRange.Rows.Count).Row
    26.         For ii = xRange.Columns(1).Column To xRange.Columns(xRange.Columns.Count).Column
    27.             b = xRange.Columns(ii).Column
    28.             b = Chr(64 + b)
    29.             CellValue = Worksheets("Sheet1").Range(b & i).Value
    30.             If IsEmpty(CellValue) = False Then
    31.                 Worksheets("Sheet1").Range(b & i).Value = Round(CellValue, 0)
    32.             End If
    33.         Next
    34.     Next
    35.  
    36. End Sub


    HTH
    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    After running the macro....

    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    Thank you, I don't fully understand all of that code, but i've got an idea how it works. If i'm reading this right it will only work if the selected region is in "Sheet1". I can't be certain that I will always be working in "Sheet1" or that it won't have been renamed to something else. Can i replace "Worksheets("Sheet1")" with "ActiveSheet"?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes you can. I was just trying to show some different ways to do the same thing.
    Updated code fragment.
    VB Code:
    1. '...
    2. '...
    3. '...
    4. CellValue = ActiveSheet.Range(b & i).Value
    5. If IsEmpty(CellValue) = False Then
    6.     ActiveSheet.Range(b & i).Value = Round(CellValue, 0)
    7. End If
    8. '...
    9. '...
    10. '...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    I'm having some problems with the code. I was doing some tests and it seems to behave funny if there is anything in the cells before it. Attached is a zip file with a before and after example. I don't have hosting to post teh images inline, sorry.

    This is the code i'm using, it isn't changed very much at all.

    VB Code:
    1. Sub cmdRoundRegion_Click()
    2. '
    3. ' cmdRoundRegion_Click Macro
    4. ' Macro Written by RobDog888 on VBForums, Alterred by
    5. '
    6.  
    7.     Dim i As Integer
    8.     Dim ii As Integer
    9.    
    10.     MsgBox "Now starting cmdRoundRegion_Click Macro"
    11.     areaCount = Application.Selection.Areas.Count
    12.     If areaCount <= 1 Then
    13.         MsgBox "The selection contains " & Application.Selection.Columns.Count & " columns."
    14.         MsgBox "The selection contains " & Application.Selection.Rows.Count & " rows."
    15.     Else
    16.         For i = 1 To areaCount
    17.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Columns.Count & " columns."
    18.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Rows.Count & " rows."
    19.         Next i
    20.     End If
    21.     Set xRange = Application.ActiveCell.CurrentRegion
    22.     c = xRange.Columns(xRange.Columns.Count).Column
    23.     c = Chr(64 + c)
    24.     b = xRange.Columns(1).Column
    25.     b = Chr(64 + b)
    26.     MsgBox "First column = " & b
    27.     MsgBox "First row = " & xRange.Rows(1).Row
    28.     MsgBox "Last column = " & c
    29.     MsgBox "Last row = " & xRange.Rows(xRange.Rows.Count).Row
    30.     For i = xRange.Rows(1).Row To xRange.Rows(xRange.Rows.Count).Row
    31.         For ii = xRange.Columns(1).Column To xRange.Columns(xRange.Columns.Count).Column
    32.             b = xRange.Columns(ii).Column
    33.             b = Chr(64 + b)
    34.             CellValue = ActiveSheet.Range(b & i).Value
    35.             If IsEmpty(CellValue) = False Then
    36.                 ActiveSheet.Range(b & i).Value = Round(CellValue, 0)
    37.             End If
    38.         Next
    39.     Next
    40.  
    41. End Sub

    It always evaluates as if areaCount is <= 1, which seems odd to me. It also prints out the wrong rows and columns.
    Attached Files Attached Files

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    You don't need any hosting to post the images in line. Right click
    and click View Source... and look for the code to show the image.

    All I am doing is attaching the image to the post and then edit
    the post after and add an IMG and point the url text to the
    attachment using "http://www.vbforums.com/attachment.php?
    s=&amp;postid=88888888" where 88888888 is the id of the
    attachment. You can get the id after the updating of the
    post. Look at your url for the id. So you actually have to edit your
    post twice.

    This keeps the image on vbforums webservers and i dont have to
    bloat my webserver with a bunch of images.

    Hope I explained it ok.

    I will check out your highlight issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    I think i figured out the problem. I wasn't aware earlier but there is a difference between a Range and a Region. Sorry for any confusion. A Region is apparantly a range bounded by empty Cells. I updated a line to remove this issue. The other issue that wasn't immediately apparant was that it was actually making changes for me offset by several columns. I made another adjustment to resolve that. Thank you for your help RobDogg, you pointed me in the right direction. I get frustrated with VBA trying to learn all the predefined objects, properties, and methods.

    Here is the updated code that seems to work for me. Later i'll try updating it further so it works on multiple selected ranges, but this is good enough for now.

    Edit: the change was easier than i thought because i just finished it. I've updated the code as well.


    VB Code:
    1. Sub cmdRoundRange()
    2. '
    3. ' cmdRoundRange Macro
    4. ' Macro Written by RobDog888 on VBForums, Alterred by Michael Lawrence Jr.
    5. '
    6.  
    7.     Dim i As Integer
    8.     Dim ii As Integer
    9.     Dim ac As Integer
    10.    
    11.     areaCount = Application.Selection.Areas.Count
    12.     MsgBox "areaCount = " & areaCount
    13.     If areaCount <= 1 Then
    14.         Set xRange = Application.Selection
    15.         c = xRange.Columns(xRange.Columns.Count).Column
    16.         c = Chr(64 + c)
    17.         b = xRange.Columns(1).Column
    18.         b = Chr(64 + b)
    19.         MsgBox "First column = " & b
    20.         MsgBox "First row = " & xRange.Rows(1).Row
    21.         MsgBox "Last column = " & c
    22.         MsgBox "Last row = " & xRange.Rows(xRange.Rows.Count).Row
    23.         For i = xRange.Rows(1).Row To xRange.Rows(xRange.Rows.Count).Row
    24.             For ii = xRange.Columns(1).Column To xRange.Columns(xRange.Columns.Count).Column
    25.                 b = ii
    26.                 b = Chr(64 + b)
    27.                 CellValue = ActiveSheet.Range(b & i).Value
    28.                 MsgBox "CellValue of " & b & i & " is " & CellValue
    29.                 If IsEmpty(CellValue) = False Then
    30.                     ActiveSheet.Range(b & i).Value = Round(CellValue, 0)
    31.                 End If
    32.             Next
    33.         Next
    34.     Else
    35.         For ac = 1 To areaCount
    36.             Set xRange = Application.Selection.Areas(ac)
    37.             c = xRange.Columns(xRange.Columns.Count).Column
    38.             c = Chr(64 + c)
    39.             b = xRange.Columns(1).Column
    40.             b = Chr(64 + b)
    41.             MsgBox "First column = " & b
    42.             MsgBox "First row = " & xRange.Rows(1).Row
    43.             MsgBox "Last column = " & c
    44.             MsgBox "Last row = " & xRange.Rows(xRange.Rows.Count).Row
    45.             For i = xRange.Rows(1).Row To xRange.Rows(xRange.Rows.Count).Row
    46.                 For ii = xRange.Columns(1).Column To xRange.Columns(xRange.Columns.Count).Column
    47.                     b = ii
    48.                     b = Chr(64 + b)
    49.                     CellValue = ActiveSheet.Range(b & i).Value
    50.                     MsgBox "CellValue of " & b & i & " is " & CellValue
    51.                     If IsEmpty(CellValue) = False Then
    52.                         ActiveSheet.Range(b & i).Value = Round(CellValue, 0)
    53.                     End If
    54.                 Next
    55.             Next
    56.  
    57.         Next ac
    58.     End If
    59.    
    60. End Sub
    Last edited by PhalSe; Apr 8th, 2004 at 05:00 PM.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    CurrentRegion vs. Selection range. I see.

    Glad its working correctly now.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width