Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

including count result into

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Including a count result into a subtraction formula

Including a count result into a subtraction formula

ResolvedVersion 2010

David has attended:
Access Introduction course
Excel VBA Intro Intermediate course

Including a count result into a subtraction formula

Here is my challenge:

Range E20:P20 in an Excel Spreadshet calculates the difference between Sales Forecast values shown in Range E15:P15 and Sales business plan values shown in Range E5:P5 . The formula applied in range E20:P20 is as follows:

ActiveCell.FormulaR1C1 = "=R[-5]C-R[-15]C

Since I have to add new lines into the spreadsheet each month with new forecast data, the number of lines between forecast and business plan is increasing.

My idea to create a flexible deviation formula is to use a count operation by running a loop to establish the number of lines between the business plan line and the forecast line that will move down each month.

I have been doing fine counting the difference using marks in column A (P for the business plan line and x for the deviation line) but now I do not know how to correctly include the result of the counting into the subtraction formula (see last line before "End Sub" line for my current attempt).

Perhaps it will be easier if I can send you the file. Please provide me an email address for this if possible.


The Macro code:

Sub DeviationFormulaAdjustment()

ActiveCell.Select
Dim Count As Long
Count = 0
Do
Count = Count - 1
ActiveCell.Offset(-1, 0).Select
Loop Until ActiveCell = "P"

Do Until ActiveCell = "x"
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(0, 4).Select
ActiveCell.FormulaR1C1 = "=R[-5]C-R[Count]C"

End Sub

RE: Including a count result into a subtraction formula

Hi David

Thanks for getting in touch. Your code is very close, and you need to just let Excel know that part of your formula is a variable and not to be taken literally.

On a side note, I'd be wary about using the variable name "Count" as it is a reserved expression in VBA - I'm amazed it's let you use it at all! I recommend you change it to RowCount or similar to avoid other errors.

Modify your last line:

ActiveCell.FormulaR1C1 = "=R[-5]C-R[" & RowCount & "]C"

This will concatenate the variable into the formula.

However I also offer a suggestion as an alternative method for determining the amount of rows in a list. You can replace your loop with:

RowCount = ActiveCell.CurrentRegion.Rows.Count

This will figure out how many rows are in the current table. It will get thrown off by entirely empty rows however. You may also need to subtract 1 if your data contains a header row.

I hope this helps. Please let us know if you have any further questions.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Including a count result into a subtraction formula

Dear Gary,

Thanks for your quick reply! It dit work with your suggested line

ActiveCell.FormulaR1C1 = "=R[-5]C-R[" & RowCount & "]C"

as long as I did not change the variable name "Count" to to "RowCount" but instead kept is as "Count". So my working line reads as:

ActiveCell.FormulaR1C1 = "=R[-5]C-R[" & Count & "]C"

Once I my variable is changed to "RowCount" the count wrongly and indicates -1 instead of -15. I believe I made a mistake perhaps you can spot it:


Sub DeviationFormulaAdjustment()

ActiveCell.Select

Dim RowCount As Long

RowCount = 0
Do
RowCount = Count - 1
ActiveCell.Offset(-1, 0).Select
Loop Until ActiveCell = "P"

Do Until ActiveCell = "x"
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(0, 4).Select
ActiveCell.FormulaR1C1 = "=R[-5]C-R[" & RowCount & "]C"

End Sub


Thanks as well for the alternative method.

RE: Including a count result into a subtraction formula

Hi David

Thanks for the reply. You need to make sure all instances of the variable name has been changed:

RowCount = Count - 1

should be

RowCount = RowCount - 1

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Including a count result into a subtraction formula

...perfect - working now as needed!

This will save me a lot of time once I applied this code for similar formulas I have in my file.

Thanks for your great support!

Kind Regards,
David

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Hiding a worksheet in Excel

Want to circulate an Excel workbook file but keep certain worksheets hidden from your colleagues' view?

You can do so by bringing up the sheet you wish to hide on your screen; then going to Format - Sheet - Hide.

It will not be immediately obvious that a sheet is hidden from view unless perhaps the sheet are still labelled Sheet 1, Sheet 2 etc.

To display the sheet again, you can go to Format - Sheet - Unhide on any of the other sheets in the workbook. A dialogue box will appear, allowing you to select the hidden sheet/s. Click OK to make the sheet/s reappear again.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.