VBA Loop through rows with multiple ranges and conditions

Lesara

New Member
Joined
Jul 28, 2018
Messages
2
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
Disclaimer: New to VBA
I have an excel sheet where I would like to build a vba that does the following things.

  1. My requirement is to fill a value in row Z based on a formula determined by the text in row M
  2. The formula deals with summing values from row A, B, C in different combinations (determined by text in row M)
  3. Loop this for each row starting from row 5 to row 10000
Eg: If cell M5 = Apple, then Z5 = A5+B5 ; If cell M9 = Samsung, then Z9 = C9+A9
I know a nested if formula does it easily but there are just too many conditions and I'm looking for an automatic and cleaner route.
Thanks!


</body>Disclaimer: New to VBA
I have an excel sheet where I would like to build a vba that does the following things.

  1. My requirement is to fill a value in row Z based on a formula determined by the text in row M
  2. The formula deals with summing values from row A, B, C in different combinations (determined by text in row M)
  3. Loop this for each row starting from row 5 to row 10000
Eg: If cell M5 = Apple, then Z5 = A5+B5 ; If cell M9 = Samsung, then Z9 = C9+A9
I know a nested if formula does it easily but there are just too many conditions and I'm looking for an automatic and cleaner route.
Thanks!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Option Explicit


Sub Lesara()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim i As Long
    For i = 5 To 10000
        If Range("M" & i) = "Apple" Then
            Range("Z" & i) = Range("A" & i) + Range("B" & i)
        ElseIf Range("M" & i) = "Samsung" Then
            Range("Z" & i) = Range("A" & i) + Range("C" & i)
        End If
    Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    MsgBox "Complete"


End Sub
 
Upvote 0
Cross posted https://stackoverflow.com/questions...-multiple-ranges-and-conditions?noredirect=1#

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Code:
Option Explicit


Sub Lesara()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim i As Long
    For i = 5 To 10000
        If Range("M" & i) = "Apple" Then
            Range("Z" & i) = Range("A" & i) + Range("B" & i)
        ElseIf Range("M" & i) = "Samsung" Then
            Range("Z" & i) = Range("A" & i) + Range("C" & i)
        End If
    Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    MsgBox "Complete"


End Sub


Thank you soooo much! Exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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