VBA to Multiply cell calue if another cell equals certain value

nirvehex

Active Member
Joined
Jul 27, 2011
Messages
493
Office Version
  1. 365
Platform
  1. Windows
Question:

In column Y i have numbers:

y2 = 8
y3 = 8
y4 = 8
y5 = 7
y6 = 6
y7 = 4
y8 = 1.5

and so on....

In column X I have text:


x2 = no
x3 = yes
x4 = no
x5 = yes
x6 = yes
x7 = yes
x8 = no

What I would like to do is where column X reads "No", in the corresponding Y cell I would like to multiply the existing value by 2.5 and have it become the new value for that cell. S

So for example, after I run the macro, the cell values would read as follows:

y2 = 20
y3 = 8
y4 = 20
y5 = 7
y6 = 6
y7 = 4
y8 = 3.75

Any idea how to do this on VBA?

Thanks!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi.

Code:
Sub Multiply()


For Each r In Range("Y2:Y" & Cells(Rows.Count, "Y").End(xlUp).Row)
      
      If r.Offset(0, -1).Value = "no" Then
            r.Value = r.Value * 2.5
      End If

Next r


End Sub

Regards
 
Upvote 0
Thanks for the info. The code keeps breaking as soon as it goes to run it though. It breaks on the "For Each r In Range("Y2:Y" & Cells(Rows.Count, "Y").End(xlUp).Row)" line.

It says "Object required"

Any idea why?
 
Upvote 0
Sorry - no.

Perhaps there is some fileshare site that you use?

Regards
 
Upvote 0
I didn't and it worked fine.

Regards



Hey I figured out what was going on.. Needed to make it Rows not Row.


Code:
Sub CleanExport() 
    For Each r In Range("Y2:Y" & Cells(Rows.Count, "Y").End(xlUp).Row) 
        If UCase(r.Offset(0, -1).Value) = "TOT" Then 
            r.Value = r.Value * 0.00495113169 
        End If 
    Next r 
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,916
Members
444,694
Latest member
JacquiDaly

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