开发者

VBA another for-loop question

I know there are a ton of questions about constructing开发者_如何转开发 looped codes in vBA already but hopefully this will be a quick answer, i wasn't able to find a page addressing this issue.

My goal is to check the values from one range with values in another range, and if is a match it will perform a function and display results at the end. However, if the corresponding value in the range is "N/A" I want the results to display immediately and move onto the next checked value. Right now I am obtaining a 'no for loop' error for my code and i understand why. But I don't know how to fix this problem. Can anyone help?

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")

For Each a In anion
   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"
                Next a
             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
Next a
End Sub

I have the line 'Next a' twice, and excel doesnt like this, but I want to automatically jump to the next checked value without performing the remaining function if I get the "N/A" value.


I know this will rile the feathers of some of my purist brethren, but I would actually suggest a judicious use of GoTo in your case:

    For Each a In anion
       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"
                    GoTo NextA
                    ....
                 End If
            End If
            ....
       Next j
       ....
NextA:
    Next a

Overuse of GoTo will quickly turn your code into spaghetti, but in this case I think it is actually the most readable option you have.


You must define a reference to an object using SET:

SET groups = Worksheets("Solubility").Range("A2:A33")

(Same for all ranges)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜