vba functions question

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

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA Functions Question

VBA Functions Question

resolvedResolved · Medium Priority · Version 2007

Imran has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course
Excel Advanced - Formulas & Functions course
Excel Advanced - For Power Users course

VBA Functions Question

Hi,

I'm just practicing my VBA after yesterdays course, and I tried to execute the following code, and it seems to highlight the following text (the one where I have inserted arrows) and the error message I get is a "Argument not Optional"

Option Explicit
Sub Bold()

Range("d5").Select

Do Until ActiveCell.Offset(0, -2) = ""

If ActiveCell.Offset(0, -1) = 1 Then
ActiveCell.EntireRow.Font.Bold = True
End If

ActiveCell.Offset(1, 0).Select

Loop

Range("E5").Select
Do Until ActiveCell.Offset(0, -3) = ""
If ActiveCell.Offset(0, -2) = 1 Then
>>>>>Call ProfitMargin<<<<<<
End If

ActiveCell.Offset(1, 0).Select
Loop




End Sub

Function ProfitMargin(Sales As Byte)



ProfitMargin = ActiveCell.Offset(0, -1) * 0.5




End Function

RE: VBA Functions Question

Hi Imran

Thanks for getting in touch. Great to hear from you and good to see that you're practicing!

When you CALL your ProfitMargin function, it's expecting you to pass it an argument.

You then declare that you're going to use "Sales As Byte" but it's then not used in the function. You should rewrite two lines like this:

Call ProfitMargin(ActiveCell.Offset(0,-1))

and

ProfitMargin = Sales * 0.5

This is why the "argument is not optional". You need to send it a piece of data to calculate with, as specified in your function declaration.

I'd also guess that you are trying to return ProfitMargin to the ActiveCell. Hence I'd suggest one more modification to the CALL line:

ActiveCell = ProfitMargin(ActiveCell.Offset(0, -1))

Let me know if this isn't clear.

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

Edited on Fri 20 Dec 2013, 17:19

RE: VBA Functions Question

Hi Gary,

Thanks for that, I have now got the below set up on the after about an hour, and I don't get an error message, but I get zero values for the function. I used the Control Structures spreadsheet from the course. I looked in the Local window and it seems like the value field is empty, an I know we covered it in the course, but I just can't remember for the life of me figure out what it means! I tried what you said, but it did not seem to work. Can you adapt the code below and I will try comparing it with mine and try to figure it out from there.


Option Explicit
Dim Sales As Byte



Sub Bold()

Range("d5").Select

Do Until ActiveCell.Offset(0, -2) = ""

If ActiveCell.Offset(0, -1) = 1 Then
ActiveCell.EntireRow.Font.Bold = True
End If

ActiveCell.Offset(1, 0).Select

Loop

Range("E5").Select
Do Until ActiveCell.Offset(0, -3) = ""
If ActiveCell.Offset(0, -2) = 1 Then
ActiveCell = ProfitMargin(Sales)

End If

ActiveCell.Offset(1, 0).Select
Loop




End Sub

Function ProfitMargin(Sales As Byte)



ProfitMargin = Sales * 0.5




End Function

Regards

Imran

RE: VBA Functions Question

Don't worry about it Gary I've figured it out with the help of John Walkenbach's book that I had in my cupboard!

 

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:

Display Functions on Worksheets

Functions in Excel can be difficult to recall their format/syntax

For example, you want to use the =PMT function.

Enter =PMT, then select keystroke, CTRL+SHIFT+A.

This usful memory jog, will display the arguments of a function on a worksheet, allowing the user to proceed with the generation of the function



View all Excel hints and tips


Server loaded in 0.09 secs.