I have a spreadsheet that has formulas spread over the sheet (averaging around five cells between them).
I want to copy a formula from say A1 and paste it into cell A6 but have the formula only increment the cell reference by 1.
For example:
=IF(ISBLANK(sheet1!$a$2),"",sheet1!$a$2)
And paste this into cell A7 but have the absolute reference now be
=IF(ISBLANK(sheet1!$a$3),"",sheet1!$a$3)
There are about 200 of these to add to the sheet so I'd rather not manually edit each one.
Use INDEX on column A and some maths to create the stagger.
=IF(ISBLANK(INDEX(Sheet1!$A:$A, INT((ROW(1:1)-1)/5)+1)), "", INDEX(Sheet1!$A:$A, INT((ROW(1:1)-1)/5)+1))
I suppose that INT could be replaced by ROUND for a 'fuzzy' stagger 'averaging around 5 cells between them'.
Other possibilities for incrementing the stagger could be COUNT, COUNTA, COUNTIF or even COUNTIFS but you haven't provided enough information to base any concrete suggestions upon beyond generalities.
Try using the Indirect in that way :
=IF(ISBLANK(INDIRECT("Sheet1!$A$" & ROW()+1)),"",INDIRECT("Sheet1!$A$" & ROW()+1))
If you like using VBA you can use this simple loop
Sub Test()
Dim r As Long
For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row Step 6
Cells(r, 1).Formula = "=IF(ISBLANK(Sheet1!$A$" & r + 1 & "),"""",Sheet1!$A$" & r + 1 & ")"
Next r
End Sub
Related
I'm trying to search on the specific column(E), and if matched with the first 4 digit, I would like to copy the number to a different column.
Column E is where i would like to paste all the random number(dynamic)
Column A/B/C is static where i would add 4 digits from time to time.
Column I/J/K is where is would like to paste the result.
PS:
I'm doing it manually and would really appreciate if someone can help me out with the automation hence no code is provided. :(
Having ExcelO365 means you may use FILTER(). Therefor try the below:
Formula in I2:
=FILTER($E:$E,ISNUMBER(MATCH(--LEFT($E:$E,4),A:A,0)))
Drag right to K2. Now, this is dynamic and will change accordingly upon data entry in column E:E, or changing values in A:C.
this is the code to execute on sheet 1, it goes through the entire column E and validates through the formula of counting if in each of the first three columns and assigns the value found in the corresponding columns.
Sub macro()
Dim Static_Data As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Hoja1")
Active_row = 2
Do While Sht.Range("E" & Active_row).Value <> ""
Static_Data = Sht.Range("E" & Active_row).Value
For i = 1 To 3
If Application.WorksheetFunction.CountIf(Sht.Columns(i), Mid(Static_Data, 1, 4)) > 0 Then
Sht.Cells(Sht.Cells(Rows.Count, i + 8).End(xlUp).Row + 1, i + 8).Value = Static_Data
End If
Next i
Active_row = Active_row + 1
Loop
End Sub
For Excel versions that don't support FILTER or as an alternative you can use standard formulas for this.
If you use columns F-H as helper columns (and these columns can be hidden) then the formula in F2 will be:
=IF(NOT(ISERROR(VLOOKUP(VALUE(LEFT($E2,4)),A$2:A$100,1,FALSE)))=TRUE,$E2,"")
The formula can then be copied across and down. This will find your matches.
In order to then remove the blanks from the data you can use the following formula in I2 and again copy across and down. Depending on how many numbers you want to add in, you may want to extend the range A$2:A$100 in the top formula and F$2:F$100 in the bottom formula
=IFERROR(INDEX(F$2:F$100,AGGREGATE(15,6,(ROW(F$2:F$100)-ROW(F$2)+1)/(F$2:F$100<>""),ROWS(I$2:I2))),"")
Lets say we have 5000 rows with random values (blanks, numbers, characters). I need to show type of all the cells in these 5000 rows in a single cell using a formula. Is it actually possible? I've tried to CONCATENATE(CELL("type";array)) and ctrl+shift+enter but it didn't work (it returns the type of the first cell in the array).
If you want to know, this is for finding a cell with text rather than values or blanks in a very big file. Maybe you have a better solution.
Thanks in advance.
UPD: thanks for macros but I can't use them in this workbook, I need a formula-solution.
UPD: I've got how to do it with conditional formatting => new rule => use a formula to determine... => use ISTEXT('first cell of the range') formula
But still, is it possible to create the formula?
The best way to go about this is to use MACROs
Here is my sample code:
Sub Button2_Click()
numRows = 10 ' Number fo rows to loop through, in your case 5000
'loop through each cell located in column 1
'Check its type
'Concatenate each one in 1 cell on the 8th column
For i = 1 To numRows
Sheet1.Cells(1, 8).Value = Sheet1.Cells(1, 8).Value & TypeName(Sheet1.Cells(i, 1).Value) & ","
Next i
End Sub
You can adapt this small user defined function to your needs.
Say we are looking at cells in the first row, from A1 through D1 and we want to concatenate their types into a single cell. Enter the following UDF() in a standard module:
Public Function KonKaType(rIN As Range) As String
Dim r As Range
For Each r In rIN
s = "cell(""type""," & r.Address(0, 0) & ")"
KonKaType = KonKaType & Evaluate(s)
Next r
End Function
and then in the worksheet cell E1 enter:
=KonKaType(A1:D1)
I want to check Column A (A1:A365) in Sheet2, Sheet3, Sheet4 and Sheet5 and find Maximum value for each cell. Compare A1 in Sheet2, Sheet3, Sheet4 and Sheet5, find maximum of it and report it in A1 in result page. Also in cell B1 report corresponding sheet for this maximum. This goes on to Column A (A1:A365)
the following code i used:
Worksheets("sheet2").Range("A1").Value = a
Worksheets("sheet3").Range("A1").Value = b
Worksheets("sheet4").Range("A1").Value = c
Worksheets("sheet5").Range("A1").Value = d
MaxValue = Application.Max(a, b, c, d)
Range("A1").Value = MaxValue
yes i have just 4 sheets – Mohsen 11 mins ago
Non VBA Solution
In Sheet1, Cell A1, put this formula
=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)
In Sheet1, Cell B1, put this horrifying formula. I am sure there is a better way to find the sheet name though.
=IF(Sheet1!A1=Sheet2!A1,RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)),IF(Sheet1!A1=Sheet3!A1,RIGHT(CELL("filename",Sheet3!A1),LEN(CELL("filename",Sheet3!A1))- FIND("]",CELL("filename",Sheet3!A1),1)),IF(Sheet1!A1=Sheet4!A1,RIGHT(CELL("filename",Sheet4!A1),LEN(CELL("filename",Sheet4!A1))- FIND("]",CELL("filename",Sheet4!A1),1)),IF(Sheet1!A1=Sheet5!A1,RIGHT(CELL("filename",Sheet5!A1),LEN(CELL("filename",Sheet5!A1))- FIND("]",CELL("filename",Sheet5!A1),1)),""))))
A word of caution though. To use the RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)), you need to have the workbook saved.
My Sheet2, A1 has 1, Sheet3, A1 has 2, Sheet4, A1 has 2.5,Sheet5, A1 has 3
VBA Solution
Sub Sample()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("A1:A365").Formula = "=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)"
.Range("A1:A365").Value = .Range("A1:A365").Value
For i = 1 To 365
Select Case .Range("A" & i)
Case ThisWorkbook.Sheets("Sheet2").Range("A" & i).Value: .Range("B" & i).Value = "Sheet2"
Case ThisWorkbook.Sheets("Sheet3").Range("A" & i).Value: .Range("B" & i).Value = "Sheet3"
Case ThisWorkbook.Sheets("Sheet4").Range("A" & i).Value: .Range("B" & i).Value = "Sheet4"
Case ThisWorkbook.Sheets("Sheet5").Range("A" & i).Value: .Range("B" & i).Value = "Sheet5"
End Select
Next i
End With
End Sub
Quick solution
Based on Sidd's answer, here's my non-VBA solution without the horrifying formula:
Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
As the sheet name does not have to be flexible (I'd assume you don't change it that often), you can use this formula in B1:
=IF(Sheet2!A1=A1,"Sheet2",
IF(Sheet3!A1=A1,"Sheet3",
IF(Sheet4!A1=A1,"Sheet5",
"Sheet5")))
More structural solution (better suited for many worksheets):
If you have many worksheets, you could consider this alternative.
Have a list of the relevant worksheets stored somewhere in your worksheet. (in the example, I place the list in E3:E7). Name this range Sheets. (Similar to Sidd's horrifying formula, I used the CELL formula to dynamically get each sheet name. However, this is not necessary in a static model)
(same as step 1 above): Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
Place this formula in A2:
=INDEX(Sheets,MATCH(1,COUNTIF(INDIRECT("'"&Sheets&"'!A1"),A1),0))
Enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
I uploaded the second solution here.
Kudos to this instruction!
Since the formula is intended to be copied down, you don't want to hard-code a reference to cell A1 in the INDIRECT in Peter Albert's formula. You can use CELL("address",A1) to get a reference to A1 that can be copied down instead.
You can also avoid the need to array-enter the formula by using LOOKUP to return the result instead of INDEX & MATCH.
Note that LOOKUP will return the name of the last worksheet with the max value in case of a tie.
The following formulas use a named range Sheets with the names of each worksheet
=MAX(Sheet2:Sheet5!A1) returns max value (identical to Peter Albert's formula)
=LOOKUP(2,1/COUNTIF(INDIRECT("'" & Sheets & "'!" & CELL("address",A1)),A1),Sheets) returns name of sheet with max value
Make two new sheets with one titled "First" and the other "Last". Make new sheets for your project originate through a button that contains a macro to add duplicate sheets between "First and Last". Then just put one of these simple formulas for example in your reporting cell: =SUM(First:Last!K28) or =MAX(First:Last!K28)
Working on a excel macros where I am trying to add the cell values from above cells to calculate total value. This is what my data looks like
Here I want to add above cell values for each column to calculate the sum. To accomplish this i have written a macro as follows.
For cl = 2 To 5
Worksheets(5).Cells(4, cl).Formula = "=SUM(B4:B6)"
Next cl
This should set the formula to each cell in a row till 5 columns.
But it sets the same formula on all cells in a row it should get change according to column. How to set sum formula for each cell for corresponding column ?
Not sure I quite understand your code. You seem to be writing into row 4, but you also want to sum from row 4 to row 6. That will create a circular reference.
Let's assume the formula is written into row 3 instead. You will want to use the R1C1 reference style to make the cells to sum relative to the current cells.
A trick to learn what reference to use is:
Get a new worksheet, enter =SUM(B4:B6) into cell B3 and copy to the right.
Then click File > Options > Formulas and select R1C1 Reference Style.
Now inspect the formulas in the sheet. You will see this: =SUM(R[1]C:R[3]C)
This is what you need in the macro.
For cl = 2 To 5
Worksheets(5).Cells(3, cl).FormulaR1C1 = "=SUM(R[1]C:R[3]C)"
Next cl
some more details to R1C1 and R[1]C[1] style in formulas.
As far as I know R[1]C[1] creates a relative reference and R1C1 creates a absolute reference. But keep in mind that the figures for R[x] and C[y] are an offset to the cell which contains the formula.
That means if you want to show the sum of A1:B4 in C5 the code has to be like this:
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R[-4]C[-2]:R[-1]C[-1])"
If you want to the same but ending up with an absolute reference is aht to look like this.
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R1C1:R4C2)"
You can use very simple formula like below:
Sub sum_month()
Sheets("13").Activate
Range("D2").Formula = "=SUM(A1+A2+A3)"
End Sub
And next just hold and drag the cell to fill up another rows automatically.
For your case, you can use this:
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(0, 0), (Cells(1, cl).Column < 27) + 2)
ValueSum = "=SUM(" & ColName & "4:" & ColName & "6)"
Worksheets(5).Cells(4, cl).Formula = ValueSum
Next
Try something like this.
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(False, False), 1 - (cl > 26))
Worksheets(5).Cells(4, cl).Formula = "=SUM(" & ColName & "4:" & ColName & "6)"
Next cl
I think you can just reference the whole formula range and Excel will be smart enough to adjust the columns.
With no loop:
Worksheets(5).Cells(4, cl).resize(1,4).Formula = "=SUM(B4:B6)"
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.