I'm trying to create a macro to copy cells down an entire column of a table in hopes of acting as a 'refresh' in case those formulas were altered or replaced.
I have multiple tables in the same sheet so I can't select the table name because they constantly change.
What I'm thinking of is having a bottom row with a keyword that VBA can select down until they hit the keyword and select those cells to copy the formulas down.
The thing is that I have multiple tables and they would all have the bottom row of keywords.
When I recorded a macro, I have to Control+Shift+Down multiple times to account for missing rows which I imagine wouldn't always be the case. This is what scares me for this macro since sometimes a table would have no missing data so the xlDown function would select more data than it should.
Here is what I recorded:
Sub Macro9()
'
' Macro9 Macro
'
'
ActiveCell.Offset(3, 2).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
End Sub
Here is an example of the column I am trying to restore formulas on:
I want the formula below "Total Price" to fill down until it hits the word "Total". Note that formulas are hidden if there is no data elsewhere in the sheet.
There are multiple tables on this sheet so this would have to work in different sections of te same sheet.
If you have actual Tables/Listobjects then you could do something like this:
Sub FillDownFormulas()
Dim lo As ListObject, col As Range
For Each lo In ActiveSheet.ListObjects
For Each col In lo.DataBodyRange.Columns
If col.Cells(1).HasFormula Then 'first cell in column has a formula?
col.Formula = col.Cells(1).Formula 'fill formula to rest of column
End If
Next col 'next column
Next lo 'next table
End Sub
One of the most simple ways (for people that usually don't use VBA) is to get the number of the last row in your table. You can do that by counting values in table or with your own code by using a column that is always filled, like:
last_row = Range("B2").end(xldown).row
With last_row value you can fill your formula in ranges, like:
Range("C2").value = 'Your Formula here
Range("C2").AutoFill Destination:=Range("C2:C" & last_row)
You can do that to every column that you want.
Related
I want to write a macro that will insert a new column into a table, enter a formula in the first cell after the header, and paste it to all cells in that column to the bottom of the table.
My issue is defining the bottom of the table. I can find the last row with data by using the following.
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
And the formula portion will always start in this column and works for the first cell:
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]=R[-1]C[-1]"
Range("E2").Select
Here is the issue; ideally I'd want the range to start at "E1" (or Selection since its already there) and end at the variable # of rows the table ends at, NOT E10.
ActiveCell.Select
Selection.Copy
Range(Selection, "E10").Select
ActiveSheet.Paste
I've tried replacing "E10" with just the LastRow variable but I see that wont work since it's just a number. Any help would be appreciated!
(I've also tried this range, but since its a new column there's no way Excel knows where the bottom of the table is with ctrl shift down; maybe it can be tweaked/altered to work?):
Range(Selection, Selection.End(xlDown)).Select
Range("E2").Resize(LastRow - 1).FormulaR1C1 = "=RC[-1]=R[-1]C[-1]"
You don't have to Select - just change the FormulaR1C1 property of the whole range. The -1 part of the Resize argument is because you are in row 2. If you started in row 10, it would be LastRow - 9.
I'm trying to create a summary of data (responses) in a separate sheet that pulls a value from a calculated column based on the value in another column.
For example, my data output has Item Type in column C and columns D:L are item descriptions based on type. Every row (response) has an item type in column C (e.g., office supplies), then depending on that item type there is a description (e.g., pens) in the appropriate column (e.g., Office supplies item description). All other description cells in that row are blank.
What I want is to summarize the data from D:L into one column (e.g., Item description). In other words, I want to summarize all of the non-blank cells in D:L in one column.
I must add: I'm using Google Sheets with the data output from a Google Form. Every time a new response is added to the sheet, a new row is inserted below the previous row, so using reference cells (e.g., C2) doesn't seem to work.
My first time posting here so please let me know if I need to provide more information.
Example:
Response data
End goal/summary
You can use the below macro to achieve this result :
Sub Macro1()
'
' Macro1 Macro
'
Sheets("Response Data").Select
Range("C1").Select
Selection.End(xlDown).Select
LR = ActiveCell.Row
Sheets("Summary").Select
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Response Data'!R1C4:R" & LR & "C12,ROW(Summary!RC[-1]),MATCH(Summary!RC[-1] & "" item description"",'Response Data'!R1C4:R1C12,0))"
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range("B2", "B" & ActiveCell.Row).Select
Selection.FillDown
End Sub
What I was able to do is create a FILTER of the Google Forms output in a separate sheet (which created a copy of the responses that would NOT insert a new row for each entry), then use TEXTJOIN (referencing columns D:L in the new sheet) to get the info in one column on the summary sheet.
Thank you to BigBen for reminding me in the comments of the TEXTJOIN function :)
I’m trying to create VBA that allows me to copy the one of a cell and paste over until end of the row in the same column.
But I’m unable to figure out how this works, I have a total of 109 rows with 20 columns and in column “BD” that is an empty column and I want to put today date and fill up rest of the same column until end of row.
Please see the code at the bottom
Sub CopyInsert()
Range("BD2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("BD2").Select
Selection.Copy
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveSheet.Paste
End Sub
This code is working fine but I’m not getting the right result what these codes do is copy and paste over pass 109 rows mean is paste over up to around 2000 rows
My question is how can I copy and paste over until the end of the row like till row 109! If we have over 200 rows, how to copy and paste until end of the row, row 200 in one column! But not using (BD2:BD), I have tried (BD2:BD) and is paste over 109 rows is not stopped at the end of row.
many thanks.
So you need to find the lastRow based on the column where the actual data is...
Try something like this:
With worksheets("mySheet")
Dim lastRow as Long
lastRow = .Cells(.Rows.Count,1).End(xlUp).Row 'this uses column A for example
.Range("BD2:BD" & lastRow).Formula = "=TODAY()"
End With
I'm looking for a way to have extra cells added to a table, in a sheet with multiple tables. Table A (Column B to F) should get an extra row, under the selected cell. Table B, (Column H to J) should be Fixed: When i add extra cells to row 8 in Table A, Table B should not change. I can only find the EntireRow.Insert, but not for specific cells.
My code so far:
Sub Button1_Click()
Dim anyR As Range
Set anyR = Selection.SpecialCells(xlVisible)
Selection.SpecialCells(xlVisible).Offset(1, 0).EntireRow.Insert
anyR.Offset(1, 0).Select
End Sub
But it adds cells to Table B as well. Thanx for having a look into this!
The following will add a cell beneath the current selection:
Selection.Offset(1, 0).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.SpecialCells(xlVisible).Offset(1, 0).EntireRow.Insert
You're inserting a row in the worksheet - that's like right-clicking the row heading and then selecting "insert".
If you have two tables on the same sheet, with rows at the insertion point, then yeah, both tables will earn an extra row.
If you want to add rows to one table, then work with the table instead of Selection (you hardly ever really need to work with Selection anyway):
Dim table1 As ListObject
Set table1 = Worksheeets("MyAwesomeSheet").ListObjects(1)
table1.ListRows.Add Position:=5, AlwaysInsert:=True
That will insert a new row in the first table of MyAwesomeSheet, making a blank 5th row in that table.
Note that this assumes "table" in the OP is referring to actual tables in the spreadsheet, i.e. that the target sheet has ListObject objects to play with.
I found a way:
Range("A" & ActiveCell.Row & ":L" & ActiveCell.Row).Select
Selection.Copy
Selection.Offset(1, 0).Insert Shift:=xlDown
Range("A" & ActiveCell.Row & ":L" & ActiveCell.Row).Offset(1, 0).ClearContents
ActiveCell.Offset(1, 0).Select
In this case, column A to L are selected.
Thanks for helping!
KR, freek
Hello i need some help with a abit of code, basically i have a workbook which has a lot of data for product Accessories listed in rows in the columns next to these there are the products with blank fields in all of the cells, basically the point of this is when the user types an "x" in any of these cells the table is able to be filtered.
I have made a summary sheet where i want it to look at these specific columns for example columns E-N and if it contains an "x" it will copy the corresponding row which are A,B,C,D and paste it on this summary sheet one after another as well as copying the product from the row above all the x's
e.g
Column E has an x in E4,5,10,15,53
i want it to copy
ABCD4
ABCD5
ABCD10
ABCD15
ABCD53
as well as the product name e.g "melons" which is located just above the first blank box of each column in this case lets say E4 is the first blank so E3 would be the product name.
and paste it in a sheet called "Summary Sheet" one row after another.
I know this is really confusing but i hope you can help :)
code i have:
I now have this code and im almost at where i want to be i need help on the line
Range("A5").Select
It gives me an error but unless i select this cell it pastes randomly on the sheet
Private Sub CommandButton9_Click()
Range("A7:D7").Select
Range(Selection, Selection.End(xlDown)).Select ' Go to last line
' Add a filter behavior
Selection.AutoFilter Field:=5, Criteria1:="<>"
Range("A7:D7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste data where you want
Sheets ("Summary Sheet")
Range("A5").Select
ActiveSheet.Paste
End Sub
The idea could be
Select your set of data
Apply filter to isolate rows with x as value for the filtering
column
Select data and do a copy / paste action
Proposed sample of code (should be adapted to your spreadsheets)
' Select your range of data - maybe
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select ' Go to last line
' Add a filter behavior
Selection.AutoFilter Field:=5, Criteria1:="<>"
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste data where you want
Sheets("Feuil2").Select
Range("A1").PasteSpecial Paste:=xlPasteValues