I am trying to extract values from preceding rows based on a value which is given in next columns.
Like in following example (Explanation Table Image), in cell A2 Cell value is 10 (Highlighted in Green) the corresponding value in cell D2 is 3 (Highlighted in Red) so the value required in Cell E2, E3 & E4 is values of cells A3, A4 and A5 which are A-1111, B-2222, C-3333 after removing text "SLR# " respectively. And in case of Cell A6 the corresponding value in cell D6 is 2 so the required value in cell E6 & E7 will be D-4444 & E-5555.
The data continuous like this and formula or VBA code will fill my requirement in column E. Kindly let me know if you need further clarification. Thanks in advance...
Explanation Table:
I have tried the following User Defined Function but it gives me all values in one cell rather in next cell. (Thanks to Mr. Fluff for this UDF)
Function UDF(Rng As Range, Rws As Long) As String
Dim i As Long
Dim Cl As Range
For Each Cl In Rng
If Left(Cl, 4) = "SLR#" Then
UDf = UDF & Cl.Value
i = i + 1
If i = Rws Then Exit For
End If
Next Cl
UDF = Trim(Replace(fiberboysa, "SLR#", ""))
End Function
I could be simplifying this but if the data is ALWAYS structured that way then try this formula ...
=IF(D3<>"","",IF(ISBLANK(A3),"",A3))
... copy that into cell E2 and fill down.
I'm happy if it doesn't do it for you but it worked with the simple example you gave.
Related
Hopefully I can explain this right.
Looking to combine cells of text strings from multiple worksheets into one master worksheet.
Basically 3-D References. But formatted into rows and columns. And referencing a range of worksheets so new worksheets can be added or removed in between the bookends.
Desired output:
Column 1
Column 2
Column 3
WS01 Cell B1
WS02 Cell B1
WS03 Cell B1
WS01 Cell B2
WS02 Cell B2
WS03 Cell B2
WS01 Cell B3
WS02 Cell B3
WS03 Cell B3
Input: Strings from B1:B3 (should become matching rows separated into columns for each linked worksheet)
Each worksheet ('Worksheet 01:Worksheet 03') follows same format:
Column B
WS## Cell B1
WS## Cell B2
WS## Cell B3
Attempts:
=CONCAT('Worksheet 01:Worksheet 03'!B1:B3)
Result:
WS01 Cell B1WS01 Cell B2WS01 Cell B3WS02 Cell B1WS02 Cell B2WS02 Cell B3WS03 Cell B1WS03 Cell B2WS03 Cell B3
Please let me know what you think. Thank you for your time.
You can use:
=HSTACK(Sheet1:Sheet3!B1:B3)
Even though the answer below works fine, please look at this answer by JvdV that is far easier to use:
https://stackoverflow.com/a/74077560/12634230
=LET(
c,CONCAT(Sheet1:Sheet4!B1:B3),
q,SEQUENCE(LEN(c)/36,3,,12),
TRANSPOSE(MID(c,q,12)))
c uses your CONCAT formula to retrieve a concatenation of all values.
q calculates a sequence by the length of c divided by the length of text for the 3 values per Sheet (3* length 12 = 36) by 3 with steps of the length of each value (12).
This sequence is used in the MID function and needs the result to be transposed to meet your requirements:
If a Sheet will be added, changing the Sheet names in c will change the result to show the values from that Sheet as well. No further adjustments of the formula are required.
And if the number of outputs per sheet, or string length may change in future you could define these as variables too:
=LET(c,CONCAT('Worksheet 01:Worksheet 03'!B1:B3),
stringlength,12,
stringcount,3,
q,SEQUENCE(LEN(c)/(stringlength*stringcount),stringcount,,stringlength),
TRANSPOSE(MID(c,q,stringlength)))
#P.b just posted a formula approach, but as an alternative here's a VBA user-defined formula which returns an array. The only tricky part is getting the 3D reference in the UDF, since there's no structure or type equivalent to that in VBA: if you try to get it directly from the argument you just get an error.
Building from: https://www.excelforum.com/excel-programming-vba-macros/476283-user-defined-function-receiving-a-range-as-parameter.html
Function MyUDF(v)
Dim c As Range, f, arr, arrWs, rngAddr
Dim arrout, indx1, indx2, i As Long, r As Long, data
On Error Resume Next
Set c = Application.Caller
On Error GoTo 0
If c Is Nothing Then
f = "=myudf(Sheet1:Sheet3!A1:A3)" 'for testing purposes (adjust as needed)...
Else
f = c.Formula 'read the formula from the calling cells
End If
f = Mid(f, 8, Len(f) - 8) 'parse out the parens and formula name
arr = Split(f, "!") 'get an array from splitting on !
arrWs = Split(arr(0), ":") 'get the start/end worksheet names
indx1 = ThisWorkbook.Worksheets(arrWs(0)).Index
indx2 = ThisWorkbook.Worksheets(arrWs(1)).Index
rngAddr = arr(1) '...and the range address
'size the output array
ReDim arrout(1 To Range(rngAddr).Rows.Count, 1 To 1 + (indx2 - indx1))
For i = indx1 To indx2 'loop over the worksheets
data = ThisWorkbook.Sheets(i).Range(rngAddr).Value
For r = 1 To UBound(data)
arrout(r, i) = data(r, 1)
Next r
Next i
MyUDF = arrout 'return the array
End Function
I have an excel sheet which looks like .
I want an output like .
If any one of the cells in three columns is highlighted, then the output for that row should be 1 else 0. The output column is named count in linked output image.
For example: cells B2 and C2 are highlighted in the first row, so the output cell D2 should be 1. A4, B4, and C4 don't have any highlighted cells, so the output D4 should be 0.
Would someone please help me with a function for this?
Try this user-defined-function,
Option Explicit
Function IsHighlighted(rng As Range) As Long
Dim r As Range
For Each r In rng
IsHighlighted = IsHighlighted Or CBool(r.Interior.Pattern <> xlNone)
Next r
IsHighlighted = Abs(IsHighlighted)
End Function
I'm using Excel and I've just started using vba in Excel.
I need to add the values of column I, cells I6:I26 in sheet 1 to column D, cells D1:D21 in sheet 2.
So if I6 = 4 and D1 = 6, and I press the ADD button, D6 will equal to 10. Same goes for the rest of the cells.
I don't want it cell I6 to replace the value of D6, I want it to add to it.
This is the code I have so far for it;
Dim r1 as Range, v as variant
Set r1 = Sheets("Sheet2"). Range("D1")
V = Application.WorksheetFunction.Sum(Sheet1.Range("I6"), Sheet2.Range("D1"))
R1 = v
I had to do that 20 times..is there anyway to simplify this?
You can Evaluate the sum of arrays:
[Sheet2!D1:D21] = [Sheet1!I6:I26 + Sheet2!D1:D21]
I'm finding it a bit confusing where you want the result to go. Sheet1 has data in I6:I26 and Sheet2 has data in D1:D21.
You want the first result to go into cell D6... on what sheet? On Sheet2 so it starts overwriting the old data, in Sheet1? Some other sheet?
This code will place a formula in cell D6 on sheet 1 and drag it down to cell D26.
I've given two types of formula. A1 notation and R1C1 notation.
A breakdown of the R1C1 notation:
RC9 means take data from this row, column 9 - in cell D6 this will be cell I6.
R[-5]C4 means take data from 5 rows up in column 4 - in cell D6 this will be D1.
Sub Test()
With Sheet1
.Range("D6").Formula = "=SUM($I6,Sheet2!$D1)"
'You could also use R1C1 notation (which is easier if you're looping through rows/columns).
'.Range("D6").FormulaR1C1 = "=SUM(RC9,Sheet2!R[-5]C4)"
.Range("D6:D26").FillDown
End With
End Sub
I am trying to find a way to get multiple values from an array to display in one cell
Say for example I have the two columns as below
a 1
b 2
c 1
d 3
e 2
I want all the values form the first column where the second column is 1
vlookup and index with match both only provide the first matching instance, is there a way to do this with a function or does it have to be created in a macro with VBA?
Thanks
If you want all the results to be shown in ONE cell you could use that VBA formula:
'r is the range of cells that you have a value to look for
'v is the value you are looking for
Function getValues(r As Range, v As Variant)
Dim c As Range
getValues = ""
For Each c In r
If c.Value = v Then
If getValues = "" Then
'Offset(0,-1) will give you value from previous coulmn
getValues = c.Offset(0, -1).Value
Else
getValues = getValues & "," & c.Offset(0, -1).Value
End If
End If
Next c
End Function
Use example: in cell C1 enter this =getValues(B1:B5,1)
Considering your data is in range A1:B5 and Cell C1 contains the number you are looking for.
Enter the following array formula in Cell D1 and drag/copy down till the row as required.
=IFERROR(INDEX($A$1:$A$5, SMALL(IF($C$1=$B$1:$B$5, ROW($B$1:$B$5)-MIN(ROW($B$1:$B$5))+1, ""), ROW(A1))), "")
Being an array formula you'll have to commit above formula by pressing Ctrl+Shift+Enter.
Using helper columns this is possible with formulas alone.
Formulas:
In D1:
=""
In D2 downwards:
=IF(B2=$B$1,D1&A2&", ",D1)
In C1:
=LEFT(LOOKUP(2,1/(D:D<>0),D:D),LEN(LOOKUP(2,1/(D:D<>0),D:D))-2)
It Sum of columns is storing in one cell and if I want the same answer to be printed in another cell, what is the formula to get this?
Let's say your sum is stored in B1 and you want same in C1 then you can do
=B1
inside C1 cell
Let's say you want the value of cell A1 in cell B1. You would use this formula in B1:
= A1
In code you would write something like this (example in VBA):
Dim sht as Worksheet
Set sht = ThisWorkbook.Worksheets("MySheet") //Or whatever your target sheet is
sht.Range("B1").Formula = "=" & sht.Range("A1").Address