?
Solved

defining and comparing the value of a cell in VB

Posted on 2012-03-12
1
Medium Priority
?
153 Views
Last Modified: 2012-03-13
I have the following statement in VB;

PROD = Cells(stateRow, 2).Value

I am trying to compare the contents of this cell (which changes relative to the row)
The length of the contents is variable.

I would like to compare the contents of this cell to another cell (PRODX)  on a spreadsheet to use in a sumifs statement in VB.

The challenge is the the value of PRODX can be the value of PROD  & "*"  

Can someone tell me how to code a sumif in VB comparing PROD to PROD X.

This is an example of what I am using when I am matching two fields that are the same length

totYr = Round(Application.WorksheetFunction.SumIfs(Range("pAuths"), _
                              Range("pEvent"), evtType, _
                              Range("pYr"), Pyr, _
                              Range("ProdX"), PROD) / Members, 2)
0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
Frank White earned 2000 total points
ID: 37712056
So if I understand correctly, what you want is a function that returns the sum of the values in a range, but only counting values that have a corresponding cell equal to  PROD or PRODX?

If so, why not simply use "If (.value = PROD) Or (.value = PRODX) Then _(...)" to compare?

If instead, what you meant was that you want to compare PROD against PRODX, but also validate if PRODX = PROD & "*" and add that number even if there's an offending asterisk, that's also very simple.

To ignore the ending asterisk, simply add a condition similar to: If (PRODX = PROD) Or (PRODX = PROD & "*") Then _(...)

If VB complains or fails to recognize the second comparison, simply assign a temporary local variable in the function and let it = PROD & "*", and compare PRODX to it.

I'd also take the opportunity to point out that calls to WorksheetFunction methods are much slower than retrieving the values once, doing the comparison in full VBA code, and then setting cell values or returning if you're using a UDF.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

800 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