Copying the rows to different column with rows incrementing by 21 - excel

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.

Related

How to copy the number if contains certain number (first 4 digit) to another column - EXCEL VBA

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))),"")

How can I calculate the sum of the numbers of two columns for only a portion of the rows?

Say I have an Excel sheet with 10,000 rows and two columns. All 20,000 cells are filled with numbers and there is no missing data. I want to have a third column, the values of which are the sum of Column A and Column B. For example, C70 = A70 + B70, and C82 = A82 + 82, and the like.
The only problem is I want to do it for only a portion of the rows, say from row 125 to row 8954. I don't care about the rest of the values. And I don't want to do it by dragging the grid using the mouse. Is that possible?
If you have access to SEQUENCE() (Currently only available to Office 365 Insiders) then yes it is possible:
=INDEX(A:A,SEQUENCE(1000,,ROW(),1))+INDEX(B:B,SEQUENCE(1000,,ROW(),1))
Where 1000 is the number of rows desired. Place the formula in the first cell desired and it will automatically fill the rest.
I believe you need some logic about what is going on, related to the start and end row.
You can use an if-statement or sumifs() for this... will do an if-statement so i can specify not meeting the requirements as null.
With Start row as 2 and end row as 4 (see image), you can use this formula, and drag it down to the bottom of all columns:
=IF(AND(ROW(A2)<=F$2,ROW(A2)>=E$2),SUM(A2:B2),"")
Notice in the image that C5 has no value; this is due to the conditions of the if-statement being false.
Another idea, a simple macro that will do what you want by asking the user what the starting and end row is.
Sub test()
Dim startrow As Integer 'variable to hold first row
Dim endrow As Integer 'variable to hold last row
startrow = InputBox("Enter the start row")
endrow = InputBox("Enter the end row")
'loops through you desired range calculating what you want
Dim i As Integer
For i = startrow To endrow
Cells(i, 4).Value = Cells(i, 1).Value + Cells(i, 2).Value
Next
End Sub
Just change the values to suit your needs in what cells you want to add up and where you want the sum to go.

Paste absolute references in a formula but increase by 1

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

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".

Use CELL on an array to return string made of types of the cells

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)

Resources