I have a large table of data and my goal is to advanced filter that table by Column B from sheet 2.
I defined my variables as ws0 (where the data I am filtering is) is my Sheet1 and ws02 is my Sheet2 (where the filter criteria is). It's just not working, I can get it to work if i specify the exact ranges but I want this to find the last row on sheet two as that range of data will change. Here is my code: I would also love to be able to find the last from from ws0 as well..but one step at a time :)
Dim LastRow As Long
With ws02
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
ws0.Range("A1:I3000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
ws02.Range("B1").LastRow, Unique:=False
End Sub
If anyone can help I'd greatly appreciate it!!
Here is Sheet 1
Here is Sheet 2
Try changing
CriteriaRange:= _
ws02.Range("B1").LastRow
To
CriteriaRange:= _
ws02.Range("B2:B" & LastRow)
You are attempting to use LastRow as a property or a method of the Range object, but no such property or method exists.
This will use the criteria from cell B2 to the last row in column B.
Related
I am creating an Excel workbook that will use VBA to apply two filters to a sheet of requested items. Column Q contains either 'Hardware' or 'Smartphone', Column R contains either 'Yes' or 'No'. The contents of the other fields don't matter. A filter will be applied to Column Q for 'Hardware' and Column R for 'No'. For each item which meets both criteria, they will be pasted into a specific row, A11, on another tab which contains a header row in A10. Below the rows that have been pasted in, I'd like two blank rows, then a line of text "Your Text Here", and the header row will be repeated. Lastly the Column R filter will be switched to 'Yes' with the Column Q filter staying on 'Hardware'. The rows produced by this filter will be copied in below this second header row.
The Hardware tab will then contain the following,
A1-A9 - Blank, the contents will be added later.
A10 - Header row for the data,
A11-A(X) - Data rows with Column Q = "Hardware" and Column R = "No",
A(X+1)-A(X+2) - Blank rows,
A(X+3) - "Your Text Here",
A(X+4) - Header row for the data,
A(X+5) - Data rows with Column Q = "Hardware" and Column R = "Yes".
I have adapted a VBA script which I found here on this site, many thanks to Siddharth Rout.
How to copy a line in excel using a specific word and pasting to another excel sheet?
When I run the macro, it applies the two filters correctly and copies the correct rows into the 'Hardware' tab. However I want it to be pasted into Cell A11 below the header row, but the data is actually overwriting this and copying into Cell A10.
Because I can't stop overwriting my header row or get the first set in the right place, I'm not sure how to then add what I need below it as well as how to determine where the second set will go.
I have tried changing the value in 'After:=.Range("A11"), _' but I am quite possibly misunderstanding what it does. I want it to always go in starting at row A11 and not touch anything in rows 10 and above even if they are blank.
Dim wb1 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Raw")
With ws1
.AutoFilterMode = False
lRow = .Range("Q" & .Rows.Count).End(xlUp).Row
With .Range("Q1:R" & lRow)
.AutoFilter Field:=1, Criteria1:="Hardware" '-> Hardware Items
.AutoFilter Field:=2, Criteria1:="No" '-> Not Business Continuity
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
.AutoFilterMode = False
End With
'-> Destination
Set ws2 = wb1.Worksheets("Hardware")
With ws2
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A11"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
copyFrom.Copy .Rows(lRow)
End With
wb1.Save
End Sub
Many thanks to anyone who may respond and apologies if my summary could be better.
If you know that you have to paste the range in A11, you can directly paste it there.
Instead of using find for lrow ,inside the With ws2 do
With ws2
copyFrom.Copy .Range("A11")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
This will paste the range in row A11 then you can get the next last row . Get X by (define it as Dim lrow as Long).
Now you have the lastrow that contains data (X). As you stated the next data row is X + 5 . First clear the initial filter by
ActiveSheet.AutoFilter.ShowAllData
So adjust the code to do the other filter with criteria "Yes" and then paste it by
With ws2
copyFrom.Copy .Range("A" & lrow + 5)
I have data in an excel spreadsheet and I am trying to create a pivot table of the data where it is able to change from week to week as the data will change. Right now my code works but it is constant where it says “GL ENTERED INV raw data!R3C1:R30C06” how do I get this to change from week to week?
Sub ptable
Range(“A4:F” & Rows.Count).End(xlUp).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDataBase, SourceData:=“GL ENTERED INV raw data!R3C1:R30C06”, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=“Sheet1!R3C1”, TableName:=“PivotTable1”, DefaultVersion:=xlPivotTableVersion12
End sub
You need to declare a variable holding the last row of your data and replace the "R30" with R & LastRow like this:
Sub ptable()
Dim LastRow As Long
With ThisWorkbook.Sheets("GL ENTERED INV raw data")
LastRow = .Cells(.Rows.Count, 1).End(xlUp) 'the 1 means column "A" so if your data has the max rows on column A is ok, otherwise change that number
ThisWorkbook.Sheets.Add
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="GL ENTERED INV raw data!R3C1:R" & LastRow & "C6", _
Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=“Sheet1!R3C1”, _
TableName:=“PivotTable1”, DefaultVersion:=xlPivotTableVersion12
End Sub
You could also just create a NamedRangeunder the ribbon Formulas and use the Offset formula to get a dynamic range, your pivot table would always use as source that NamedRange
I am looking for a method that will select only cells which contain data between TWO columns. I can't wrap my head around the logic I need to accomplish this.
I am thinking that this is my best bet, but am open to other suggestions.
Sheet1.Columns("A3:B1000").SpecialCells(xlCellTypeConstants, 23).Select
With this code I can select the range that contains a value, however it doesn't work simultaneously between the two columns. If column A has data but column B does not, it will still select column A.
Below is what I am looking to do.
The following code will do what you expect by filtering any blank cells and then selecting all visible cells, for my example, I used columns A & B, amend this as required.
NOTE: I agree with comments from CallumDA, you would usually want to avoid selecting anything, but yet the example code below will show you how to add that given range to a variable, should you want to do something with it, rather than just select it.
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
Dim rng As Range
ws.Range("$A$1:$B$" & Lastrow).AutoFilter Field:=1, Criteria1:="<>"
ws.Range("$A$1:$B$" & Lastrow).AutoFilter Field:=2, Criteria1:="<>"
Set rng = ws.Range("A2:B" & Lastrow).SpecialCells(xlCellTypeVisible)
ws.Range("$A$1:$B$" & Lastrow).AutoFilter
rng.Select
End Sub
In the image I resume a little bit what I need, the names will be different but repetitive, so I need to resume in a second cell the names.
Screenshot
I am not sure if I got your question right, but if you want unique values from a column,
try copying the data to a new column and then data --> data tools --> remove duplicates
Found a solution with VBA, assuming columns are A and B
Sub findDuplicates()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).row
Sheets("Viajes").Range("A3:A" & lastrow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Sheets("Viajes").Range("B3"), _
Unique:=True
End Sub
I am trying to produce some code that will use the AutoFill Down function. I would like to fill Column B with the typical 1,2,3,4, etc. as long as there is text/values in the respective rows of Column A. Right now, my code is hardwired to fill down to cell B50 no matter what, but I don't want it to paste that far down if Column A only has data through cell A7, for example. Ideally, I would like to use the following variable -- rownum = Range("A1").End(xlDown).Row -- to count the number of cells in Column A that have text/values, and use that to replace "B50" in the row designation below. Just not sure of the appropriate syntax to make that happen. Here is the code I have so far.
ActiveCell.FormulaR1C1 = "1"
Range("B2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A1").Select
Range("B1:B2").Select
Selection.AutoFill Destination:=Range("B1:B50"), Type:=xlFillDefault
Thanks in advance to anyone who helps me out! I am a new user of both Macros and VBA Code, and the amount of knowledge that so many of you have amazes me!
Econ
You're much better off if you don't select anything in your code. If you just want to number the rows in column B based on the contents of column A, this will do the trick:
Sub NumberColumn()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
' get the last row from column A that has a value
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
' use the last row to determine how far down to extend the formula
ws.Range("B1:B" & lastRow).Formula = "=row()"
End Sub
Pretty simple option to use without the need for a string:
Selection.AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Same goes for copying the number of rows in a column:
Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row).Select
Selection.Copy