Reference another cell in a different row having calculated the minimum value in a datatable column - vb.net

Reference another cell in the same row having calculated the minimum value in a datatable column
The above answered my question as to how to reference another cell in the same row having calculated the minimum value in a datatable column. But how do I reference a cell in another row still using that original cell?
In other words, if the min value in a column is in row 5, I now know how to get the values of cells in all other columns within row 5, but how do I get the values from columns within other rows (eg row 6) using the original cell as a starting point?
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)
was the answer to my first question, how would I amend this to get to another row? I tried:
dim column2 = Data(1)(1)
but it returns an array error. I think the numbers in brackets are absolute, but I need to use relative ones.
Thanks in advance again.

Related

Reference another cell in the same row having calculated the minimum value in a datatable column

Using VB, I've used the following line to successfully find the minimum value in a specific column (say column 5, where the values are all of double) in a datatable:
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
I would now like to refer to the values in other columns (let's say column 2) along the row containing that minimum column value.
Any help would be much appreciated as I can't get my head round it!
Thanks
You can use the DataTable.Select() method to get the row(s) that contain the minimum value. DataTable.Select() returns a DataRow(). In the code below, I assumed only one column contains the minimum value hence Data(0).
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)
All you have is a value but you currently have no idea what row(s) contain that value. You can use the table's Select method to get the row(s) that contain that value in that column. Once you have the row(s), you can do whatever you want with it/them:
Dim minValue = CDbl(myDataTable.Compute("MIN(MyColumn)", Nothing))
Dim rows = myDataTable.Select($"MyColumn = {minValue}")
For Each row In rows
'Use row here.
Next
Select always returns an array, even if there is only one row, so the loop will always work. If you know that there will never be more than one match, you can just get the first element directly.

Adding Rows in DataGridView

I have a DGV that would change in columns, how would i modify the code to dynamically add all the cells what are present in the row, instead of hardcoding each cell - i could see a problem if i added 31 cells and then a month with 28 would throw an error. I'd miss the first column becuase thats a name, but all the rest are numbers.
For Each SelectedRow as DataGridViewRow in dgv_service_centers.SelectedRows()
Total = SelectedRow.Cells(1).value + SelectedRow.Cells(2).value + SelectedRow Cells(3).value
Next
Cheers,
Pete
LINQ is your friend in this case.
Dim sum = dgv_service_centers.SelectedRows.
Cast(Of DataGridViewRow)().
Sum(Function(row) row.Cells.
Cast(Of DataGridViewCell)().
Sum(Function(cell) CInt(cell.Value)))
The inner expression sums the Integer values from each cell in the current row and the outer expression sums all those sums for the selected rows. If the values are some type other than Integer, replace the Cint as appropriate.

how to combine cell vertically in excel

It might be the most silly question in planet. how can I merge two cell values vertically in a repeated manner. as like this:
Column A and B has 400+ cells therefore it is impossible to do what I want to achieve manually.
Note: I want to merge B into A.
You can create a simple loop in VBA that runs through each cell in the data range then adds it to the output column
Sub Merge()
Dim data As Range
Dim cell As Range
Dim output As Range
Dim i As Integer
Set data = Range("A2:B4")
Set output = Range("D2")
i = 0
For Each cell In data
output.Offset(i, 0) = cell
i = i + 1
Next
End Sub
You can use the INDEX function to refer to each cell. If data is in A2:B4, this formula works in any column but must start in row 2 and can then be filled down:
=INDEX($A$2:$B$4,ROW()/2,MOD(ROW(),2)+1)
The formula uses the current row as a counter. On every even row it gets a value from the first column of data and on every odd row it gets a value from the second column of data. After every 2 rows it gets values from the next row of data.

Select cell from column with row value

This simple problem has caused me some recent issues. I have a range of cells which are columns that hold onto different types of information. Using a row value (Integer not Range) which is previously determined I am looking to perform a check with the values within a single cell.
For example, I look through a list of names in column A. If the name is found it holds onto the Row value. Let's assume that the row is 10. This row value will be used in checking the column values for this row (I.e. C10, J10, and K10). How can I select a single cell and then compare the values within those cells?
To get the equivalent to MATCH() / INDEX() or VLOOKUP() in VBA for getting the data for Darth Vader here:
we could use something like:
Sub GetTheRowValue()
Dim RowValue As Long
RowValue = Range("A:A").Find(What:="Darth Vader", After:=Range("A1")).Row
MsgBox Range("B" & RowValue).Value
End Sub
The finds the proper row and then acquires data from other columns in that row.

Fetch cell value with increment in row number every day

I have an excel sheet which contain dates in one column (Say column A) and some values corresponding to each date in another column (Say column E). I want to fetch the value from the cell at the intersection of today's date (in column A) and it's corresponding value (in column E).
The value fetched should be assigned to a different cell (say R1). The value should automatically update in R1 since we need to fetch based on today's date.
Please provide me a formula for cell R1.
Example:
-----A----------B---------C--------D--------E
5/8/2015-------------------------------------3
6/8/2015-------------------------------------3
7/8/2015-------------------------------------6
8/8/2015-------------------------------------10
9/8/2015-------------------------------------3
10/8/2015------------------------------------12
11/8/2015------------------------------------3
If today is 10/08/2015, then cell R1 should be filled with the value 12.
Tomorrow R1 should be filled with the value 3 automatically.
Thank you.
You should try this formula in your cell R1:
=VLOOKUP(NOW(), A1:E11, 5, TRUE)
One thing is that if needed, you should modify range A1:E11 as you like.
ADDED
If your R1 cell is in other sheet, use this formula:
=VLOOKUP(NOW(), sheetname!A5:E11, 5, TRUE)
Here, also has one point that you should modify sheetname to your data sheet name.
I'm assuming you want to solve this with a formula, so try this formula in cell R1
=INDEX(D:D;MATCH(TODAY();A:A;0))