Copying and Pasting Row and Column Depending on cell value - excel

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

Related

Excel VBA to Select Rows Down That Include Blanks

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.

Dynamic column reference from heading identified in another Excel sheet

Firstly I have to apologise as this is my first attempt at any VBA coding!
I am trying to find a way of automating a process whereby I extract data from a single data sheet with multiple columns in it into several separate worksheets.
My source data sheet has multiple column headings that are identified by their heading name, column number and column reference.
I am looking for some code that will (for the 2 specified column headings in each of my output sheets:
perform an auto filter selection (=Blanks for column 1 in the operation, <> Blanks for column 2 in the operation)
copy and paste the returned data into a specified output sheet
reset the auto filters in data sheet
move onto the next output sheet, select the next two relevant columns identified in the next output sheet
perform an auto filter selection (=Blanks for column 1 in the operation, <> Blanks for column 2 in the operation)
copy and paste the returned data into a specified output sheet
reset the auto filters in data sheet etc.
I can do this for 1 sheet and repeat but fall over due to the data sheet columns not necessarily remaining in the same order (as new activity milestones are added between the activity I am counting are added).
My crude code is below:
Sub FcastSFR()
'
' FcastSFR Macro
'
'
Application.ScreenUpdating = False
Range("A23:B345").Select
Selection.ClearContents
Sheets("MOAT").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1:$DT$5000").AutoFilter Field:=17, Criteria1:="="
ActiveSheet.Range("$A$1:$DT$5000").AutoFilter Field:=15, Criteria1:="<>"
Range("O1:P631").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SFR Submit").Select
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I am looking for help to make the Field=17/Field=15 and the Range ("O1:P631") be selected from the relevant output sheets.
Help!!

Copy and paste through specific criteria on filter

I'm new to VBA and I'm facing some problems with the code below:
It just need to find all the cells with "Add" on column DE and paste the names which is on the left column DD to the last blank cell on column A. The macro runs ok until the paste line. I don't know if there is another way around it.
ActiveSheet.Range("$DD$16:$DE$200").AutoFilter Field:=109, Criteria1:="Add"
Range("DD16").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'unfilter Macro
ActiveSheet.Range("$A$13:$DE$133").AutoFilter Field:=109
'Paste names marked as "Add"
Range("A14").Select
ActiveCell.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste

Unable to expand table if new values are copied and pasted from another table in vba

I am trying to copy values from one table(table1) and paste it on another table (table2) using vba macro.
Ex: Sheet1 "data" is filtered by header "type" with "blanks" and resulted values are copied except headers and pasted below table2 (next to last row) in sheet2
The issue here is macro is working fine but the table2 where the values are pasted doesn't expand when it is pasted.
I have tried enabling "include rows and column in table" in autocheck option still table is not expanding.
I've tried the following:
range("Check_for_Alias[[#Headers],[Type]]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("Check_for_Alias").range.AutoFilter Field:=18, _
Criteria1:="=" range("Check_for_Alias[Type]").Offset(1, 0).Select
range(Selection, Selection.End(xlDown)).Select
range("Check_for_Alias[[Team]:[Type]]").Select
range("Check_for_Alias[Type]").Offset(1, 0).Activate
Selection.Copy Sheets("Share_point").Select
ActiveSheet.range("C7").End(xlDown).Offset(1, 0).Select
ActiveSheet.paste

What does the range in a VBA filter function refers to

Would like to check under ActiveSheet.Range("$A$1:$O$1583").AutoFilter Field:=7, Criteria1:="=", what does the range $O$1583" refer to?
sub Filter
‘Remove empty cell in column G,K,L’
Range("A2").Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("B2").Select
ActiveSheet.Range("$A$1:$O$1583").AutoFilter Field:=7, Criteria1:="="
ActiveSheet.Range("$A$1:$O$1583").AutoFilter Field:=11, Criteria1:="="
ActiveSheet.Range("$A$1:$O$1583").AutoFilter Field:=12, Criteria1:="="
Rows("10:10").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.ShowAllData
End sub
The ActiveSheet.Range("$A$1:$O$1583") is the range, which is going to be filtered. The A1 is the top left cell and the O1583 is the bottom right. This cell is in column O, row 1583.
The $ signs could be removed, in VBA they are not important. In Excel $ signs means that the row or column which comes after the dollar sign is anchored or absolute. When you copy Excel formulas, they will copy cells referred in that formula relative to the position where they are being copied to.
Try this lines to see the range selected:
Sub TestMe
ActiveSheet.Range("$A$1:$O$1583").Select
End Sub
The range is important, because it shows the filtered range. Imagine this data:
Once you run this Range("A1:G10").AutoFilter, you would see that only the values from 1 to 6 are in the filter and MORE is not present there. However, if you write values in all the cells from the end of the filter to MORE (rows 11,12,13,14) it would be present, as Excel would think that the values have been increased.
Also column-wise you can't filter to a field beyond your range. Range("$A$2:$B$1583").AutoFilter Field:=3 wouldn't work as you'd be looking to filter by column 3 out of a 2 column range.

Resources