VBA to copy, multiply, roundup and paste values

Supernerd81

New Member
Joined
Jun 16, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am trying to automate our price increases. Unfortulately I don't know much about excel vba. What I want:
Take the old price in column C, multiply by the value in F2, roundup to the nearest 10 and paste it in column B.
I am currently using a formula which I copy paste every row:
Excel Formula:
=ROUNDUP((C2*$F$2);-1)
7.png


I am trying to automate this with a macro, but thus far I have no succes. This is what I have:
VBA Code:
Sub Copypaste()
Dim SrchRng As Range, cel As Range
Set SrchRng = Range("C2:C200")
For Each cel In SrchRng
    If cel.Value <> "" Then
        cel.Offset(0, -1).Value = cel.Value
    End If
Next cel
End Sub

This at least copies my values in column B (and leaves the 'price on request') but I still have to multiply the entire column. Does anyone have a suggestion on how I can do that? The simple ROUNDUP function doesn't seem to work in VBA?

Thanks!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about:
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRatio As Double
  With Application
  oldPrices = .Transpose(Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row))
  ReDim newPrices(1 To UBound(oldPrices), 1 To 1)
  increaseRatio = Range("F2").Value2
  
  For i = 1 To UBound(oldPrices)
    If IsNumeric(oldPrices(i)) Then
      newPrices(i, 1) = .WorksheetFunction.RoundUp(oldPrices(i) * increaseRatio, -1)
    Else
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
  
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub
 
Upvote 0
Wow that works, but I have no idea what it does!
I changed your code because you used column D, this should be C.

Thanks a lot!
 
Upvote 0
Another option using Evaluate. I'm not sure where you get 140 from for potato (?) doesn't seem to be 125 * 1.05 ?!

Code
VBA Code:
Option Explicit
Sub Supernerd81()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change sheet name to suit ***
    Dim x As Double
    x = ws.Range("F2").Value
    
    With ws.Range("B2:B" & Cells(Rows.Count, "C").End(xlUp).Row)
        .Value = Evaluate("IF(isnumber(" & .Offset(0, 1).Address & "),Roundup(" & .Offset(0, 1).Address & "*" & x & ",2)," & .Offset(0, 1).Address & ")")
    End With
End Sub

Before
Book1
ABCDEF
1Old PriceOld Price
2Apple€ 2.250increase prices by1.05
3Pear€ 3.250
4Banana Price on request
5Tomato€ 11.590
6Potato€ 125
Sheet1


After
Book1
ABCDEF
1Old PriceOld Price
2Apple€ 2.370€ 2.250increase prices by1.05
3Pear€ 3.420€ 3.250
4Banana Price on request Price on request
5Tomato€ 12.170€ 11.590
6Potato€ 131€ 125
Sheet1
 
Upvote 0
I just realised that it's your separators that's confusing me (Doh!). Try this instead
VBA Code:
Option Explicit
Sub Supernerd81()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change sheet name to suit ***
    Dim x As Double
    x = ws.Range("F2").Value
   
    With ws.Range("B2:B" & Cells(Rows.Count, "C").End(xlUp).Row)
        .Value = Evaluate("IF(isnumber(" & .Offset(0, 1).Address & "),Roundup(" & .Offset(0, 1).Address & "*" & x & ",-1)," & .Offset(0, 1).Address & ")")
    End With
End Sub

Before
supernerd81.xlsm
ABCDEF
1Old PriceOld Price
2Apple2,250increase prices by1.05
3Pear3,250
4Banana Price on request
5Tomato1,159
6Potato125
Sheet1


After
supernerd81.xlsm
ABCDEF
1Old PriceOld Price
2Apple2,3702,250increase prices by1.05
3Pear3,4203,250
4Banana Price on request Price on request
5Tomato1,2201,159
6Potato140125
Sheet1
 
Upvote 0
I just realised that it's your separators that's confusing me (Doh!). Try this instead
VBA Code:
Option Explicit
Sub Supernerd81()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change sheet name to suit ***
    Dim x As Double
    x = ws.Range("F2").Value
  
    With ws.Range("B2:B" & Cells(Rows.Count, "C").End(xlUp).Row)
        .Value = Evaluate("IF(isnumber(" & .Offset(0, 1).Address & "),Roundup(" & .Offset(0, 1).Address & "*" & x & ",-1)," & .Offset(0, 1).Address & ")")
    End With
End Sub

Before
supernerd81.xlsm
ABCDEF
1Old PriceOld Price
2Apple2,250increase prices by1.05
3Pear3,250
4Banana Price on request
5Tomato1,159
6Potato125
Sheet1


After
supernerd81.xlsm
ABCDEF
1Old PriceOld Price
2Apple2,3702,250increase prices by1.05
3Pear3,4203,250
4Banana Price on request Price on request
5Tomato1,2201,159
6Potato140125
Sheet1
It's nice to have alternatives.
That one gives me #VALUE in all cells and I am not sure why.
 
Upvote 0
As you can see from post #6 it does work for me on the small data set I used to test it. I would really need to get your actual worksheet to see what the problem is, but whether it's worth your trouble, given that you already have a working solution from @Flashbond is up to you. You could provide a sample of your worksheet using the XL2BB add in, or alternatively share your file via Dropbox, Google Drive or similar file sharing platform.
 
Upvote 0
Oh my bad.. I am glad you have managed. Thanks for the feedback 👍
I am back with one more request!
I would like to skip certain rows. I can make the values a different text colour or I can fill the cells with a different colour. I have tried putting it in your macro but I'm having difficulty. It says 'object required'.
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRatio As Double
  With Application
  oldPrices = .Transpose(Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row))
  ReDim newPrices(1 To UBound(oldPrices), 1 To 1)
  increaseRatio = Range("F2").Value2
 
  For i = 1 To UBound(oldPrices)
    If IsNumeric(oldPrices(i)) And Interior.Color(oldPrices(i)) = RGB(255, 0, 0) Then
      newPrices(i, 1) = .WorksheetFunction.RoundUp(oldPrices(i) * increaseRatio, -1)
    Else
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
 
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub

I have tried adding "Interior.Color(oldPrices(i)) = RGB(255, 0, 0)" which should only select the red cells, but as I said this requires an object! (And "oldPrices(i)" is not the object apparently.)
 
Upvote 0
These are just arrays. They only store values. They are not range or actual cell referances. You can't format them.

However, you can define rule for skipping. What would be the rule?
If I were you I would go for conditional formatting for the color. What will be the coloring rule?
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top