Results 1 to 3 of 3

Rounding issue in VBA

Threaded View

Grilleman Rounding issue in VBA 04-26-2019, 11:54 AM
MrShorty Re: Rounding issue in VBA 04-26-2019, 12:51 PM
Grilleman Re: Rounding issue in VBA 04-26-2019, 01:08 PM
  1. #1
    Forum Contributor
    Join Date
    04-11-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    245

    Rounding issue in VBA

    Hello -

    Attached is a workbook that allows user to compute new pricing based on established criteria. Everything works great except when I apply a discount of say 10%, the result is not rounded properly. Anyway to fix this rounding issue using VBA code? FYI... I know I can easily correct this situation by inputing a formula directly in the worksheet itself but I'm trying to learn how to effect these sorts of changes using VBA.

    Thanks!

    Grilleman

    
    Sub PriceIncrease()
    
    'Calculator that allows user to adjust pricing by Business Group, by Product Group, by Material
    
    Dim Rows As Long
    Dim Rg As Range
    Set Rg = Range("A1").CurrentRegion
    
    Dim BizGrp As String
    BizGrp = Range("K3")
    Dim ProGrp As String
    ProGrp = Range("K4")
    Dim Material As String
    Material = Range("K5")
    Dim Adjustment As Double
    Adjustment = Range("K6")
    
    Dim i As Long
    
        For i = 2 To Rg.Rows.Count
        
            If Cells(i, 1) = BizGrp And Cells(i, 2) = ProGrp And Cells(i, 4) = Material Then
            Cells(i, 6) = Cells(i, 5) * Adjustment
            
            End If
            
            If Cells(i, 6) <> 0 Then
            Cells(i, 7) = Cells(i, 5) + Cells(i, 6)
            Cells(i, 8) = (Cells(i, 7).Value2 - Cells(i, 5).Value2) / Cells(i, 5).Value2
            
            Rg.Cells(i, 8).NumberFormat = "0.00%" '  HERE'S THE LINE OF CODE THAT RETURNS PERCENT TO A TWO DIGIT PRECISION.
        
            ElseIf Cells(i, 6) = "" Then
            Cells(i, 7) = Cells(i, 5).Value2
            
            End If
            
        Next i
        
    Dim Myrow As Range
    Set Rg = Range("A1").CurrentRegion
    Set Rg = Rg.Offset(1, 0)
    Set Rg = Rg.Resize(Rg.Rows.Count - 1)
    
        For Each Myrow In Rg.Rows
        
            If Myrow.Row Mod 2 = 0 Then
            Myrow.Interior.Color = RGB(204, 204, 255)
        
            End If
            
        Next Myrow
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. =if rounding issue
    By the running frog in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-29-2014, 05:12 AM
  2. Rounding Issue
    By alex57 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-08-2008, 09:54 AM
  3. Another rounding issue
    By Aladin Akyurek in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 04:05 AM

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