Insert rows into table based on cell value - vba

I am trying to have a macro add in a specific number of rows to a table based on the value of A2.
For example:
If A2=10, then when I run the macro it should insert 10 rows.
also, as a second part to the macro I want it to copy the formula that the previous cells have.
Example:
A3= "='Sheet2'!A1"
and if I add 10 rows then the following rows should be sequential:
A4= "='Sheet2'!A1"
A5= "='Sheet2'!A2"
A6= "='Sheet2'!A3"
A7= "='Sheet2'!A4"
A8= "='Sheet2'!A5"
etc...
I know that a typical drag of the table will copy the cells, I want to ensure that the formula's are copied to the next row.

This will copy and paste the formula as you showed in A3 down the number you enter in A2.
ActiveWorkbook.Sheets(1).Cells.Range("A4:A" & Rows.Count).ClearContents
For i = 1 To ActiveWorkbook.Sheets(1).Cells(2, 1).Value
ActiveWorkbook.Sheets(1).Cells(3 + i, 1).Formula = ActiveWorkbook.Sheets(1).Cells(3, 1).FormulaR1C1
Next i
Is this what you are looking for?

Related

Loop through column values from one sheet and paste COUNTIF value from another column into another sheet

I have two sheets in an Excel file and need to perform a COUNTIF formula from one sheet and paste the respective information in another sheet. The original sheet just has the type in 1st column with an empty 2nd column. I am trying to loop through the Type from Sheet 1, in each increment loop through the Type from Sheet 2, and past the Count of column 2 from Sheet 2 into Column 2 of sheet 1.
My current VBA code is as follows:
Sub TestOE()
'For loop to go until end of filled cells in 1st column of each sheet
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
'Loop
For i = 2 To a
For j = 2 To b
If Worksheets("Sheet1").Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
Worksheets("Sheet1").Cells(i, 2).Value = Application.WorksheetFunction.CountIf(Range("B:B"), 1)
End If
Next j
Next i
End Sub
This code is only pasting 0's in the desired outcome on Sheet 1.
Sheet to extract information from
Sheet to paste information in
Desired Outcome in destination sheet
You can simply use sumif function to sum the values based on criteria.
here is the formula
=COUNTIF(Sheet1!$A$2:$A$20,Sheet2!A2)
if you want to sum the col B then
=SUMIF(Sheet1!$A$2:$A$20,Sheet2!A2,Sheet1!$B$2:$B$20)
In a few steps, you can accomplish what you want without VBA, and just use a pivot table. Just do as follows.
Select your data set, including the header.
Click on insert tab, then PivotTable. See example for Office 365
Since you want a different worksheet, set PivotTable to be "New Worksheet" See example.
You'll need to drag the TYPE field into the rows, and binary into the values. CountIF is the same as summing binary, so you can leave as sum. See Example
And you'll have an output nearly identical to what you're looking for:

Autofill dynamic range based on another worksheet

I've got 2 sheets in a workbook with different sets of data. Sheet 1 has a set of data not formatted as a table. The header row for this data is on Row 4. When I try doing a count using Range("A" & Rows.Count).End(xlUp).Row, I get the last row of that entire sheet, not the count of how many rows of data there are from my starting point.
Sheet 2 has its header row in Row 1. So when I try to use the same count of rows I mentioned above to AutoFill, I always get 3 extra rows because the Sheet 1 count is just looking at what the last row of data is.
I don't want to have to shift things around in either sheet. I just want to be able to autofill based on the same number of rows as there are in Sheet 1, beginning the count at A5 and going down to the last row of data. Is there a different count formula to start at a specific cell and only count the rows below it, and then telling the other sheet to only AutoFill based on that number that I'm missing?
You may be over thinking this. Just discount the row count by 3
Range("A" & Rows.Count).End(xlUp).Row - 3
I would qualify the objects & properties (Range & Row) as well for good measure.
could you use either:
set rng = Range("A1:A1000").SpecialCells(xlCellTypeConstants, 2)
or
set rng = Range("A5").CurrentRegion.Resize(,1)
assuming no blanks in the data range

Adjust criteria to depend on multiple values

I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub

select row based on cell value

I am working on a macro to move data from one sheet to another based on matching cell values.
Let's say I have 2 sheets, Sheet1 & Sheet2, respectively.
Sheet1 contains data that I wanted to be copied into Sheet2.
Sheet2 contains a value in column "C", and this value with have multiple matches in column "C" of
Sheet1 (which are already sorted and same values are grouped together).
My overall goal is to copy cells from Sheet1 to Sheet2 based on matching values in column "C". I want to insert these values one row below the row with matching column "C" values.
The difficulty lies in the fact that the range of values copied from Sheet1 to Sheet2 will differ with each different value in Column "c" of Sheet2, because there will be a different number of rows with respect to a particular cell value.
(I would show a simple picture for this, but it won't allow me to post a picture due to low post count - I can email this if needed for clarification)
I am okay with basic macro stuff and rely on the Macro Record for some stuff as well. But with my current knowledge and lack of the macro recorder's ability to make a macro like this, I am just stumped!
My request:
Help with macro that selects a range of cells based on matching cell values to copy
Help with inserting the copied range starting 1 row below the cell value of interest (cell value is row 2, insert cells starting at row 3)
Have this repeated for each value listed in Sheet2
I think I can figure the basic coding with this. If I can just get help with the particular string that does what I am looking for would be great! I am not trying to just be handed the answer, but I have been working on this issue for 8+hrs and can't find anything online that is similar to this...
This code assumes that you have sorted the data as you have in the example:
Sub transfer()
'If everything is sorted, you can do it like this:
Dim x, y 'x is the sheet1 row, y is the sheet2 row
y = 2 'they start at the same place x = 2, y = 2
For x = 2 To Sheets(1).Cells(Sheets(1).Rows.Count, "A").End(xlUp).Row
If Sheets(1).Cells(x, 1) = Sheets(2).Cells(y, 1) Then 'If the cell value matches
Sheets(1).Range("A" & x).Copy 'Copy the cell value from Sheet1
Sheets(2).Cells(y + 1, 1).Insert Shift:=xlDown 'And insert it below the Sheet2 Cell
'Then copy the rest of the data (columns C and D)
Sheets(1).Range("C" & x & ":D" & x).Copy Destination:=Sheets(2).Cells(y + 1, 2)
Else
x = x - 1 'We haven't found a match for this cell yet so check it again
End If
y = y + 1 'After incrementing y
Next x
End Sub
Sorry for slow reply - I can explain the code to you soon if need be!
Hope this helps! :)
I wrote this specifically for the example you gave me, so hopefully you are able to build upon this concept if your needs change.

apply vlookup formula for variable no.of rows

I have an excel sheet in which the first 3 columns are a pivot table and whenever I refresh it, the no. of rows may change. Columns E,F,G,H,I use a VLOOKUP formula based on Columns A,B,C.
Since the no. of rows are changing, how can I make sure that the vlookup formula also automatically adjusts for columns E,F,G,H,I based on no. of rows of A,B,C ?
Attached is an image in which you can see that the formula didn't apply for last 3 rows and I have manually drag the cells if it were to work.
Somewhat brute force would be to do something like
' Just Refreshed Pivot Table
'
' Assumes variable ws refers to this worksheet
' Assumes your formulas start on row 2
ws.Range("E2:I2").Copy ws.Range("E3:E" & ws.UsedRange.Rows.Count)
Then put an if statement around your formula in column E (and similar if statements on the other columns):
=If(C2="","",VLookup(...))
You can always do a more robust way to determine how many rows are on the left versus the right. Or step through like:
For i = 1 to ws.UsedRange.Rows.Count
If(ws.Cells(i,3).Value = "")
ws.Range("E" & i & ":I" & i).ClearContents
Else
''' Apply your formulas / values in columns E through I of row i
End If
Next i