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!
 
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?
Well the cells I want to skip are linked cells, they get their value from a different sheet. I mark them manually with a colour.
These cells contain:
VBA Code:
=Sheet2!I11
(for example)
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
As I told you, it is not working with cell references. I can't test it right now but it should work.
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRate As Double
  With Application
  oldPrices = .Transpose(Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row).Formula)
  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) * increaseRate, -1)
    ElseIf Not InStr(oldPrices(i), "=Sheet2!I11") > 0 Then
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
 
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub
 
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?
Ah, now I understand what you are saying.

No there are more formulas than the one in my example. My conditioning formatting rule would be: highlight cell rules --> use a formula to determine which cells to format --> Formula:
Excel Formula:
=ISFORMULA(C2)
(wow that actually saves me from doing that manually!)

Can you use that rule to skip and copy the old values instead?
Your last macro makes everything zero again!
 
Upvote 0
If I were you I would only check if value contains "=" sign.
So what the code does is, check if it is a number, then multiply and round up.
Then check if it has "=" sign (if it is a formula). If it is not, then copy the text (like "Price on request")
If it is not numeric, if it contains "=" then, skip. Skip to next value.
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRate As Double
  With Application
  oldPrices = .Transpose(Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row).Formula)
  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) * increaseRate, -1)
    ElseIf Not InStr(oldPrices(i), "=") > 0 Then
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
 
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub
 
Upvote 0
If I were you I would check if value contains only "=" sign.
So what the code does is, check if it is a number, then multiply and round up.
Else if, it has "=" sign (if it is a formula). If it is Not, then copy the text (like "Price on request")
If it is not numeric, if it contains "=" then, end if. Do nothing. Skip to next value.
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRate As Double
  With Application
  oldPrices = .Transpose(Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row).Formula)
  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) * increaseRate, -1)
    ElseIf Not InStr(oldPrices(i), "=") > 0 Then
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
 
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub
 
Upvote 0
If I were you I would check if value contains only "=" sign.
So what the code does is, check if it is a number, then multiply and round up.
Else if, it has "=" sign (if it is a formula). If it is Not, then copy the text (like "Price on request")
If it is not numeric, if it contains "=" then, end if. Do nothing. Skip to next value.
VBA Code:
Sub test()
  Dim oldPrices As Variant, newPrices As Variant, increaseRate As Double
  With Application
  oldPrices = .Transpose(Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row).Formula)
  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) * increaseRate, -1)
    ElseIf Not InStr(oldPrices(i), "=") > 0 Then
       newPrices(i, 1) = oldPrices(i)
    End If
  Next
  End With
 
  Range("B2").Resize(UBound(newPrices)) = newPrices
End Sub
It doesn't work. I can get it to copy and multiply the values. I can get it to copy the cells with the formula. But not both at the same time. :(
 
Upvote 0
Ok what are your conditions, please state them one by one again from the beginning.
 
Upvote 0
Ok what are your conditions, please state them one by one again from the beginning.
Sorry if I am being unclear. :(

- If the cell contains a number, copy paste and multiply (and round up to the nearest 10).
- If the cell contains text, just copy paste
- If the cell contains formula (= sign), just copy paste
- If the cell contains nothing, do nothing.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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