98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA Functions Question
VBA Functions Question
Resolved · 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
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
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
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Display Functions on WorksheetsFunctions in Excel can be difficult to recall their format/syntax |