I have a GridView which I plan to add different colors in each row based on certain conditions.
One such condition is to color the row in a specific color if a number in column 6 is higher than the number in column 7.
If e.Row.Cells(6).Text > e.Row.Cells(7).Text Then
e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
Else
However, when I test the code it seems to only change the color when the left most value in column 6 is larger than column 7, regardless of the fact that column 6 is in the hundreds, while column 7 is in the thousands.
I believe the cause is coming from me using text in the If statement. However, any instance of number I can think to change doesn't seem to fit into the code:
If e.Row.Cells(6).Number > e.Row.Cells(7).Number Then
or
If e.Row.Cells(6).Integer > e.Row.Cells(7).Integer Then
These example gives me a message saying
'Number' is not a member of 'System.Web.UI.WebControls.TableCell'
What else can I use that is compatible with the current code?
I believe that you are missing a casting. I'm not sure if you're comparing only Integers or complex numbers, if you don't know I recommend you to cast directly to doubles:
If Convert.ToDouble(e.Row.Cells(6).Text) > Convert.ToDouble(e.Row.Cells(7).Text) Then
e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
Else
Convert.ToDouble will convert any text to number.
More info about casting:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number
https://www.dotnetheaven.com/article/casting-integer-to-long-single-and-double-using-vb.net
If you might have some empty values you can use the TryCast:
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator
Related
I have a data frame in R that I want to analyse. I want to know how many specific numbers are in a data frame column. for example, I want to know the frequency of number 0.9998558 by using
sum(deviation_multiple_regression_3cell_types_all_spots_all_intersection_genes_exclude_50_10dec_rowSums_not_0_for_moran_scaled[,3]== 0.9998558)
However, it seems that the decimal shown is not the actual one (it must be 0.9998558xxxxx) since the result I got from using the above command is 0 (the correct one should be 3468). How can I access that number without knowing the exact decimal numbers so that I get the correct answer? Please see the screenshot below.
The code below gives the number of occurrences in the column.
x <- 0.9998558
length(which(df$a==x))
If you are looking for numbers stating with 0.9998558, I think you can do it in two different ways: working with data as numeric or as character.
Let x be your variable:
Data as character
This way counts exactly what you are looking for
sum(substr(as.character(x),1,9)=="0.9998558")
Data as numeric
This will include all the values with a difference with the reference value lower than 1e-7; this may include values not starting exactly with 0.9998558
sum(abs(x-0.9998558)<1e-7)
You can also "truncate" the numbers in your vector and compare them with the number you want. Here, we write 10^7 because 7 is the number of decimals you want to compare.
sum(trunc(x*10^7)/10^7)==0.9998558)
I need to align text in an ALERT STRING column with the row identified by number in an ID ROW column.
Additionally, I need to also align the same ALERT STRING text with the same ID ROW number AND with the ID matching that embedded in a string in the TEXT WITH ID column. (This double-check will sometimes be necessary with the real-world data.)
So far, I've only figured out how to align the ALERT STRING with the ID matching that embedded in the TEXT WITH ID column:
=LOOKUP(2,1/SEARCH(A2,$F$2:$F$11),$G$2:$G$11)
I appreciate any help folks can offer. You can find an editable copy of the workbook here:
https://1drv.ms/x/s!ArQ7Kw6ayNMY2zktTW3pDCbMmJZ_
UPDATE: Nayan provided a solution to the first part of this question (please see answer below). I'm still trying to work out a formula for the column D part of this question, in which the row reference shown in column E is combined with a match of the ID shown in column A with its corresponding value in one of the text strings in column F.
The best I've been able to come up with so far is a formula with a high failure rate:
=INDEX($G$2:$G$11,MATCH(ROW(D2),$E$2:$E$11,MATCH("*"&A2&"*",$F$2:$F$11,0)))
Any help with this part of the question will be greatly appreciated.
ROW([reference])
Returns the row number of a reference
E.g.: Row(B2) returns 2. If nothing provided like ROW() will also
return row number based on position of cell where it is called.
VLOOKUP(loolup_value, table_array, col_index_num, [range_lookup])
Looks for a value in the leftmost column of a table, and then returns a value in the same row from a column you specify (col_index_num)
By default - the table must be sorted in an ascending order.
Try this:
=VLOOKUP(ROW(B2),$E$2:$G$11,3,FALSE)
INDEX(array, row_num, [column_num]) INDEX(reference, row_num,
[column_num], [area_num])
Returns a value or reference of the cell at the intersection of a particular row and column, in a given range.
In this case, you have to get row_num with MATCH function.
MATCH(lookup_value, lookup_array, [match_type])
Returns a relative position of an item in an array that matches a specified value in a specified order.
match_type: 1 (Less than), 0 (Exact match), -1 (Greater than)
Try this:
=INDEX($G$2:$G$11,MATCH(ROW(B2),$E$2:$E$11,0))
Identity Data with Multiple Criteria Condition using MATCH()
=INDEX($G$2:$G$11,MATCH(1, (ROW(D2) = $E$2:$E$11) * (ISNUMBER(SEARCH(A2, $F$2:$F$11))),0))
References:
https://exceljet.net/excel-functions/excel-vlookup-function
https://exceljet.net/excel-functions/excel-index-function
https://exceljet.net/formula/index-and-match-with-multiple-criteria
This is the formula I was looking for in column D:
=INDEX($G$2:$G$11,MATCH(ROW(D2)&"*"&A2&"*",INDEX($E$2:$E$11&$F$2:$F$11,),0))
You can see it working here.
Nayan provided a great deal of help with answering this question, so I will mark his answer as the accepted solution.
Syeda Fahima Nazreen provided the example I referenced to figure out the formula shown above.
Reference:
Nested Excel Formula with Two INDEX Functions and a MATCH Function with Multiple Criteria
I’m looking for a simple expression that puts a ‘1’ in column E if ‘SomeContent’ is contained in column D. I’m doing this in Azure ML Workbench through their Add Column (script) function. Here’s some examples they give.
row.ColumnA + row.ColumnB is the same as row["ColumnA"] + row["ColumnB"]
1 if row.ColumnA < 4 else 2
datetime.datetime.now()
float(row.ColumnA) / float(row.ColumnB - 1)
'Bad' if pd.isnull(row.ColumnA) else 'Good'
Any ideas on a 1 line script I could use for this? Thanks
Without really knowing what you want to look for in column 'D', I still think you can find all the information you need in the examples they give.
The script is being wrapped by a function that collects the value you calculate/provide and puts it in the new column. This assignment happens for each row individually. The value could be a static value, an arbitrary calculation, or it could be dependent on the values in the other columns for the specific row.
In the "Hint" section, you can see two different ways of obtaining the values from the other rows:
The current row is referenced using 'row' and then a column qualifier, for example row.colname or row['colname'].
In your case, you obtain the value for column 'D' either by row.D or row['D']
After that, all you need to do is come up with the specific logic for ensuring if 'SomeContent' is contained in column 'D' for that specific row. In your case, the '1 line script' would look something like this:
1 if [logic ensuring 'SomeContent' is contained in row.D] else 0
If you need help with the logic, you need to provide more specific examples.
You can read more in the Azure Machine Learning Documentation:
Sample of custom column transforms (Python)
Data Preparations Python extensions
Hope this helps
I have a spreadsheet called "MTM Lookup", this is downloaded from a specific site, in column D of this spreadsheet are values, but these values have a formula attached to them. The formula rounds these values to 0. The formulas don't round on a specific cell. They look like this =ROUND(35370.6708773751,0) or =ROUND(48368.0427161566,0). I need the values to come through with all the decimals or rounded to 10 decimals but cannot get this to happen, I can remove the formula and leave the value but it is rounded to zero. Please could anyone assist with some simple vba to either remove the =round(,0) or replace the 0 to 10 ie round(x,10).
I don't see any problem in the formula you provided.
When I put
=ROUND(35370.6708773751,0)
to a cell, I correctly see 35371 in the cell.
There are, however, two things in play here.
Formula
Cell Format
For example, when I enter the following value to the cell:
=ROUND(35370.6708773751,10)
I do see 35370.67088 as a result (after rounding to 10 places, cell format rounds it again to 5 decimal places)
I don't know why entering the value without any formula shows you 0, but this leads me to the same suspscion, i. e. that the problem is in the cell format.
You can check it by right clicking on the cell > Format Cells (in office 2010 at least) or programatically, using the following, for example:
sheets("MTM Lookup").range("A:1").numberformat = "0.0000000000"
Hope that it helps.
Tools: asp.net VB, SpreadsheetGear 2010
I need to read in Excel files from external users and I do not control the format. There are 3 sections of data. I am able to read in the header. The challenge is that the body details can contain an infinite number of rows, and sometimes those rows are blank. I can determine the footer row because there is a piece of text that is unique. Therefore I can perform the following:
Dim dFoot As SpreadsheetGear.IRange = worksheet.Cells("20:21").Rows
Dim rngDet As SpreadsheetGear.IRange
rngDet = dFoot.Find(what:="UNIQUE TEXT HERE",
lookIn:=SpreadsheetGear.FindLookIn.Values,
lookAt:=SpreadsheetGear.LookAt.Whole,
searchOrder:=SpreadsheetGear.SearchOrder.ByColumns,
searchDirection:=SpreadsheetGear.SearchDirection.Next,
matchCase:=False,
after:=dFoot)
Unfortunately, I am now stuck here. I cannot determine where the cell is (i.e. V79) or how to move 7 columns over and 3 down to get the first value for inserting. I have tried offset and different methods of reading the rngDet, but no luck. I have also tried rngDet.Activate() to read the active cell, but it does not work the same as Microsoft.
I appreciate any help you can provide.
Tim, Thank you for your questions. First time asking so I probably was not very clear. I did find the answer for myself today. I have a group of cells that I need to upload. The problem is that they will be moving up and down the page depending on how many rows were entered in the section above. I realized that I did know what column it was going to be in so I performed the following:
Dim x As Integer
Dim xFooter As Integer
Dim columnnumber As Integer = 21 'Column V
txtTest.Text = worksheet.Cells.RowCount
For x = 500 To 1 Step -1
If worksheet.Cells(x, columnnumber).Value = "TOTAL PRODUCT COST CALCULATION" Then
txtTest.Text = x
xFooter = x
Exit For
End If
Next x
I am now able to use my x value as the left hand corner of my group of cells. I know that the values go over 7 columns from here and down 20 rows which gives me my quandrant of values.
Thought I would share just in case anyone has the same problem.