VBA for each loops

rharn

Board Regular
Joined
Jun 21, 2011
Messages
54
I am trying to write a sub that compares the values from one range to the values in another range, and to perform certain functions if it finds a match. However, if the corresponding value is "N/A" I want the loop to automatically skip to the next checked value "a" . I do not know how to skip to the next "a" however and I get an error that says it does not have a corresponding for in the program. Can someone help me with this? it should be a relatively easy solution but I am new to VBA still. The code is attached below.

Code:
Sub solubility()
 
    Dim coeff As Range, groups As Range
    Dim anion As Range
    Dim a As Range
    Dim nextrow As Long
    Dim j As Range
    
    Worksheets("properties").Select
    Range("P7:P2000").Select
    Selection.ClearContents
 
    'solubility groups range
    groups = Worksheets("Solubility").Range("A2:A33")
    'group coefficients range
    coeff = Worksheets("Solubility").Range("B2:B33")
    anion = Worksheets("properties").Range("AB7:AB887")
 
   [COLOR=red] For Each a In anion
[/COLOR]       For Each j In groups
            If UCase(a.Value) = UCase(groups(j).Value) Then
                 If groups(j).Value = "" Or "N/A" Then
                    Worksheets("properties").Range("P" & a.Row).Value = "N/A"
                  [COLOR=red]  Next a
[/COLOR]                 Else
                    anvalue = coeff(j).Value * Range("AC" & a.Row).Value
                 End If
            End If
            If UCase(Range("AD" & a.Row).Value) = UCase(groups(j).Value) Then
                cavalue = coeff(j).Value * Worksheets("properties").Range("AE" & a.Row).Value
            If UCase(Range("AF" & a.Row).Value) = UCase(groups(j).Value) Then
                cb1value = coeff(j).Value * Worksheets("properties").Range("AG" & a.Row).Value
            End If
            If UCase(Range("AH" & a.Row).Value) = UCase(groups(j).Value) Then
                cb2value = coeff(j).Value * Worksheets("properties").Range("AI" & a.Row).Value
            End If
       Next j
            If UCase(Range("AD" & a.Row).Value) = UCase("[MIm]") Then
                cavalue = Range("AE" & a.Row) * Worksheets("solubility").Range("B2").Value + Range("AE" & a.Row) * Worksheets("solubility").Range("B7").Value
            End If
        nextrow = Worksheets("properties").Cells(Rows.Count, 15).End(xlUp).Offset(1, 0).Row
        Worksheets("properties").Range("P" & nextrow).Value = _
            anvalue + cavalue + cb1value + cb2value + Worksheets("solubility").Range("b34").Value
   [COLOR=red] Next a
[/COLOR]End Sub
 
Last edited:
Thanks so much for the help Comfy! that solved the problem! It's nice to have an extra set of eyes sometimes!
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,958
Messages
6,175,628
Members
452,661
Latest member
Nonhle

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