Summing two columns together (each cell separately) - excel

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

Related

Excel 3D Reference: Delineate string results from range of Worksheets

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

Excel Automated Data Retrieval From Another Workbook

So I'm pretty new to excel formulas and got almost no experience with VBA. But I've come across with a problem that I need to solve.
So the scenario goes like this.
I've got two workbooks and I need to retrieve data to one workbook from another if the condition for a cell value is met. Let me explain with an example.
(C for columns, R for rows, x for random numbers)
I've got Workbook A as shown below:
And Workbook B with the same structure
So what I'm trying to achieve here is:
When I change/insert values in Workbook A, C3Rx there will be a conditional mechanism that will check for the value.
Let's say if C3R1's value is "1" on Workbook A, it should fill C1R1, C2R1 and C3R1 on Workbook B accordingly.
If the value is not "1", it just should keep scanning the Workbook A, C3 and when it meets the conditional requirement (C3Rx having the value of "1"), it should write it in and go to the next row (C1R(x+1)). Follow the procedure again and again. Scanning all values in Workbook A.
I've tried to make it work using VLOOKUP and some other functions together but it doesn't suit really well with my case. It works with spaces when the value does not meet the condition and also, I need to fill all the cells on C1 with the formula till the end. (considering I don't know how long it may go, that's not really a solution for me)
I think it's achievable with Macros but like I've said, I don't have much experience with VBA.
Thanks for your help in advance.
Have a good one.
I'm not exactly get what you mean.
Anyway below I am guessing on what you mean.
Below is what contains in Workbook-A sheet1 column A to C
Below is what contains in Workbook-B sheet1 column A to C
With the first condition that Workbook-A and Workbook-B are arlready open.... below is Workbook-A sheet1 where cell C4 and C7 fill with 1 value,
and after the button is clicked :
1. Cell A4 to C4 value in Workbook-A Sheet1 become the value of cell A4 to C4 value in Workbook-B Sheet1.
2. Cell A7 to C7 value in Workbook-A Sheet1 become the value of cell A7 to C7 value in Workbook-B Sheet1
Button1 is assign to a macro like this :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
r = cell.Row
wbA.Range("A" & r, "C" & r).Value = wbB.Range("A" & r, "C" & r).Value
'wbA.Range("A" & r, "C" & r).Interior.Color = vbRed
End If
Next
End Sub
The code will look to each value in column C (starts from row 2) in Workbook-A sheet1.
If the code find the value is 1 in row X of column C, then it copy row X of column A to C in Workbook-B sheet1.
That's if I'm not mistaken on what you mean.
Assumed that both workbooks already open.
Below is the beginning look of wb-A right after it's open :
Below is the beginning look of wb-B right after it's open :
Back to wb-A, after you examined the data, you decided that id-003 branch location is not London, but Madrid. So you type number 1 in the third column id-003 row. WB-A now look like this :
And what you expect is, if the code find any row in the third column of wb-A with value "1", then the code have to copy all the three columns of id-003 row then paste it to the last blank row in the emp_id column of wb-B. So, wb-B look like this :
Here is the code which has to be in wb-A module :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
Range(cell, cell.Offset(0, -2)).Copy Destination:= _
wbB.Range("A1000000").End(xlUp).Offset(1, 0)
End If
Next
End Sub
Again, above is just my guessing because I'm still not clear what you want and how is the situation.

Extract Values From Cells Based On Corresponding Cell Value

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.

Excel : Drag cell with one cell gap

I have the formula written in the cell B1 and need to drag it down till the B100.
Example:
Formula for cell B1
=(A2 - A1)
Note: Now I need to drag cell B1 to B100 and need one cell blank in between like shown below:
Expected Result:
A B
-----------
2 3
5
8 1
9
Is that possible to format the drag option?
You can use:
=IF(MOD(ROW(),2)<>0,A2-A1,"")
As pointed out by #Jeeped, above formula can also be written as:
=IF(MOD(ROW(),2),A2-A1,"")
A typical VBA solution might be easily done with a Union method.
Sub werqtr()
Dim i As Long, bs As Range
With Worksheets("Sheet1") 'KNOW WHAT WORKSHEET YOU ARE ON!!!!!!
Set bs = .Range("B1")
For i = 3 To 100 Step 2
Set bs = Union(bs, .Cells(i, "B"))
Next i
bs.Formula = "=A2-A1"
End With
End Sub

Matching of cells in excel

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

Resources