I am new to excel macros and would like to create loop that identified a column range and hopefully a row range to remove duplicates. Currently I have taken the long way around that created an excel formula to create the macro script for the below.
Any help with the below would be much appreciated cause I am now at 60 columns and need to add another 40...
Thank you
Sheets("Result").Select
Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$100000").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("B:B").Select
ActiveSheet.Range("$B$1:$B$100000").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("C:C").Select
ActiveSheet.Range("$C$1:$C$100000").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("D:D").Select
ActiveSheet.Range("$D$1:$D$100000").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("E:E").Select
ActiveSheet.Range("$E$1:$E$100000").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("F:F").Select
ActiveSheet.Range("$F$1:$F$100000").RemoveDuplicates Columns:=1, Header:=xlNo
This will work if you only care about duplicates per column.
Sub RemoveDuplicates()
'Stop the screen from updating to reduce lag
Application.ScreenUpdating = False
'Main loop
For i = 1 To 100
ActiveWorkbook.Sheets("Result").Columns(i).RemoveDuplicates Columns:=1, Header:=xlNo
Next
'Reset ScreenUpdating
Application.ScreenUpdating = True
End Sub
You need a loop!
Sub RemoveDuplicates()
Dim TargetSheet As Worksheet
Set TargetSheet = ActiveWorkbook.Worksheets("Result")
For i = 1 To 100
TargetSheet.Cells(1, i).EntireColumn.RemoveDuplicates Columns:=1, Header:=xlNo
Next i
End Sub
This will perform a column-by-column duplicate removal:
Sub Kleanup()
For i = 1 To Columns.Count
Columns(i).Cells.RemoveDuplicates Columns:=1, Header:=xlNo
Next i
End Sub
Related
So I'm trying to format to all sheets apart from the "Names" sheet. and what I came up with below doesn't seem to be able to loop and detect the sheet "Names". It will try to format "Names" the said sheet is active or it will only apply format a single other sheets when the sheets is active
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Names" Then
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$Q$19").AutoFilter Field:=4, Criteria1:="="
Rows("2:2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$Q$16").AutoFilter Field:=4
Columns("G:G").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
Range("J15").Select
End If
Next ws
I've tried rewriting the codes completely but the same problem persists
In addition to removing Activesheet, rewriting to avoid .select, and maybe considering an alternative to Criteria1:="=" (as already mentioned);
Consider using a With statement to definitely connect each action to the current sheet.
Sub Format_Worksheets()
Dim WS As Worksheet
Dim lRow As Long
Dim lCol As Long
For Each WS In ThisWorkbook.Worksheets
If WS.Name <> "Names" Then
With WS
.Rows("1:1").AutoFilter
.Range("$A$1:$Q$19").AutoFilter Field:=4, Criteria1:="="
lRow = .Range("A2").End(xlDown).Row
lCol = .Range("A2").End(xlToRight).Column
.Range(.Cells(lRow, 1), .Cells(lRow, lCol)).Delete shift:=xlUp
.Range("$A$1:$Q$16").AutoFilter Field:=4
lCol = .Range("G1").End(xlToRight).Column
.Range("G1", .Cells(1, lCol)).Delete shift:=xlToLeft
End With
End If
Next WS
End Sub
Let me know if this works out for you. It did for me... but I'm not 100% sure the formatting will match what your did. I rewrote it without .select or .activate but sometimes it's hard to tell without looking at the data.
In an Excel workbook, I have two worksheets with similar structures.
I wrote VBA code that:
converts format from the text to the data in a range;
sorts the date in a range from oldest to the newest;
filters in a range by the specific characters (the full name of the head of the department, e.g. J.S.Doe);
makes active and moves the view to the top left corner cell in both worksheets;
goes to the next worksheet and repeats the code, then goes to the previous worksheet.
Sub SuperiorsOrders()
Application.ScreenUpdating = False
Range("I3", Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
Range("A3", "J3").End(xlDown).Sort [I2], xlAscending, Header:=xlYes
Range("A3", "J3").End(xlDown).AutoFilter Field:=8, Criteria1:="J.S.Doe"
Range("A1").Select
Application.GoTo ActiveSheet.Range("A1"), Scroll:=True
ActiveSheet.Next.Select
Range("I3", Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
Range("A3", "J3").End(xlDown).Sort [I2], xlAscending, Header:=xlYes
Range("A3", "J3").End(xlDown).AutoFilter Field:=8, Criteria1:="J.S.Doe"
Range("A1").Select
Application.GoTo ActiveSheet.Range("A1"), Scroll:=True
ActiveSheet.Previous.Select
Application.ScreenUpdating = True
End Sub
To reduce the code, I tried to wrap it into the For Each loop statement. It still works, but only for active worksheet, not for all of them.
Sub SuperiorsOrders()
Application.ScreenUpdating = False
Dim WS As Worksheet
For Each WS In Worksheets
Range("I3", Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
Range("A3", "J3").End(xlDown).Sort [I2], xlAscending, Header:=xlYes
Range("A3", "J3").End(xlDown).AutoFilter Field:=8, Criteria1:="J.S.Doe"
Range("A1").Select
Application.GoTo ActiveSheet.Range("A1"), Scroll:=True
Next WS
Application.ScreenUpdating = True
End Sub
I searched the internet, including similar questions here, but it does not work for me.
You have to add the worksheet reference to the range in the loop otherwise Range always refers to the active sheet
ws.Range("I3", ws.Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
' add the remaining lines of code starting with ws.
or
With ws
.Range("I3", .Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
' add the remaing lines of code in the same way
End With
So your code would look like that
Sub SuperiorsOrders()
Application.ScreenUpdating = False
Dim WS As Worksheet
For Each WS In Worksheets
With WS
.Range("I3", .Range("I3").End(xlDown)).TextToColumns FieldInfo:=Array(1, 4)
.Range("A3", "J3").End(xlDown).Sort [I2], xlAscending, Header:=xlYes
.Range("A3", "J3").End(xlDown).AutoFilter Field:=8, Criteria1:="J.S.Doe"
' .Range("A1").Select That is not necessary
End With
'Application.GoTo ActiveSheet.Range("A1"), Scroll:=True <= What is that good for?
Next WS
Application.ScreenUpdating = True
End Sub
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.
I am having some difficulties with macros in Excel. I have a built a call logger in a workbook containing 2 macros:
macro A moves all the data from worksheet A into worksheet B;
macro B automatically sorts the data so one column is always in descending alphabetical order.
However, I can't get both macros to work at the same time. They both work individually but when I try to implement one macro into a workbook containing the other they seem to cancel each other out. Where am I going wrong? Is there a way of possibly combining the 2 macros, for example? Macros are below.
Macro A:
Sub Macro6()
' Macro6 Macro
Application.ScreenUpdating = False
Sheets("Logger").Select
Range("B4:I4").Select
Selection.Copy
Sheets("DATA").Select
Range("B4").Select
lMaxRows = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & lMaxRows + 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Logger").Select
Range("B4:I4").Select
Selection.ClearContents
End Sub
Macro B:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("H:H")) Is Nothing Then
Range("H3").Sort Key1:=Range("H4"), _
Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
Thank you in advance for your assistance with this.
I have a few thousand columns of data and I need to remove the duplicate records in each individual column before looking at the next column. I have this code that was created when I recorded the Macro, but it's only doing the columns that I specifically entered, and I want it to continue looking at future columns until there is no more data.
Sub DUPLICATE()
'
' DUPLICATE Macro
'
' Keyboard Shortcut: Ctrl+d
'
ActiveSheet.Range("$T$1:$T$12").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("U:U").Select
ActiveSheet.Range("$U$1:$U$12").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("V:V").Select
ActiveSheet.Range("$V$1:$V$12").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("W:W").Select
ActiveSheet.Range("$W$1:$W$12").RemoveDuplicates Columns:=1, Header:=xlNo
Columns("X:X").Select
ActiveSheet.Range("$X$1:$X$12").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
This should do the trick:
Sub DeleteDublicates()
Dim i As Integer
For i = 1 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column Step 1
ActiveSheet.Range(Cells(1, i), Cells(Cells(Rows.Count, i).End(xlUp).row, i)).RemoveDuplicates Columns:=1, Header:=xlNo
Next i
End Sub