Retrieving a comparison operator from cell - excel

I am trying to do a vba code for excel where I can retrieve the comparison operator(e.g. <, <= etc.) from the excel sheet. What I am trying to do give a score based on the value and the bands being key in.
I wanted to do something like this in the code:
Sample data:
cell A1 = 80(input)
cell A4 = "<"
cell B4 = 75
cell C4 = "="
cell D4 = 75
cell E4 = ">"
cell F4 = 75
Example of the code I wanted to do:
dim score as integer
dim result as integer
score = range("A1").value
methodoperatorb1 = range("A4").value
methodoperatorb2 = range("C4").value
methodoperatorb3 = range("E4").value
band1 = range("B4").value
band2 = range("D4").value
band3 = range("F4").value
if score (methodoperator1)(band1) then result = 1
elseif score (methodoperator2)(band2) then result = 2
else result = 3
Sorry for the bad example and really hope someone can help me with this problem.

You could use Evaluate to evaluate the expressions like this:
Sub foo()
Dim score As Integer
score = Range("A1").Value
methodoperatorb1 = Range("A4").Value
methodoperatorb2 = Range("C4").Value
methodoperatorb3 = Range("E4").Value
band1 = Range("B4").Value
band2 = Range("D4").Value
band3 = Range("F4").Value
Dim result As Integer
If Application.Evaluate(score & methodoperatorb1 & band1) Then
result = 1
ElseIf Application.Evaluate(score & methodoperatorb2 & band2) Then
result = 2
Else
result = 3
End If
MsgBox result
End Sub
Note that this will only work if the total length of the expression is under 256 characters.

Related

Excel Not Responding after trying to run code to copy data and send an email

Basically I have 7 cells that could be populated with text (b2, b4, b6, b8, b10, b12 and b14). I want to the code to check each of the cells to see if they have a value and send only the cells that do have a value in an email. For formatting purposes the cells pasted into the email need to have one empty cell in between and the cells need to be kept in the order they are in originally, just without the unnecessary blank cells.
I've never officially learned VBA I've only taught myself on a case by case scenario so there could be an easy solution that I'm missing. Often I can debug and find the problem but in this case Excel completely freezes and turns "Not Responding". I have a feeling that means I've got a loop somewhere unresolved but I don't really understand how. The code -seems- to run up until Range("A2").Value = Line(LineCount1). Any suggestions would be appreciated.
Public Sub SingleEmail()
Dim LineCount1 As Integer
Dim LineCount2 As Integer
Dim LineCount3 As Integer
Dim LineCount4 As Integer
Dim LineCount5 As Integer
Dim LineCount6 As Integer
Dim LineCount7 As Integer
Dim NumOfLines As Integer
Range("A2", "A14").ClearContents
LineCount1 = 2
Range("A2").Value = Line(LineCount1)
LineCount2 = 2 + LineCount1
Range("A4").Value = Line(LineCount2)
LineCount3 = 2 + LineCount2
Range("A6").Value = Line(LineCount3)
LineCount4 = 2 + LineCount3
Range("A8").Value = Line(LineCount4)
LineCount5 = 2 + LineCount4
Range("A10").Value = Line(LineCount5)
LineCount6 = 2 + LineCount5
Range("A12").Value = Line(LineCount6)
LineCount7 = 2 + LineCount6
Range("A14").Value = Line(LineCount7)
NumOfLines = Range("n3").Value
If Range("A2") <> "" Then
Range("A2", "A" & NumOfLines).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "personalemailaddress#Someplace.com"
.Item.CC = ""
.Item.Subject = "Email Subject"
.Item.send
End With
End If
End Sub
Function Line(ByRef LineCount As Integer) As String
Line = ""
Do While Line = "" Or LineCount < 13
If Range("B" & LineCount).Value <> "" Then
Line = Range("B" & LineCount).Value
Else
LineCount = LineCount + 2
End If
Loop
End Function
To answer your question:
If B4 has value and B2 is blank then this While loop become infinite. the LineCount is Stuck on 4, hence no overflow. That's why your code freezes.
Why are you running a loop in the first place. You can simply assign the values like this Range("A2:A14").Value =Range("B2:B14").Value
As per your comment, you need to use And operator in place of OR
Do While Line = "" And LineCount < 13 now the loop will exit if line <> "" or LineCount > 14

How to define variable in range

I just started VBA coding and I am struck here:
For one cell this program works:
Dim score As Integer, result As String
score = Range("A1").Value
If score >= 60 Then
result = "pass"
Else
result = "fail"
End If
Range("B1").Value = result
And how about a column of cells? Can loop works for this?
My code using loop - But How to define variable in range?
Dim score As Integer, result As String, I As Integer
score = Range("AI").Value
For I = 1 To 6
If score >= 60 Then
result = "pass"
Else
result = "fail"
End If
Range("BI").Value = result
Next I
Thanks in advance!
Almost, you just need to use string concatenation (&)
Dim score As Integer, result As String, I As Integer
'score = Range("AI").Value
For I = 1 To 6
score = Range("A" & I).Value '// Needs to be inside the loop to update.
If score >= 60 Then
result = "pass"
Else
result = "fail"
End If
Range("B" & I).Value = result
Next I
This can also be written as:
For i = 1 To 6
Range("B" & i).Value = IIf(Range("A" & i).Value >= 60, "pass", "fail")
Next
you can also go with a "formula" approach:
Range("B1:B6").FormulaR1C1 = "=If(RC1 >= 60, ""pass"", ""fail"")"
thus maintaining that check active for any possible subsequent change in columns A cells values
or, should you want to have "static" values only:
With Range("B1:B100")
.FormulaR1C1 = "=If(RC1 >= 60, ""pass"", ""fail"")"
.Value = .Value
End With

Excel how to find real random data set from mean and standard deviation?

In Excel it is easy to find the mean and standard deviation given 10 single data points. But is it possible to generate 10 random data points in Excel given the mean and standard devation?
In Excel I can right click "View Code" and access a macro to use macro coding, but how would I go about coding this?
Sub rand()
Range("B4").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B4").Calculate
Range("B4").Value = Range("B4").Value
Range("B5").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B5").Calculate
Range("B5").Value = Range("B5").Value
Range("B6").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B6").Calculate
Range("B6").Value = Range("B6").Value
Range("B7").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B7").Calculate
Range("B7").Value = Range("B7").Value
Range("B8").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B8").Calculate
Range("B8").Value = Range("B8").Value
Range("B9").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B9").Calculate
Range("B9").Value = Range("B9").Value
Range("B10").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B10").Calculate
Range("B10").Value = Range("B10").Value
Range("B11").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B11").Calculate
Range("B11").Value = Range("B11").Value
Range("B12").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B12").Calculate
Range("B12").Value = Range("B12").Value
Range("B13").Formula = "=NORMINV(RAND(),$B$14,$B$15)"
Range("B13").Calculate
Range("B13").Value = Range("B13").Value
End Sub
This is my random data generated based on mean and std dev, then i will use this data to find mean and std dev, if the mean and std dev did not mean the value i want then redo until it correct, so what code i need to add to make it redo?
Without VBA:
Say we want a set of values with a mean of 7 and a std-dev of .005
In A1 through A20 enter:
=RAND()
this is the data we must transform to meet our requirements. In B1 enter:
=A1*0.005/STDEVP(A:A)
and copy down through B20. The values in column B will have the desired std-dev.
In C1 enter:
=B1+7-AVERAGE(B:B)
and copy down through C20.
Column C will have both the correct std-dev and correct mean:
With VBA:
Perform exactly the same technique, but use arrays rather than columns of cells. It is equally easy to generate sample data for Poisson, LogNormal, etc. distributions. Here is some simple code to put 20 values in column G:
Sub ForcedDistribution()
Dim ary(1 To 20) As Double
Dim bry(1 To 20) As Double
Dim cry(1 To 20) As Double
Dim mean As Double, std As Double
Dim i As Long, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Randomize
mean = 7#
std = 0.005
For i = 1 To 20
ary(i) = Rnd
Next i
For i = 1 To 20
bry(i) = ary(i) * std / wf.StDevP(ary)
Next i
For i = 1 To 20
cry(i) = bry(i) - wf.Average(bry) + mean
Next i
For i = 1 To 20
Cells(i, 7).Value = cry(i)
Next i
End Sub

Excel vba how to repeat an if then else condition on rows

I want to repeat my 'if then else' condition on the 30 rows that follow.
This is my code (I am new to this).
Dim score As Range, result As Double
If Range("A2") = "2a" Then
Range("C2") = 20
Else
If Range("A2") = "3c" Then
Range("C2") = 23
So at the moment when I enter 2a / 3c in cell A2, the number 20 / 23 comes up in cell C2.
I would like the same thing to happen in row 3, 4, 5 ... 30. When I enter 2a / 3c in cell A5, the number 20 / 23 comes up in cell C5.
Is it possible with a loop or another type of code? Or will I have to copy this over and over for each row?
Any help is really appreciated.
Thanks!
Here's a basic for-next loop that would accomplish what you ask...
For i = 2 To 30
If Range("A" & i) = "2a" Then
Range("C" & i) = 20
End If
If Range("A" & i) = "3c" Then
Range("C" & i) = 23
End If
Next
Here's a tutorial I picked at random from a Google search.
Try this as well
Dim score As Range, c As Range, x
Set score = Range("A2:A32").SpecialCells(xlCellTypeConstants, 23)
For Each c In score.Cells
x = IIf(c = "2a", 20, IIf(c = "3c", 23, ""))
c.Offset(0, 2) = x
Next c
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target,range("A2:A32")) is Nothing then target.offset(,2)=iif(instr("2a3a",target)>0,20+3*abs(target="3c"),"")
End sub

Using range-variable to all selected rows

Below is working VB, but I want to set variable D5:D222, F5:F222 and I5:I222 and to see result in G5:G222.
Below is for single row, but I need it to run for all selected rows. I tried all but I don't know how to set it.
Dim score As Integer, result As String
D5 = Range("D5").Value
F5 = Range("F5").Value
I5 = Range("I5").Value
If D5 = 1 And F5 = 1 Then
result = "=I5*0.67"
End If
If D5 = 1 And F5 = 0 Then
result = "=I5"
End If
If D5 = 0 Then
result = "0"
End If
Range("G5").Value = result
If solution with formulas is OK, you can enter in G5:
=D5*I5*(1-0.33*F5)
and fill it down to row 222
If you want VBA code to calculate the value, you can use:
Dim i as Integer
For i = 5 to 222
Range("G" & i).Value = _
(Range("I" & i).Value*(1-0.33)*Range("F" & i).Value)*Range("D" & i).Value
Next i
If you want VBA code to automatically fill the formula in each cell:
Range("G5:G222").FormulaR1C1 = "=RC[-3]*RC[2]*(1-0.33*RC[-1])"

Resources