Home » excel » excel – Formula in VBA for all rows above a specified value

excel – Formula in VBA for all rows above a specified value

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a question about how to apply and repeat a formula to all values in a column above a specific variable.

I have a large excel sheet with many samples, their temperature ranges (in C) at Time1 and Time2, and their location.

  1. SampleA 54 80 Outside
  2. SampleA 50 81 Inside
  3. SampleB 48 80 Inside
  4. SampleB 52 74 Outside
  5. SampleB 53 78 Outside
  6. SampleC 55 80 Lab conditions
  7. SampleC 50 76 Inside
  8. SampleD 48 77 Freezer
  9. SampleD 51 76 Freezer
  10. SampleD 52 75 Outsize
  11. SampleD 53 80 Lab Conditions

I am working on a script that would understand when SampleA ends and SampleB begins and onwards, calculate the difference between the two temperature ranges for each sample, and give percentage per sample of location. For example, Sample D location percentages would be 50% freezer, 25% Outside, and 25% Lab Conditions.

With the help of others on this website, I have a mock code for at lease separating the different samples. Thanks, @DisplayName. To just note a change in the sample, I placed “Hello” in the column. I am also familiar with FillDown but that can that be stopped at a certain variable to loop back around?

`Sub FormulasNoLoops()
    With Worksheets("Sheet1")
      With .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Offset(, 15)
         .Formula = "=IF(A2<>A3,""hello"","""")"
         .Value = .Value
       End With
     End With
 With Worksheets("Sheet1")
      With .Range("B2:C2", .Cells(.Rows.Count, "B:C").End(xlUp)).Offset(, 16)
         .Formula = "=IF(""=C2-B2"","""")"
         .Value = .Value
       End With
     End With
End Sub`

In the next column, column 16, I would like to calculate the difference between column C and column B. I am not writing the With .Range("B2:C2", .Cells(.Rows.Count, "B:C").End(xlUp)).Offset(, 16) command correctly.

  1. SampleA 54 80 Outside Difference between Temperature2 and Temperature1
  2. SampleA 50 81 Inside Hello Difference between Temperature2 and Temperature1
  3. SampleB 48 80 Inside Difference between Temperature2 and Temperature1
  4. SampleB 52 74 Outside Difference between Temperature2 and Temperature1
  5. SampleB 53 78 Outside Hello Difference between Temperature2 and Temperature1
  6. SampleC 55 80 Lab conditions Difference between Temperature2 and Temperature1
  7. SampleC 50 76 Inside Hello Difference between Temperature2 and Temperature1
  8. SampleD 48 77 Freezer Difference between Temperature2 and Temperature1
  9. SampleD 51 76 Freezer Difference between Temperature2 and Temperature1
  10. SampleD 52 75 Outsize Difference between Temperature2 and Temperature1
  11. SampleD 53 80 Lab Conditions Difference between Temperature2 and Temperature1

Example of desired outcome with SampleD:Example of workflow for SampleD

How to&Answers:

You can achieve the desired solution by using the formula SUMIF. I am explaining it in 2 steps with screenshots and I hope it will be of help to you as beginner in Excel, Formula and VBA.

We will use the below formula IN Cell E2 and FillDown:

=IFERROR((SUMIFS(C:C,A:A,A2,D:D,D2) -SUMIFS(B:B,A:A,A2,D:D,D2))/(SUMIFS(C:C,A:A,A2)-SUMIFS(B:B,A:A,A2)),"-")

to calculate the temperature difference over time at various location with in a sample and display it as percentage.

SUMIF Excel