Excel VBA - Fill down to bottom of sheet - excel

I have a macro I've made, but when I run the macro it fills down the updated value to the bottom of the sheet instead of to the bottom of the data set, is there a way to make it only fill down partially?
With ActiveSheet
.AutoFilterMode = False
With Range("I1", Range("I" & Rows.Count).End(xlDown))
.AutoFilter Field:=1, Criteria1:="="
On Error Resume Next
.Resize(.Rows.Count - 1).Offset(1, 0).SpecialCells(xlCellTypeVisible).Value = "1"
On Error GoTo 0
End With
.AutoFilterMode = False
End With

Related

How can I include only particular words when autofiltering a column using VBA

I am trying to include only certain words in a particular column when autofiltering, but am finding a syntax error message, but cannot see where the error is. The column is H, which is column 8, but when I run the macro, it brings up a compile error...syntax error, with the code which has all the words highlighted.
I have a smaller version of the same code working beautifully in another sheet, though it only filters for 4 words and is looking to exclude them, so has .AutoFilter .Columns.Count, "<>X". With this one I wanted to filter out anything except the words I chose, so I copied the code, everything from With to End With, from the other sheet, amended the words and changed the autofilter line to remove the <>
I'm sure someone here will spot the mistake immediately, but for the life of me I cannot see it.
Here is the code
Sub FA_Racing_1()
'
' FA_Racing_1 Macro
' VDW Rank, RnkPFP, Distance, PR Odds, Handicap
'
Dim ws As Worksheet, lc As Long, lr As Long
Set ws = ActiveSheet
'range from A1 to last column header and last row
lc = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lr = ws.Cells.Find("*", after:=ws.Range("A1"), LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With ws.Range("A1", ws.Cells(lr, lc))
With .Cells(2, .Columns.Count).Resize(.Rows.Count - 1)
.FormulaR1C1 = "=if(or(rc8={""Ballinrobe"",""Bellewstown"",""Clonmel"",""Cork"", _
""Curragh"",""Downpatrick"",""Down Royal"",""Dundalk"",""Fairyhouse"", _
""Galway"",""Gowran Park"",""Kilbeggan"",""Killarney"",""Laytown"",""Leopardstown"", _
""Limerick"",""Listowel"",""Naas"",""Navan"",""Punchestown"",""Roscommon"", _
""Sligo"",""Thurles"",""Tipperary"",""Tramore"",""Wexford""}),""X"","""")"
.Value = .Value
End With
.HorizontalAlignment = xlCenter
.AutoFilter .Columns.Count, "X"
.AutoFilter Field:=3, Criteria1:= _
"<>*Handicap*"
.AutoFilter Field:=38, Criteria1:="<=5"
.AutoFilter Field:=24, Criteria1:="=~*", _
Operator:=xlOr, Criteria2:="=~*~*"
.AutoFilter Field:=100, Criteria1:="1"
If .Rows.Count - 1 > 0 Then
On Error Resume Next
.Columns("C:C").EntireColumn.Hidden = True
.Columns("G:G").EntireColumn.Hidden = True
.Columns("I:I").EntireColumn.Hidden = True
.Columns("K:L").EntireColumn.Hidden = True
.Columns("N:W").EntireColumn.Hidden = True
.Columns("Z:AJ").EntireColumn.Hidden = True
.Columns("AN:AO").EntireColumn.Hidden = True
.Columns("AQ:BQ").EntireColumn.Hidden = True
.Columns("BT:CU").EntireColumn.Hidden = True
.Columns("CW:CW").EntireColumn.Hidden = True
.Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy
On Error GoTo 0
Else
Exit Sub
End If
End With
Workbooks("New Results File Active.xlsm").Sheets("FA Racing 1") _
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Any thoughts as to what is causing the error?

Issue with Object Variable or With Block Variable not Set Error Involving adding new sheets and range

Receiving Object Variable Error with ActiveSheet.AutoFilter.Range.Copy code line
Any reason why I'm receiving this?
Sub Macro3()
'Create three new sheets with unique names
Sheets.Add.Name = "SOX App-not linked to active KC"
Sheets.Add.Name = "Non SOX App-linked to Active KC"
Sheets.Add.Name = "Active KC - Delisted App"
With Sheets("Report")
.AutoFilterMode = False
With .Range("A1:N20000")
.AutoFilter Field:=7, Criteria1:="Yes"
.AutoFilter Field:=6, Criteria1:=Array("In Development", "In Production", "Projected Retire", "Retain - App and Data", "Retain - Data Only"), Operator:=xlFilterValues
.AutoFilter Field:=11, Criteria1:="Active"
ActiveSheet.AutoFilter.Range.Copy
Sheets("SOX App-not linked to active KC").Select
Range("A1").Select
Sheets("SOX App-not linked to active KC").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
Sheets("SOX App-not linked to active KC").Range("A1").PasteSpecial xlPasteFormats
End With
End With
End Sub
You should not use ActiveSheet there - just
.AutoFilter.Range.Copy
(and move that line up to the sheet With block)
It's already scoped by the With block, and will likely error if "Report" is not the active sheet.
Sub Macro3()
Dim ws As Worksheet
'Create three new sheets with unique names
Set ws = Sheets.Add()
ws.Name = "SOX App-not linked to active KC"
Sheets.Add.Name = "Non SOX App-linked to Active KC"
Sheets.Add.Name = "Active KC - Delisted App"
With Sheets("Report")
.AutoFilterMode = False
With .Range("A1:N20000")
.AutoFilter Field:=7, Criteria1:="Yes"
.AutoFilter Field:=6, Criteria1:=Array("In Development", "In Production", _
"Projected Retire", "Retain - App and Data", _
"Retain - Data Only"), Operator:=xlFilterValues
.AutoFilter Field:=11, Criteria1:="Active"
End With
.AutoFilter.Range.Copy '<<< move this up!
End With
With ws.Range("A1")
.PasteSpecial xlPasteValuesAndNumberFormats
.PasteSpecial xlPasteFormats
End With
End Sub

VBA to stop macro with msg box if no criteria found in auto filter

I have a macro to filter 2 different criteria which is "0" and "1" but I need a msg box pop out and stop the macro if there is no criteria found in the auto filter.
Can you advice how I can do that for below coding?
Thanks.
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$2:$A$8").AutoFilter Field:=1, Criteria1:="=0", _
Operator:=xlOr, Criteria2:="=1"
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("C3").Select
ActiveSheet.Paste
This should warn you if your filter returns no results. Note that I also had it turn off the filter at the end because some of the results (from C3 and below) could be hidden by the filter.
Sub CheckFilter()
''Remove any existing filters
ActiveSheet.AutoFilterMode = False
With Range("A1")
''get range before filter
Dim rngBefore As Range
Set rngBefore = Range(.offset(1, 0), .offset(1, 0).End(xlDown))
''filter
.AutoFilter Field:=1, Criteria1:="=0", Operator:=xlOr, Criteria2:="=1"
''get range after filter
On Error Resume Next
Dim rngAfter As Range
Set rngAfter = rngBefore.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
''check whether any cells matching criteria were found
If rngAfter Is Nothing Then
MsgBox "No cells match filter"
Exit Sub
Else
rngAfter.Copy Range("C3")
End If
End With
''Remove filter
ActiveSheet.AutoFilterMode = False
End Sub

Copy filtered information

I want to copying information from two different workbooks into a third. The code below works for B, but for A it pastes only the first row of information.
I set the destination for A to a different tab of the source workbook and it worked. Then I set the destination to a newly created workbook, and also worked.
When I tried again with the workbook I want the information in, it pastes only the first row.
'open file A
Set W_Book = Workbooks.Open(Folder_Path & A_Rep)
Sheets("A").Activate
'filter out information and copy it
With ActiveSheet
.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter Field:=5, Criteria1:=Start_Date
.UsedRange.AutoFilter Field:=10, Criteria1:="AAA10"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
End With
'paste on the tracker and close the report
Windows("Tracker.xlsm").Activate
Sheets("Sheet A").Range("A1").PasteSpecial
W_Book.Close False
'open file B
Set W_Book = Workbooks.Open(Folder_Path & B_Rep)
'filter out information and copy it
With ActiveSheet
.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter Field:=7, Criteria1:="BBB10"
.UsedRange.AutoFilter Field:=24, Criteria1:="Done"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
End With
'paste on the tracker and close the report
Windows("Tracker.xlsm").Activate
Sheets("Sheet B").Range("A1").PasteSpecial
W_Book.Close False
This is happening because you are using ActiveSheet when filtering the data, but after you open workbook B, you don't specify a sheet to copy, try the code below and it should give you better results, I specified the first worksheet to copy data from, which you may need to amend:
Sub foo()
Dim wbTracker As Workbook: Set wbTracker = Workbook("Tracker.xlsm")
'open file A
Set W_Book = Workbooks.Open(Folder_Path & A_Rep)
'filter out information and copy it
With W_Book.Sheets("A")
.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter Field:=5, Criteria1:=Start_Date
.UsedRange.AutoFilter Field:=10, Criteria1:="AAA10"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
End With
'paste on the tracker and close the report
wbTracker.Sheets("Sheet A").Range("A1").PasteSpecial
W_Book.Close False
'open file B
Set W_Book = Workbooks.Open(Folder_Path & B_Rep)
'filter out information and copy it
With W_Book.Sheets(1)
.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter Field:=7, Criteria1:="BBB10"
.UsedRange.AutoFilter Field:=24, Criteria1:="Done"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
End With
'paste on the tracker and close the report
wbTracker.Sheets("Sheet B").Range("A1").PasteSpecial
W_Book.Close False
End Sub

MS excel button Copy Insert & delete

I had a coding suggested by others, but i no fully match my requirements. What i need are "INSERT" those data to another worksheet rather than "PASTE" them, because when using "PASTE" previous data will be overwrite.
Below are the coding, any can help to change the "PASTE" to "INSERT"? I had try many times also error.
Private Sub All_Click()
With Worksheets("Sheet1")
'turn off AutoFilter if it is on
If .AutoFilterMode Then .AutoFilterMode = False
'set a CF rule for <Now
With .Range(.Cells(2, "L"), .Cells(Rows.Count, "L").End(xlUp))
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression,Formula1:="=$L2<NOW()")
.Font.Color = vbRed
End With
End With
'add an AutoFilter for red font cells
With .Range(.Cells(1, "L"), .Cells(Rows.Count, "L").End(xlUp))
.AutoFilter Field:=1, Criteria1:=vbRed, _
Operator:=xlFilterFontColor
End With
'deal with the red font cells
With .Range(.Cells(2, "L"), .Cells(Rows.Count, "L").End(xlUp))
If CBool(Application.Subtotal(103, .Cells)) Then
With .SpecialCells(xlCellTypeVisible)
'select them (there are better ways to get things done)
'.Select
'copy them to sheet2 (do not need Select for this)
.EntireRow.Copy Destination:=Sheet2.Range("A4").Rows("1:1")
'delete them
.EntireRow.Delete
End With
End If
End With
'turn off AutoFilter
If .AutoFilterMode Then .AutoFilterMode = False
End With
End Sub
If you want to paste them after existing data on your Sheet2, you need to find the last row on that sheet and set your destination as that row + 1.

Resources