Coding string to numeric in Excel - excel

I want to convert string codes to integer codes
Here is my Excel file
Level1 Level2 Level3 Level4 Time1 Time2 Time3 Time4
A B E C 12 14 12 13
D D E C 11 17 16 9
E C C B 14 13 19 8
C E B D 11 12 10 7
B A A D 10 11 7 6
A= 4, B=3, C=2, D=1 , E= 0
get this in sheet2
Level1 Level2 Level3 Level4 Time1 Time2 Time3 Time4
4 3 0 2 12 14 12 13
1 1 0 2 11 17 16 9
0 2 2 3 14 13 19 8
2 0 3 1 11 12 10 7
3 4 4 1 10 11 7 6
Is it possible?

Use the ASCII table and some basic maths.
dim i as long
for i=65 to 69
worksheets("sheet1").cells.replace what:=chr(i), replacement:=(69-i)
next i

This isn't as fast as a dictionary, but its clocking at 1 second for 10,000 rows across 4 columns.
Searching in columns A - D down to last row (determined by Col A)
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim SearchRange As Range: Set SearchRange = ws.Range("A2:D" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
Application.ScreenUpdating = False
SearchRange.Replace What:="A", Replacement:=4, LookAt:=xlWhole
SearchRange.Replace What:="B", Replacement:=3, LookAt:=xlWhole
SearchRange.Replace What:="C", Replacement:=2, LookAt:=xlWhole
SearchRange.Replace What:="D", Replacement:=1, LookAt:=xlWhole
SearchRange.Replace What:="E", Replacement:=0, LookAt:=xlWhole
Application.ScreenUpdating = True

Set up an area of any worksheet (for example Sheet2) with your chosen mapping of letters to numbers, like this:
+--------+-------+
| Letter | Value |
+--------+-------+
| A | 4 |
| B | 3 |
| C | 2 |
| D | 1 |
| E | 0 |
+--------+-------+
Then, suppose on one of your main worksheets you have a letter in cell B3="D". You can enter a formula into a different cell, perhaps cell C3, with your lookup formula: =VLOOKUP(B3,Sheet2!A2:B10,2,FALSE). (The range A2:B10 in this case would be replaced with the range of your entire letter-to-number table). The formula will return the number value.

Enter this in Sheet2!A2
=IF(ISNUMBER(Sheet1!A2),Sheet1!A2,69-CODE(Sheet1!A2))
Fill over/down as needed.

Related

Relabeling chart series collections

I have written code to plot a graph which is supposed to show one name for each collection.
There are 10 ~ 15 collections so having names for every item in the chart gets messy.
My Excel looks like this in intake port parameter sheet and the graph is plotted between v1 and v2. a is the variable name and case is the case of the variable.
I want all the points to group near one place and have one "a" or "b" as a caption for entire group depending on the group
name case V1 V2
a 1 10 11
a 2 12 11
a 3 12 12
a 4 12 11
a 5 12 12
b 1 15 12
b 2 10 10
b 3 15 11
b 4 12 15
b 5 19 12
C 1 01 02
C 2 02 01
C 3 02 11
....
For j = 2 To i
a = 4 + (j - 1) * 6
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(j).Name = "=INTAKE_PORT_PARAMETER!C" & a
ActiveChart.SeriesCollection(j).XValues = "=INTAKE_PORT_PARAMETER!Z" & a & ":Z" & a + 5
ActiveChart.SeriesCollection(j).Values = "=INTAKE_PORT_PARAMETER!AA" & a & ":AA" & a + 5
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(j).ApplyDataLabels
ActiveChart.SeriesCollection(j).DataLabels.Item(1).Select
Selection.ShowSeriesName = True
Selection.ShowValue = False
Selection.Format.TextFrame2.TextRange.Font.Size = 8
ActiveChart.SeriesCollection(j).DataLabels.Select
Selection.ShowValue = False
Next j
When run manually (by clicking F8) the code is working.
The output dots in the graph looks as I want.
| ..(a)
| .. **(b)
|
| ## (C)
|____________________
When run using F5 instead of step by step, I get one value for each collection and the name of the collection is not visible.
| ..(10)
| .. **(15)
|
| ## (01)
|____________________
I couldn't post the pictures of the graphs. My company treats those as confidential data.
Your question is unclear in several ways.
Are V1 and V2 your X and Y values? If so, it's easy with a few formulas. The formula in cell E2, filled into E2:G14, is
=IF(E$1=$A2,$D2,NA())
Select C1:C14, then hold Ctrl while selecting E1:G14, and insert your scatter chart.

Using IF and AND function

I am trying to use the IF and AND function in excel for values in two different cells. I have 25 conditions.
Below is the formula I've created but it keeps on saying there's an error.
IF(AND(A10=“A”,B10=1),11,IF(AND(A=“A”,B10=2),16,IF(AND(A10=“A”,B10=3),20,IF(AND(A10=“A”,B10=4),23,IF(AND(A10=“A”,B10=5),25,IF(AND(A10=“B”,B10=1),7,IF(AND(A10=“B”,B10=2),12,IF(AND(A10=“B”,B10=3),17,IF(AND(A10=“B”,B10=4),21,IF(AND(A10=“B”,B10=5),24,IF(AND(A10=“C”,B10=1),4,IF(AND(A10=“C”,B10=2),8,IF(AND(A10=“C”,B10=3),13,IF(AND(A10=“C”,B10=4),18,IF(AND(A10=“C”,B10=5),22,IF(AND(A10=“D”,B10=1),2,IF(AND(A10=“D”,B10=2),5,IF(AND(A10=“D”,B10=3),9,IF(AND(A10=“D”,B10=4),14,IF(AND(A10=“D”,B10=5),19,IF(AND(A10=“E”,B10=1),1,IF(AND(A10=“E”,B10=2),3,IF(AND(A10=“E”,B10=3),6,IF(AND(A10=“E”,B10=4),10,15))))))))))))))))))))))))))))))))))))))))))))))))
I expected the output to be, for example; if cell1 is "A" and cell2 is 1 the result should be 11.
I would highly advise a lookup table. Simply have all of your options listed out with their desired results and find them with a criteria search, such as the use of sumifs function.
For example, if you paste J1:L25 your possibilities:
A 1 11
A 2 16
A 3 20
A 4 23
A 5 25
B 1 7
B 2 12
B 3 17
B 4 21
B 5 24
C 1 4
C 2 8
C 3 13
C 4 18
C 5 22
D 1 2
D 2 5
D 3 9
D 4 14
D 5 19
E 1 1
E 2 3
E 3 6
E 4 10
E 5 15
You can then place the formula =SUMIFS($L$1:$L$25,$J$1:$J$25,$A$10,$K$1:$K$25,$B$10) to return your desired value.
That is, =SUMIFS(range_of_results, criteria_range_of_A-E, A10, criteria_range_of_1-5, B10)

Match #VALUE Error?

I have two worksheets. I want to search through sheet 1 to look for the id in sheet 2, find where it matches ID and the flag, and then return the price.
I have the following formula that gives me a #VALUE error:
Column B, sheet 2
=MATCH(Sheet2!A2&"B",Sheet1!A:A&Sheet1!B:B,0)
Sheet1:
ID FLAG PRICE
-- ---- -----
1 A 12
1 B 11
2 A 10
2 B 10
3 A 12
3 B 13
4 A 14
4 B 11
5 A 12
5 B 13
Sheet2:
ID B C
-- -- --
1
2
3
4
5
Any Ideas?
Try:
=INDEX(Sheet1!$C:$C,MATCH($A2,Sheet1!$A:$A,0)+1)

Get a Sum of ONLY duplicate values

If there a way to use a sumif combined with a countif?
for example if I have
TransactionNo|NumberToSum
1 |
1 | 9
2 | 6
3 | 3
4 |
4 | 4
5 | 6
6 | 4
7 |
7 | 9
8 | 7
9 | 4
10 |
10 | 1
11 | 3
12 | 6
My result would be: 23
because the values 1,4,7,10 are duplicated, so I need to sum the NumberToSum of those 9,4,9,1 in this case.
Ideally I would like somethings along the lines of =SumIf(A:A,Count>1,B:B)
Where B is summed if the count of A inside of A:A is > 1.
but =SUMIF(A:A,COUNT(A:A)>1,B:B) does not work.
NOTE: the Data is NOT sorted as it appears in my example.
If I wanted to do it in VBA one quick way could be:
Sub sumdups()
Dim rng As Range
Dim rng2 As Range
Dim Sum As Long
Set rng = [A2:A17]
For Each rng2 In rng
If WorksheetFunction.CountIf(Range("A1:A17"), rng2.Value) > 1 Then
Sum = Sum + rng2.Offset(0, 1).Value
End If
Next rng2
End Sub
Another option I have tried is to add a column with the formula
=IF(COUNTIF(A:A,A2)>1,B2,"")
Then sum the helper column, but I am hoping there is a simple formula that can do the same all in one cell.
I am really just looking for a faster formula that can be entered in a single cell, without the need for any additional calculations.
This formula works for me:
=SUMPRODUCT(1*(COUNTIF(A1:A100,A1:A100)>1),(B1:B100))
or for entire column (a little bit slower):
=SUMPRODUCT(1*(COUNTIF(A:A,A:A)>1),(B:B))

Excel to calculate if values in ranges meet certain criteria using VBA

I have two ranges in excel, say:
x | y
------------
5 | -1
46 | -4
2 | 1
67 | -1
22 | 1
6 | 0
34 | 0
7 | -2
I want calculate the sum of the second column for values less than O only if the respective values in the first column is less than 10 (i.e sum(y(i) for i<0 and x(i)<10) . Hence in this case the sum will be -3.
Assuming your headers are in A1:B1 and your data is A2:B9 use this:
=SUMIFS(B2:B9,A2:A9,"<10",B2:B9,"<0")
Try something like
Function calc() AS Integer
Dim sum AS Integer: sum = 0
Dim c AS Range
For Each c In ThisWorkbook.Worksheets(1).Range("A1:A15")
If c.Value < 10 And c.Offset(0, 1).Value < 0 Then
sum = sum + c.Offset(0, 1).Value
End If
Next c
calc = sum
End Function

Resources