Solved

Excel calculation using VBA

Posted on 2016-09-18
10
40 Views
Last Modified: 2016-10-08
I want to add two cells based on the text in two other cells.  

I have this now that works in the VBA editor.  

Function CalcQtys()
Dim intPrev, intNew
Dim strType, strLocation

intPrev = Range("C4").Value
intNew = Range("F3").Value

strType = Range("L3").Value
strLocation = Range("N3").Value

If (strType = "Adj-In") And (strLocation = "Transfer from Final Inspect") Then
    CalcQtys = intPrev + intNew
End If

End Function

In the spreadsheet cell formula I have this: = CalcQtys().
But the cell displays #VALUE!.

This will end up a multi-level if else-if statement.  I'm new at Excel ...

Thanks,
Brooks
0
Comment
Question by:gbnorton
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41803783
Why use VBA for calculations? Excel's inbuilt calculations will be more efficient than coding.

Provide an example workbook
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41803788
The function you posted should work without an issue though it doesn't seems to be useful as this can be achieved with simple IF formula on the sheet.
What's your intention behind this? What are you trying to achieve?
Also what do you mean by multiple If else statement here, how would you incorporate them in the function.
I guess you need a sub-routine rather than a Function.
Better upload a sample workbook with some dummy data and lets know the steps you are trying to automate.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 41803809
If you want to improve what you have then change it to this.

'  Without the "As Integer", the function returns a Variant, which will work
'  but Variants are larger and slower than any other type of variable.
Function CalcQtys() As Integer

' Unless you explicitly declare a type, the variables are Variants
' as described above.
Dim intPrev As Integer, intNew As Integer
Dim strType As String, strLocation As String

intPrev = Range("C4").Value
intNew = Range("F3").Value

strType = Range("L3").Value
strLocation = Range("N3").Value

If (strType = "Adj-In") And (strLocation = "Transfer from Final Inspect") Then
    CalcQtys = intPrev + intNew
End If

' You don't really need any of your variables. Instead of the above you could do 
' the following instead of lines 5 through 18
If (Range("L3").Value = "Adj-In") And (Range("N3").Value = "Transfer from Final Inspect") Then
    CalcQtys =  Range("C4").Value + Range("F3").Value
End If

End Function

Open in new window

0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41803823
I still cannot see the advantage of VBA here, especially as the ranges are hard coded.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 41803838
I still cannot see the advantage of VBA here, especially as the ranges are hard coded.
I agree (unless there's more to it) and I was just trying point out a few coding improvements.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41804293
martin, I'm not faulting your code, I just think it is totally unnecessary. So many people seem to think VBA is the answer to all problems.
0
 

Author Comment

by:gbnorton
ID: 41804727
Thanks for your comments.  At this point, I'm just trying to get some VBA to work.  I've seen that VBA is not the way to go in this case.  I just want to learn how to use it.  The example is uploaded.  The function is in cell C3.  My goal is to add F3 and C4 and display the result in C3.
Thanks,
Brooks
Excel-vba.xlsm
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41804763
It seems that you need the formulas for C3, C5 and C7 based on the conditions for columns L and N. Right?
So as per your description if C3 = F3 + C4, what formula do you want for C5 and C7 then?
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points (awarded by participants)
ID: 41804797
Function CalcQtys() As Integer
Dim lngCurRow As Long
lngCurRow = Application.Caller.Row
If (Range("L" & lngCurRow).Value = "Adj-In") And (Range("N" & lngCurRow).Value = "Transfer from Final Inspect") Then
    CalcQtys = Range("C" & lngCurRow + 1).Value + Range("F" & lngCurRow).Value
End If
End Function

Open in new window

0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41834898
The OP insists that he wants VBA so Martin's final Function does as asked.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now