Dividing a Defined Range by a Defined Range

ohFice

New Member
Joined
May 30, 2019
Messages
24
Hello,

Been trying to figure this out for a few days now, and starting to drive me crazy.

VBA Code:
Sub Annualize()

Dim WB As Workbook
Set WB = ThisWorkbook

Dim WS As Worksheet
Set WS = WB.ThisWorksheet

Dim Range1 As Range
Set Range1 = WS.Range("B2:B4")

Dim Range2 As Range
Set Range2 = WS.Range("C2:C4")

For Each C In Range1

    If Range2.Address <> 12 Then
        For Each D In Range2()
            Range1.Cells = (Range1.Address / Range1.Address) * 12
        Next D
        
    End If
    
Next C

End Sub

I am starting to believe I would need to utilize an array to achieve the results needed. I am trying to tell VBA that if the cells in range 2 are not equal to twelve, then have the corresponding cells in range1 divide by itself, and multiply by 12.

The code I have above is completely off, just tried to simplify the code for easier understanding of what I am trying to achieve, as well as the methodology and syntax I am trying to use.

If somebody could advise further, that would be extremely appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
So if C2 =12 leave B2 as is, otherwise B2=12, & the same for the other cells. Is that right?
 
Upvote 0
For reference B2/B2 =1
How about
VBA Code:
Sub ohFice()
   With Range("B2:B4")
      .Value = Evaluate(Replace("if(" & .Offset(, 1).Address & "=12,12,if(@="""","""",@))", "@", .Address))
   End With
End Sub
 
Upvote 0
I am an idiot, I meant to do / say if C2 = 12 leave B2 as is, otherwise B2 = (B2/C2) * 12.

Sorry about that.
 
Upvote 0
Sorry, I am terrible at VBA.

Would it be easier / more correct to use an array? Also could you kind of explain what your coding is doing? You don't have to, just want to learn for myself.
 
Upvote 0
Ok how about
VBA Code:
Sub ohFice()
   With Range("B2:B4")
      .Value = Evaluate(Replace("if(" & .Offset(, 1).Address & "<>12,@/" & .Offset(, 1).Address & "*12,if(@="""","""",@))", "@", .Address))
   End With
End Sub
This is effectively a formula
=if(C2<>12,B2/C2*12,if(B2="","",B2))
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

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