VBA how to stop the formula based on other cell

Sallylwy

New Member
Joined
Jul 30, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi , I would like to seek for some advice. I want to write the sumif formula VBA code on column C and stop copying the formula while the cell of column A is blank.
Do I have to set the Lastrow in the VBA code? Do I need to set the reference cell (A2)? Thanksss
1627702665046.png
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Wrap the SUMIF in an IF Statement.

=IF(A2 = "", "", SUMIF($F$2.....
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Sallylwy()
   Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=sumif($F$2:$F$7,A2,$H$2:$H$7)"
End Sub
 
Upvote 0
Hi, thanks for your advise.
The code works for me. But if i add something on cell A8, can I just stop the sumif formula in cell C5 only? How i adjust the code? very new to vba, get a lot questions :(
1627802483194.png
 
Upvote 0
I assume you want to use VBA as a practice exercise.

This is easily catered without VBA using tables.

20210801 Sumifs Table or VBA.xlsm
ABCDEFGHI
1NameDepartment# of HolidaysManagerEmployeeHoliday
2PeterDM4PeterKay2
3BettyMO9BettyTerry4
4JohnSales3JohnAnna3
5DarronSales1DarronAmber1
6PeterOdebe2
7BettyJack5
8Top2
9Betty
10Peter
11
Data Tables
Cell Formulas
RangeFormula
C2:C5C2=SUMIFS(tbl_HolDetail[Holiday],tbl_HolDetail[Manager],[@Name])
 
Upvote 0
How about
VBA Code:
Sub Sallylwy()
   If Range("A2").Value = "" Then Exit Sub
   Range("C2:C" & Range("A1").End(xlDown).Row).Formula = "=sumif($F$2:$F$7,A2,$H$2:$H$7)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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