View Single Post
 
Old 09-06-2014, 10:59 PM
excelledsoftware excelledsoftware is offline Windows 7 64bit Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

fees? There are no fees this is all volunteer work and I am happy to do it. The modified code below can be placed in a module of your excel sheet and then you can call it whenever. This code will ask you for the percentage and then do the same thing as the last code.
Code:
Option Explicit

Sub SumPriceColumn()
  'Looks for a column named price and adds a specified percent to each row
  Dim CheckRow As Long, LastRow As Long, PriceColumn As Long, RowIssue As String
  Dim PricePercent As Single, PercentString As String, PercentConfirm As String
    On Error Resume Next
    PriceColumn = Range("1:1").Find("PRICE", LookAt:=xlWhole, MatchCase:=False).Column
    'Tell user if price column was not found
    If PriceColumn = 0 Then
      MsgBox "There was no column in row 1 with the word Price"
      End
    End If
    On Error GoTo 0 'Fix error handling back to normal
    'Find the final row
    LastRow = Cells(50000, PriceColumn).End(xlUp).Row
    PercentString = InputBox("What percent would you like to add to the " & _
    "values in column " & Chr(64 + PriceColumn) & ".")
    If PercentString = "" Then
      MsgBox "Nothing entered program is ending."
      End
    End If
    'convert the string to single
    On Error Resume Next
    PricePercent = CSng("1." & PercentString)
    If Err.Number <> 0 Then
      MsgBox "Cannot convert " & PercentString & " into a decimal. " & _
      vbLf & "Please try again using a whole number."
      End
    End If
    PercentConfirm = MsgBox("This will add " & PercentString & "% to all values in " _
    & "column " & Chr(64 + PriceColumn) & ". Do you want to proceed?", vbYesNoCancel)
    If PercentConfirm = vbNo Or PercentConfirm = vbCancel Then End

    
    'Use a loop to add specified percent to all values
    On Error GoTo NotaNumber
    For CheckRow = 2 To LastRow
      Cells(CheckRow, PriceColumn).Value = Round(Cells(CheckRow, PriceColumn).Value * PricePercent, 2)
    Next CheckRow

    If RowIssue = "" Then
      MsgBox "Complete"
      Exit Sub
    Else
      MsgBox "The following rows: " & RowIssue & " were not numbers and could not have " & _
      PercentString & "% added" & vbLf & "They have been highlighted red and skipped."
      Exit Sub
    End If
    
NotaNumber:
  Cells(CheckRow, PriceColumn).Interior.Color = vbRed
  RowIssue = RowIssue & CheckRow & ", "
  Resume Next
   
End Sub
have fun let us know if it works out.
Reply With Quote