Thank you in advance and sorry for the bad english!
I want
fix100-->current column & last row nummber?
Cells(100, ActiveCell.Column))--->Cells(???, ActiveCell.Column))
Sub ExcelVBA_CurrentValuecu_Filter()
ActiveSheet.Range(Cells(1, ActiveCell.Column), Cells(100, ActiveCell.Column)).AutoFilter Field:=1, Criteria1:=ActiveCell.Value
End Sub
Try this code:
Sub ExcelVBA_CurrentValuecu_Filter()
With ThisWorkbook.ActiveSheet
.Range(.Cells(1, ActiveCell.Column), .Cells(.Rows.Count, ActiveCell.Column).End(xlUp)). _
AutoFilter Field:=1, Criteria1:=ActiveCell.Value
End With
End Sub
The statement .Cells(.Rows.Count, ActiveCell.Column).End(xlUp) will find the last cell in your column that has data in it. I used a With block to properly qualify all the ranges you're using in your code. That's why there's a dot . in front of .Cells. This is the same as always writing ActiveSheet.Cells.
Related
Having tried for many hours without a solution, I am asking for help to please clear the contents of Columns A through H from first row where Col G = - to row 50000. I have tried many approaches without success. Users currently have instructions to do this manually, but I sure wish it could be automated by adding it to the code below. Deleting the rows is no good because it upsets array formulas elsewhere that use this data.
Sub CopyPasteToPrYrData()
'
' CopyPasteToPrYrData Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
Sheets("Barrel List by Producer").Range("AD3:AK30000").Copy
Sheets("Prior Years Data").Range("A1:H29998").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Sheet1.Activate
Range("A1:B29998").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("A1:H29998").Sort Key1:=Range("G1"), Order1:=xlDescending, Header:=xlNo
End Sub
I tried creating a concatenated range formula in Excel from calculated Find values, code to copy the first row with only a space in it and copying that down, building a range formula in VBA instead of Excel, and various iterations of those until I gave up on my ability to solve the problem.
you can use AutoFilter() to filter negative values and clear them
here's a possible code, where I also refactored your existing one to add more consistency
Sheets("Barrel List by Producer").Range("AD3:AK17").Copy
With Sheets("Prior Years Data")
.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
With .Range("A1").CurrentRegion
.NumberFormat = "General"
.Value = .Value
.Sort Key1:=.Range("G1"), Order1:=xlDescending, Header:=xlNo
.AutoFilter field:=7, Criteria1:="<0"
With .Resize(.Rows.Count - 1).Offset(1)
If CBool(Application.Subtotal(103, .Columns(1))) Then
.SpecialCells(xlCellTypeVisible).ClearContents
End If
End With
End With
.AutoFilterMode = False
End With
Sub CopyPasteToPrYrData()
'
' CopyPasteToPrYrData Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
Sheets("Barrel List by Producer").Range("AD3:AK30000").Copy
Sheets("Prior Years Data").Range("A1:H29998").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Sheet1.Activate
Range("A1:B29998").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("A1:H29998").Sort Key1:=Range("G1"), Order1:=xlDescending, Header:=xlNo
Columns("G:G").Select
Selection.Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
End Sub
May I ask, why does it always show errors whenever I run this script?
Sub Filtering()
With Worksheets("Sheet1").Range("A1:N2836")
.AutoFilter Field:=2, Criteria1:=Worksheets("Sheet2").Cells(1, 1).Value
.AutoFilter Field:=3, Criteria1:=Worksheets("Sheet2").Cells(1, 2).Value
End With
End Sub
Part 1:
Please help to advise where did it went wrong. Would like to sort for column E via descending order with header
Sub SortRows()
Dim destSht As Worksheet
Set destSht = ThisWorkbook.Worksheets("Account Level")
destSht.Sort.SortFields.Clear
Range("E2", destSht.Cells(destSht.Rows.Count, "E").End(xlUp)).Sort Key1:=Range("E2"),
Header:=xlYes, _
Order1:=xlDescending
End Sub
Part 2:
How should I input the vba code in a way which it could dynamically filter column C based on descending values?
Maybe
destSht.Range("E2", destSht.Cells(destSht.Rows.Count, "E").End(xlUp)).Sort Key1:=destSht.Range("E2"),
Header:=xlYes, _
Order1:=xlDescending
Based on latest comments 12-May-2022
Sub sortRows()
With Worksheets("Account Level")
.sort.SortFields.Clear
.Cells(1, 1).CurrentRegion.sort key1:=.Cells(2, 4), order1:=xlDescending, Header:=xlYes
.Cells(1, 1).CurrentRegion.AutoFilter
End With
End Sub
Remove Duplicated based on the adjacent cell values, with help of VBA
ID|Status
1234|Not Started - Need to be deleted
1234|Completed
3456|Completed
3456|Completed - Need to be deleted
Given your sample data all you have to do is sort by the second column in a ascending order then use Remove Duplicates.
Option Explicit
Sub sortNdedupe()
With Worksheets("sheet4")
With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "B").End(xlUp))
.Cells.Sort Key1:=.Columns(2), Order1:=xlAscending, _
Header:=xlYes
.RemoveDuplicates Columns:=1, Header:=xlYes
End With
End With
End Sub
Select which of the two methods do you prefer and try:
Sub Removeduplicates()
Dim Lastrow As Long
With Worksheets("sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Remove based on based on ID
With .Range("A1" & ":B" & Lastrow)
.Removeduplicates Columns:=1, Header:=xlYes
End With
'Remove based on ID and Status
With .Range("A1" & ":B" & Lastrow)
.Removeduplicates Columns:=Array(1, 2), Header:=xlYes
End With
End With
End Sub
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.