VBA Excel Format Cells Based on Multiple Factors

时间:2016-07-11 20:01:24

标签: excel vba excel-vba

OK, So, I have an excel spreadsheet that I am developing for another individual to utilize to keep track of certain chemicals as they are applied to ensure that they are not over the limits for our country or for export. I have the conditional formatting set up so that the cells turn red (ok, peach) or green based on whether that result is over the limit or not. There is a user form to input all of above information as well. However, this is not clear enough, so my boss wants a header for each entry on the spreadsheet, as you can see in the image, to have each country listed, which wasn't a problem. Where I am absolutely lost is how do I utilize VBA to format each country cell to turn red or green based on each of the results? Country spreadsheet

    Set ws = Sheets("Master")
Dim NextRow As Long

NextRow = ws.Range("A1").CurrentRegion.Rows.Count

With Worksheets("Master").Range("A1")
    .Offset(NextRow, 0).Value = Me.txtTestDate.Value
    .Offset(NextRow, 1).Value = Me.txtSpraybox.Value
    .Offset(NextRow, 2).Value = Me.txtRanch.Value
    .Offset(NextRow, 3).Value = Me.txtFieldBox.Value
    .Offset(NextRow, 4).Value = Me.txtCommbox.Value
    .Offset(NextRow, 5).Value = Me.txtVarietybox.Value
    .Offset(NextRow, 6).Value = "Countries"
    .Offset(NextRow, 8).Value = "US"
    .Offset(NextRow, 9).Value = "Europe"
    .Offset(NextRow, 10).Value = "Japan"
    .Offset(NextRow, 11).Value = "Canada"
    .Offset(NextRow, 12).Value = "Taiwan"
 .

. . .

Dim ChmArray() As Variant
ChmArray = Array("Cyprodinil", "Fenhaxamide", "Fludioxonil", "Iprodione", "Acetamiprid", "Azoxystrobin", "Zeta_Cypermethrin", "Boscalid", "Imidacloprid", "Bifenthrin", "Chlorantraniliprole", "Malathion", "Chlorothalonil", "Pyraclostrobin", "Spinosad", "Thiamethoxam", "Phosmet", "Captan")
    If ws.Cells(NextRow, 8).Value = "US" And ws.Cells(NextRow, 6).Value = "Cyprodinil" And ws.Cells(NextRow, 7).Value < Sheets("Limits").Cells(2, 2).Value Then
    ws.Cells(NeztRow, 8).Format.Condition.Interior.Color = vbGreen
    End If

I figured some form of If- Else-Then Statement would be my best bet, but so far, I have pretty much been bashing my head into the keyboard for all the progess I have made. I have about 2 weeks experience using VBA, so I'm not in anyway qualified for this job. I appreciate any possible advice.

0 个答案:

没有答案