I have to average sets of 3 columns.
EXAMPLE:
Blood_Patient1_0_R1, Blood_Patient1_0_R2, Blood_Patient1_0_R3
There average is in a new column Blood_Patient1_0
Similarly, Blood_Patient1_3_5_R1, Blood_Patient1_3_5_R2, Blood_Patient1_3_5_R3
The average is in a new column Blood_Patient1_3_5
This process is being repeated for 8 such sets of columns.
Currently I am averaging using the formula: IF(ISERROR(AVERAGE(B7:D7)),"",AVERAGE(B7:D7)) and auto-filling 21,000 plus rows.
Since there is a pattern in column headings, I was thinking to automate the whole process.
This is what I have thought so far in terms of algorithm:
0, 3_5, 6_25 are time values in column headers.
at each time instant, there are 3 replicates R1, R2,R3 as part of column headers
for time array [3.5h, 6.25h, 9.5h, 11.5h, 16.5h, 25h, 49h, and 156h
]
create a new column
for rows from 2 to 21458
average over replicates from R1 to R3 using above formula
I do not know how to write this in excel. Any help would be appreciated.
Give this a go.
This solution assumes that you have a continuous data set, that is, no gaps between the columns you wish to search through.
Firstly, you will need to include this function. Paste it into the same module as the subroutine. The purpose of this function is to allow the string in each heading to be compared against an array of substrings, as opposed to the single substring permitted by the InStr function.
Function Time_Search(strCheck As String, ParamArray anyOf()) As Boolean
Dim item As Long
For item = 0 To UBound(anyOf)
If InStr(1, strCheck, anyOf(item)) <> 0 Then
Time_Search = True
Exit Function
End If
Next
End Function
Next, paste in this subroutine. I have assummed that the dataset begins at cell A1. Also, I have allowed for a dynamic range, should the number of columns or rows ever change.
Sub Insert_Average_Columns()
Dim HeaderRange As Range
Dim LastRow As Long
Dim c As Range
Set HeaderRange = Range(Range("A1"), Range("A1").End(xlToRight))
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each c In HeaderRange.Cells
If Right(c.Value, 2) = "R3" Then
If Time_Search(c.Value, "3_5", "6_25", "9_5", "11_5", "16_5", "25", "49", "156") Then
c.Offset(0, 1).EntireColumn.Insert
c.Offset(0, 1) = "Average of " & Left(c.Value, Len(c.Value) - 3)
c.Offset(1, 1).FormulaR1C1 = "=IFERROR(AVERAGE(RC[-3]:RC[-1]),"""")"
c.Offset(1, 1).AutoFill Range(c.Offset(1, 1).Address, Cells(LastRow, c.Offset(1, 1).Column))
End If
End If
Next c
End Sub
There is one issue with your data. If you want the procedure to insert an average column for T = 25, then it will do so for all columns where T contains the string "25". If there are T= 8.25, 10.25, 15.25, etc, these will all have averages applied. The only way around it would be to include more of the heading string in the parameter array, but I presume you will be dealing with a variable Blood_Patient ID so that probably isn't an option.
Related
EDIT, in short:
I have an excel table that looks like this:
tabel
Each cell contains a range of numbers.
I'm looking for a function that can search for a number in the whole table and indicate me the column and row where the range that it belongs to is located.
So, if I search for number 2346, it should function like this:
function (2346) > result (C1, R2)
I have a huge archive of photos (about 300.000 items) in a library.
Photos are stored in boxes, the boxes in shelves.
Each box has a range of inventory numbers of photos.
I want to create a map of the deposit that looks like this:
Shelf 1 - contains boxes 1, 2, 3, etc.
Box 1 - contains photos with inventory numbers between 1257-1321
Box 2 -"- between 2345-2522
Box 3 -"- between 123523-123643
Translated in an excel table, it would look like this:
Column 1 would be Shelf 1, containing the boxes with the images:
cell in column 1 / row 1 (that's box 1) contains the range of numbers: 1257-1321
cell in column 1 / row 2 (box 2), the range: 2345-2522
cell in column 1 / row 3 (box 3), range: 123523-123643
They are not in order, because they entered in more than 100 years in the collection, and they are arranged as they entered and by subject. Not to mention that the deposit has been moved a few times. So, I have a hard time to find one image when I'm looking for it.
But if I have this excel table, this map of the deposit, supposedly I want to enter the inventory number that I'm looking for, let's say "2346", and a formula that search throughout the whole table would indicate me that the item (number I look for, is in a range that is located in column 1, row 2, that means shelf one, box 2 in the deposit).
Actually the concept is very simple, excel is able to do MUCH more difficult tasks, and I'm amazed I can't find a way to do this. I'm a photographer and librarian, so my experience in programming is close to zero.
Thank you very much if you can help!
It's a bit of a lenghty one but not hard to understand, I made some sample data like so:
="C"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*COLUMN($A$1:$B$3))&", "&"R"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*ROW($A$1:$B$3))
Youll just have to change ranges
If you set up your worksheet like the image below (sheet name "Library"):
You could try the code:
Option Explicit
Sub Painting()
Dim LastRow As Long, i As Long, SearchValue As Long
Dim arrData As Variant
'Let s say that all data appears in sheet called Library
With ThisWorkbook.Worksheets("Library")
'Let s say that we are looking for the value in cell H1
SearchValue = .Range("H1").Value
'Find the Last row of column A sheet Library
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Create as array from Column A row 2 up to Column D row LastRow
arrData = .Range(.Cells(2, 1), .Cells(LastRow, 4)).Value
'Loop Array to find a match
For i = LBound(arrData) To UBound(arrData)
If arrData(i, 3) <= SearchValue And arrData(i, 4) >= SearchValue Then
'Pop up with Shelf & Box name
MsgBox "Search Value " & SearchValue & " included in:" & vbNewLine & "Shelf: " & arrData(i, 1) & vbNewLine & "Box: " & arrData(i, 2)
'Select the line where the Search Value found
.Range("A" & i + 1 & ":D" & i + 1).Select
'Exit the loop
Exit For
End If
Next i
End With
End Sub
Result:
I am relatively new to Macros and VBA in Excel, so I need some guidance on how to solve my current issue.
The end goal of my project is to have a macro compare two sets of data organized into rows and columns (We'll say table A is the source data, and table B is based off of user input). Each row in table B should correspond to a row in table A, but they could be out of order, or there could be incorrect entries in table B.
My thought is that for the first row in each table, the macro would compare each cell left to right:
If Sheets("sheet1").Cells(2, 1) = Sheets("sheet2").Cells(2, 1) Then
If Sheets("sheet1").Cells(2, 2) = Seets("sheet2").Cells(2, 2)
Ect, ect.
My problem comes in when the Cell in table B does not match Table A.
First, I would want it to check B row 1 against the next row in A, and keep going throughout table A until it finds a "complete match" with all five columns of the row matching.
I've been trying to do this with Else if and For/Next staements
For row= 2 to 10
'if statements go here
Else If Sheets("sheet1").Cells(2, 1) <> Sheets("sheet2").Cells(2, 1)
Next row
I may be completely misunderstanding the syntax here, but I have yet to produce a situation where if the criteria is not met, it goes to the next row.
If no complete match is found, the last cell in table B row 1 that couldn't be matched should be highlighted.
Then regardless of whether a match was found or not, we would move to table B row 2, and start the whole process over.
So, I have the logic worked out (I think), where the comparison ifs would be inside a loop (or something) that would cycle through table A row by row. Then that whole process would be in another loop (or something) that would cycle through Table B.
At the end of the process, there would either be no highlighted cells showing that all entered data is correct, or cells would be highlighted showing data that do no match.
I am fairly certain that the cycling through table B is not the issue. Rather, I'm having difficulty getting the Macro to move to the next table A row if something doesn't match.
Please let me know if I need to elaborate on anything.
Thanks!
You could try:
Option Explicit
Sub test()
Dim Lastrow1 As Long, Lastrow2 As Long, i As Long, j As Long
Dim Str1 As String, Str2 As String
'Find the last row of sheet 1
Lastrow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'Find the last row of sheet 2
Lastrow2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow1
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str1 = Sheet1.Cells(i, 1).Value & "_" & Sheet1.Cells(i, 2).Value & "_" & Sheet1.Cells(i, 3).Value
For j = 2 To Lastrow2
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str2 = Sheet2.Cells(j, 1).Value & "_" & Sheet2.Cells(j, 2).Value & "_" & Sheet2.Cells(j, 3).Value
'If both strings match a message box will appear
If Str1 = Str2 Then
MsgBox "Line " & i & " in table A match with line " & j & " in table B!"
Exit For
End If
Next j
Next i
End Sub
Sheet 1 structure:
Sheet 2 structure:
enter image description here
Looking at the above image, I am wondering how do I find the average starting at a number that matches certain criteria? For example, given the range J3:Q3, how would I start find the averaging after a number is greater than 50 (so in this example L3:Q3)?
You can add a custome made Function in VBA in your Worksheet that you can call it from your worksheet.
In your Excel Worksheet you need to type
=Average_Cells(first_cell_for_average, value_above)
first_cell_for_average - you can put A3, the function will handle the rest.
value_above = in your post it's 50, you can modify it later to whatever value you want.
The Function Code:
Public Function Average_Cells(ByRef first_Cell As Range, larger_Than As Long) As Double
' Row 3 in your post
last_col = ActiveSheet.Cells(3, ActiveSheet.Columns.count).End(xlToLeft).Column
first_Col = first_Cell.Cells(1, 1).Column
For col = first_Col To last_col
If Cells(3, col).Value > larger_Than Then
GoTo Exit_For
End If
Next
Exit_For:
Average_Cells = Application.WorksheetFunction.Average(Range(Cells(3, col), Cells(3, last_col)))
End Function
Hack-y method:
Fill in J4 with
=IF(J3>50, J3, '')
And then copy it until Q4.
Then fill in J5 with
=AVERAGE(J4:Q4)
I have a code that returns all the data I want from a query. The problem is that the csv format separates the data, even account names with commas in them. For each row, column A is the account name followed by 11 blocks of integer data (making a total of 12 cells used per row). Fortunately, for the accounts that have commas, the result is only one additional cell (making a total of 13 cells used per row).
I need an IF-THEN formula that will concatenate Column A & Column B if there are 13 used cells in that row, otherwise leaving things alone. Being new to VBA concatenate is giving me huge problems.
Any suggestions? Thanks in advance.
Well, just from a term point of view, Excel Formulas and Excel VBA are not the same thing.
In fact, there are a lot of people on here that are formula pros that know nothing about VBA and visa versa.
That being said, you could use something like:
=IF(COUNTBLANK(C1:N1)=0,A1&" "&B1,A1)
Of course, assuming your data looks like this:
It would be easier to use:
=IF(N1="",A1,A1&" "&B1)
Concatenating in Excel formulas and VBA is pretty simple.
Simply separate the strings you wish to concatenate with an amperstand &
If I want to concatenate A1 and B1, I can use =A1&B1 as shown in the formula.
If I want to add a space, or even a word, I can just add it between them as a string:
=A1&" is way cooler than "&B1
Edit:
VBA:
Sub ConcatenateCells()
Application.ScreenUpdating = False
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then c = c & c.Offset(0, 1)
'You could also add a space between the two using c = c & " " & c.Offset(0, 1)
Next c
Application.ScreenUpdating = True
End Sub
I don't know if you want to scoot the data over after you have concatenated the values, but if you do, you can use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then
c = c & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub
Still assuming I got your data format right, here is what the before and after looks like:
Final Edit:
If you absolutely insist on counting the number of used rows instead of simply looking for a coma in column A, then use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If Cells(c.row, Columns.Count).End(xlToLeft).Column = 13 Then
c = c & " " & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub
I have data stored in three columns of Excel
Column A: Serial Number
Column B: Date
Column C: Value (e.g. Cost)
I need to look for the Value (Column C) associated with a particular Serial Number (Column A) AND Date (Column B).
So for example, in the screenshot below, if I want to look for the Value associated with Serial number (T455) and Date (Dec 13, 2010), the value should be 8.
The only method I can come up with would be computationally inefficient, because I would go through ALL the cells each time I look for a value.
Is there a method, for example, that would limit the search area for a given serial number?
For example, if I am looking for a value for Serial Number T455, how can I limit the code to search for the date in Rows (6-13) and find the corresponding value in Column C, rather than searching the whole table?
Sub FindValue()
Dim S as String
Dim D as Date
Dim V as Integer
S = T455
D = Dec 13, 2010
for i = 1 to Range("A1").End(xldown).Row
If Range("A" & i) = S And Range("B" & i) < Date - 7 And Range("B" & i) < Date + 7 Then
' This way i search a date range rather than a specific date
V = Range("C" & i).Value
End If
End Sub
I thought of While loops, or Lookup functions, but reached a dead end.
Non-VBA Solution that may be a lot easier and less of a headache.
Column A consists of the formula, for A1 = "=B1&C1"
Cell G1 formula can be seen in formula bar.
UPDATE
Here is a VBA solution that will work faster, but there are some notes based on what you wrote that I am unsure of. Also, see some comments to help your code work more like you want it to.
Sub FindValue()
Dim S As String, D As Date, V As Integer, rngFound As Range, cel As Range
S = "T455" 'needs quotes around the string
D = "Dec 13, 2010" 'needs quotes around the date
Dim wks As Worksheet
Set wks = ActiveSheet
With wks
'always better to AutoFilter than Loop when you can!
.UsedRange.AutoFilter 1, S
.UsedRange.AutoFilter 2, ">" & D - 7, xlAnd, "<" & D + 7
Set rngFound = Intersect(.UsedRange, .Columns(3)).SpecialCells(xlCellTypeVisible)
'the only thing here is if you have a date range _
'you may return more than one result _
'in that case, I don't know what you want to do with all the V's
If Not rngFound Is Nothing Then
For Each cel In rngFound
V = cel.Value
Next
End If
.AutoFilterMode = False
End With
End Sub
If you are willing to entertain a non-VBA solution, then you can use this implementation, which basically uses this formula:
=IFERROR(INDEX(List, MATCH(0, COUNTIF(H1:$H$1, List)+
IF(Category2<>Criteria2, 1, 0)+IF(Category1<>Criteria1, 1, 0), 0)), "")
There are several VBA methods, which really depend on how comfortable you are with the language and how efficient you want your solution to be. Off the top of my head:
1) filter the list with both criteria, then return the relevant value in whatever row is visible (if any)
2) sort your data by those two columns (first by serial, then date), then perform searches (you'd probably want to call built-in functions like MATCH)