Excel : Drag cell with one cell gap - excel

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

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

How to copy formula to different columns at the same time loop through a list to get different values as the input of the formula?

i m new to VBA, need help on this issue from experts.
I splitted a BBG function into cell B1 and D1
B1="BDS($C"D1=","PG_REVENUE","PRODUCT_GEO_OVERRIDE=G","number_of_periods=6")"
C2 To C6 are names of stocks
It looks like this when pasted to cells
=BDS($C2,"PG_REVENUE","PRODUCT_GEO_OVERRIDE=G","number_of_periods=6")
As the code below, I would like to copy the formula to cell firstly H11, and then move 10 cells to the right to cell R11 and paste the formula again and repeat the process.
For cell H11, i would like the formula to use $C2 as the first argument, for R11, i want 2 to be increased to 3, so the first argument becomes $C3
My codes allow me to loop and paste the formula horizontally but failed to increase the count each time it moves. (All cells gives $C6 as the input)
How can i change my codes so that the counter will increase by 1 as when it moves to the right?
Sub paste_formula()
Dim cp As Worksheet
Dim x As Integer
Dim y As Integer
Dim t As Integer
Dim m As Range
Set cp = Sheets("Control Panel")
cp.Activate
For t = 8 To 100 Step 10
For x = 2 To cp.Range("C2").End(xlDown).Row
cp.Cells(11, t).Formula = "=" & cp.Range("B1") & x & cp.Range("D1")
Next x
Next t
End Sub
I want
At cell H11, =BDS($C2,"PG_REVENUE","PRODUCT_GEO_OVERRIDE=G","number_of_periods=6")
At cell R11, =BDS($C3,"PG_REVENUE","PRODUCT_GEO_OVERRIDE=G","number_of_periods=6")
At cell AB11, =BDS($C4,"PG_REVENUE","PRODUCT_GEO_OVERRIDE=G","number_of_periods=6")
etc

Summing two columns together (each cell separately)

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

VBA Conditional Formatting with changable conditions

I am trying to set conditional formatting in 18 cells in third column ("C"). I have merged each 6 cells in first column ("A"), and unmerged (normal) cells in second column ("B"). I am trying to check for each next cell in row of column "C" if there is a "yes" in first row of column "A" or whether there is a "no" in "A" column and "pass" in "B" column. The trick is, I want to check only first row of "A" column, seventh, thirteenth and nineteenth (so with the step = 6) and check every row in "B" column. I try something like this:
Sub try()
Dim i As Integer
Dim j As Integer
i = 1
For j = 1 To 12
With Range("C1:C18")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(Cells(i, 1) = ""Yes""; AND(Cells(i, 1) = ""No""; Cells(j, 2) = ""Pass""))"
End With
If j Mod 6 = 0 Then
i = i + 6
Next j
End Sub
But it does not work like that, I saw examples with specific Cells like "A1" or "A3" but I want a number to increase with every loop (so I tried it with Cells(row,column)).
You can do it in one statement on the whole range by using relative addresses, so what applies to C1 relatively to A1 and B1 will follow automatically in the subsequent rows of the range.
The only trick is to retrieve the value in column A, since this value is only available in cells A1, A7, etc. This is achieved by the expression OFFSET(A1,-MOD(ROW(C1)-1,6),0).
Sub doIt()
With Sheet1.Range("C1:C30").FormatConditions
.Delete
.Add(xlExpression, , _
"=OR(OFFSET(A1,-MOD(ROW(C1)-1,6),0)=""yes"",AND(OFFSET(A1,-MOD(ROW(A1)-1,6),0)=""no"",B1=""pass""))") _
.Interior.ColorIndex = 6
End With
End Sub
You can also do it from the GUI using the same formula; select cell C1 then select the whole range C1:C30, and click
Conditional Fomatting -> New rule -> Use a formula... and enter the same formula.
BTW, the expression can be further simplified if you dont care to check for "no", meaning if column A is assured to be either "yes" or "no".

Copying the rows to different column with rows incrementing by 21

I want to copy my data from different rows to some other column.
Example.
B4 has to be copied to H129 ,
B25 to H130,
B46 to H131...and so on ..
Here the rows are getting incremented by 21 rows .
How do do that in excel ?
Go to VBA and:
Sub sdgfsa()
Dim i As Integer
Dim j As Integer
j = 129
For i = 4 To 1000 Step 21 'put the end number you want, the start is 4 like your example
Plan1.Cells(j, 8).Value = Plan1.Cells(i, 2).Value 'Here, plan1 represents the sheet, 2 is B column, 8 is H column
'or you can use KazJaw's Copy here
j = j + 1 'j gets incremented
Next
End Sub
And when you need to copy all (i.e.. values and formats as well) than do it in this way:
Range("B4").Copy Range("H129")
Using #Daniel code you could need something like this inside your loop
'...see Daniel answer for beginning
Plan1.Cells(i, 2).Copy Plan1.Cells(j, 8)
'... and so on
Enter the following formula into cell H129:
=INDIRECT("b"&(ROW(H129)-129)*21+4)
Then use "Fill Down" to copy the formula from this cell into the cells below it.
This works because the "H129" reference will change to H130, H131, etc. as it is copied, so the ROW() function will return successively higher numbers.
Edit:
Corrected formula to match example in question.

Resources