I am trying to remove duplicates from a table in Excel, I have a piece of code that removes duplicates without any problem, I am wondering if I could make it prompt a message box when a duplicate is found saying something along the lines "This entry is a duplicate entry" Any suggestions? This Is what I got so far:
Sub AccessTransfer()
Range("A1:F1").Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 6).Value = "Oven"
Range("A65536").End(xlUp).Offset(1, 0).Select
Call GoDupe
Sheets("Sheet1").Select
Application.CutCopyMode = False
End Sub
Sub GoDupe()
Cells.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Range("A65536").End(xlUp).Offset(1, 0).Select
End Sub
Rather than looping through, identifying and prompting for each duplicate, you could simply highlight all duplicates and prompt the user once.
Your GoDupe() sub could look something like this:
Sub GoDupe()
Cells.FormatConditions.AddUniqueValues
With Cells.FormatConditions(Cells.FormatConditions.Count)
.DupeUnique = xlDuplicate
.Interior.Color = RGB(255, 0, 0)
End With
If MsgBox("Red highlighted cells are duplicated. OK to remove duplicates?", vbOKCancel) = vbOK Then
Cells.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Range("A65536").End(xlUp).Offset(1, 0).Select
End If
Cells.FormatConditions(Cells.FormatConditions.Count).Delete
End Sub
Related
I need to create a loop for this macro:
Sub Site_No()
' Site_No Macro
' Keyboard Shortcut: Ctrl+Shift+J
Range("D2").Select
Selection.Copy
Sheets("Spray Sheet").Select
Range("F5:J6").Select
ActiveSheet.Paste
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
Ideally I'd like to loop it through from D2 - D79.
If someone could show me where to place the loop i'd be very grateful
Ollie
I will not provide you the code, but will give you some hints to achieve your goal.
Sub Site_No()
'
' Start your loop here from D1 to D79. I suggest to use Cells(Row, Column) notation.
Range("D2").Select 'There is no need to select the cell
Selection.Copy 'Instead of Copy, you can use .Value
Sheets("Spray Sheet").Select 'There is no need to select the Sheet
Range("F5:J6").Select 'There is no need to select the cell
ActiveSheet.Paste 'You don't need to paste the value.
'End loop here if you need to print only once.
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
You can see this question for an example to "copy/paste" cell content using .Value.
Copy/Paste Loop through worksheets to consolidate
Something like this should be enough:
Public Sub TestMe()
Dim lngRow As Long
For lngRow = 2 To 79
ActiveSheet.Cells(lngRow, "D") = Worksheets("Spray Sheet").Cells(lngRow, "F")
Next lngRow
End Sub
Using select and activate is something that you should try to avoid as much as possible - How to avoid using Select in Excel VBA (said by the guy who used ActiveSheet in his code).
i am self taught with VBA and still have a very basic Knowledge but learning Quickly
I Have 3 Sheets in 1 Workbook,
"Purchase Control"
"Material Index Sheet"
"Manufacturing"
This Works Manually but this isnt good as these documents are constantly updated, Column 'P' hold Test Certificates which are acquired later, with this i need the rows to copy to the "Material Index Sheet" and "Manufacturing", if the test Cert is changed it also updates on the next sheets.
i also need something that adds rows for me, instead of having my data be hardcopied B8:P200 which some of these documents dont have 200 rows.
Sub MaterialProcess Code
Sub MaterialProcess()
'Filter Based on Test Certs being Filled in
ActiveSheet.Range("$B$8:$P$202").AutoFilter Field:=15, Criteria1:="<>"
'Copy all Relevant Cells Based Above
Worksheets("Purchase Control").Range("B9:P200").Copy
Worksheets("Material Index Sheet").Range("$B$9:$P$200").PasteSpecial Paste:=xlPasteValues 'it pastes that row and doesnt affect any other row'
Worksheets("Purchase Control").Range("B9:H200").Copy
Worksheets("Manufacturing").Range("B9:H200").PasteSpecial Paste:=xlPasteValues
Worksheets("Purchase Control").Range("$B$8:$P$8").AutoFilter Field:=15
'New Customer
Range("D3:E3").Select 'Merged Cells'
Selection.Copy
Sheets("Material Index Sheet").Select
Range("D3:E3").Select
ActiveSheet.Paste
Sheets("Manufacturing").Select
Range("D3:E3").Select
ActiveSheet.Paste
'Contract Review
Sheets("Purchase Control").Select
Range("D5:E5").Select 'Merged Cells'
Application.CutCopyMode = False
Selection.Copy
Sheets("Material Index Sheet").Select
Range("D5:E5").Select
ActiveSheet.Paste
Sheets("Manufacturing").Select
Range("D5:E5").Select
ActiveSheet.Paste
'Order Num
Sheets("Purchase Control").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Material Index Sheet").Select
Range("G3").Select
ActiveSheet.Paste
Sheets("Manufacturing").Select
Range("G3").Select
ActiveSheet.Paste
'Working Order
Sheets("Purchase Control").Select
Range("G5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Material Index Sheet").Select
Range("G5").Select
ActiveSheet.Paste
Sheets("Manufacturing").Select
Range("G5").Select
ActiveSheet.Paste
'Unit
Sheets("Purchase Control").Select
Range("I4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Material Index Sheet").Select
Range("I4").Select
ActiveSheet.Paste
Sheets("Manufacturing").Select
Range("I4").Select
ActiveSheet.Paste
End Sub
This Private Sub allows us to Track anything that is getting manufactured by colour: Red, Yellow, Green,
I am able to get the Date the cell was doubleclicked To yellow or green, this also forces a comment to be added, any information can be commented for the job.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim MyRange As Range
'(1) Desired Range where it works:
Set MyRange = Range("$I$9:$Q$50")
Cancel = True
'(1) Check if double clicked cell is one where the code should work:
If Not Intersect(Target, MyRange) Is Nothing Then
Custom_ColourChange Target
End If
End Sub
'(2) Changed from default Worksheet_Selection event to Custom Sub:
Private Sub Custom_ColourChange(ByVal Target As Range)
'If the target cell is clear
If Target.Interior.ColorIndex = xlNone Then
'Double Click Turns Red
Target.Interior.ColorIndex = 3
'But if the target cell is already Red
ElseIf Target.Interior.ColorIndex = 3 Then
'Double click Turns Yellow
Target.Interior.ColorIndex = 6
'But if the target cell is already Yellow
ElseIf Target.Interior.ColorIndex = 6 Then
'Then change the background to Green
Target.Interior.ColorIndex = 4
'But if the target cell is already Green
ElseIf Target.Interior.ColorIndex = 4 Then
'Then clear the background color
Target.Interior.ColorIndex = xlNone
End If
End Sub
You can get the last row in a spreadsheet with the following code:
dim wSheet as Worksheet : Set wSheet = ThisWorkbook.Worksheets("SHEETNAME")
dim lastRow : lastRow = wSheet.Cells(wSheet.Rows.Count,"B").End(xlUp).Row
This would allow you to copy only the populated rows with
Worksheets("Purchase Control").Range("B9:P" & lastRow).Copy
You can easily get the date of the double click with a
dateTimeClicked = Now
which can then be pushed into whatever cell/comment/wherever you need
I'm hoping someone can help me with this. I have a spreadsheet with 2 sheets one called Details and another called Reconciled. I have 1000+ rows in Details and I want to cut all rows that have 0 or a - in column E (I want to cut the entire row) and paste it into sheet Details. If possible I would like to copy and paste the headers from Reconciled into Details as well.
I've tried using this code (modified slightly) used in another post
Sub Test()
For Each Cell In Sheets("Details").Range("E:E")
If Cell.Value = "0" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Reconcile").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Details").Select
End If
Next
End Sub
But there are 2 problems. Because some values - (numbers are truly) those get moved, but the ones that are 0.00 do not get moved because they are rounded (I think that's why they are not being moved). Also, the screen updates oddly, and I'm sorry I can't explain it more than.
Any help would be appreciated
Sub Test()
Application.ScreenUpdating = False
On Error Goto Finish
For Each Cell In Sheets("Details").Range("E:E")
If Cell.Value = 0 Or Cell.Value = "-" Then cell.EntireRow.copy Sheets("Reconcile").Rows(cell.Row)
Next
Finish:
Application.ScreenUpdating = True
End Sub
Notice: dont put quotes around the 0, this will make numeric comparison
Using Autofilter:
Public Sub Test()
Application.ScreenUpdating = False
With Worksheets("Details").UsedRange
.Parent.AutoFilterMode = False
.AutoFilter
.AutoFilter Field:=5, Criteria1:="0"
.Copy
With Worksheets("Reconciled").Cells(1, 1)
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
.Parent.Activate: .Select
End With
.Rows(1).Hidden = True
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Rows(1).Hidden = False
.AutoFilter
.Parent.Activate
.Cells(1, 1).Activate
End With
Application.ScreenUpdating = True
End Sub
I used Macro Recorder in Excel to record a series of tasks I need to automate. These tasks happen between two different Excel spreadsheets. I believe that the macro performs the code on the current worksheet, but right now the second sheet is hard coded. How do I prompt the user to choose an Excel sheet to reference to?
The workbook that is going to have the macro is the MasterHardwareDB & the file that needs to be replaced with a user input is Computer&DeploymentInfo_06_23_15_v3.xlsx
I was researching the filedialog object but I am not sure how to integrate here.
Sub AutomateCompare()
'
' AutomateCompare Macro
'
ActiveCell.Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ #HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([#HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-1
ActiveCell.Offset(-2, 1).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([#HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.Offset(1, 0).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-7
ActiveSheet.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, _
Criteria1:="FALSE"
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.Offset(4169, -4).Range("MasterHardwareDB[[#Headers],[Name]:[EmpID]]") _
.Select
ActiveCell.Offset(521, 1).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=-7
ActiveSheet.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, _
Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
ActiveCell.Offset(87, -8).Range("MasterHardwareDB[[#Headers],[Name]]").Select
ActiveWindow.SmallScroll ToRight:=8
End Sub
The easiest way to do this is
shtNum = InputBox("Enter the number of the sheet you want to use.")
With Sheets(shtNum)
Your code here
End With
Also, just so you know, macro recorder code contains a lot of junk. Here is a cleaned-up version of your code.
Sub AutomateCompare()
With ActiveSheet
'Change the ranges to what you need. Using activecell is usually dangerous
.Range("A1:A2").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ #HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.Range("B1").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([#HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, Criteria1:="FALSE"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
End With
End Sub
Edit: Here is code that opens another workbook
Sub AutomateCompare()
Dim fileBrowse As FileDialog
Dim shtNum As Integer
Set fileBrowse = Application.FileDialog(msoFileDialogOpen)
If fileBrowse.Show = True Then wbPath = fileBrowse.SelectedItems(1)
With Workbooks.Open(wbPath)
shtNum = InputBox("Enter the number of the sheet you want to use.")
With .Sheets(shtNum)
'Change the ranges to what you need. Using activecell is usually dangerous
.Range("A1:A2").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[scheduleddate],MATCH([ #HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.Range("B1").FormulaR1C1 = "=INDEX('Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[forecastdate],MATCH([#HostName],'Computer&DeploymentInfo_06_23_15_v3.xlsx'!Table1[computername],0))"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=9, Criteria1:="FALSE"
.ListObjects("MasterHardwareDB").Range.AutoFilter Field:=5, Criteria1:="=Scheduled", Operator:=xlOr, Criteria2:="=To Be Scheduled"
End With
End With
End Sub
I have a big problem and its driving me insane. I have a very simple piece of code that is supposed to copy a row and add it in below the active row plus a validation at the start of the code to check that you are allowed to add the row on that particular line.
The macro works perfectly when you first go in to the sheet. However, as soon as i enter anything in on any of the cells on the sheet the code bombs out with an automation error. Please say someone has found this before and has a fix for it?
The line it doesn't like is as shown here. Selection.Insert Shift:=xlDown
Sub Staffing_AddRow()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveCell.Select
Cells(ActiveCell.Row, 223).Select
If ActiveCell.Value = "Y" Then
ActiveSheet.Unprotect Password:="PasswordGoesHere"
'------------------------------------
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Selection.Insert Shift:=xlDown
'------------------------------------
Cells(ActiveCell.Row, 13).Select
ActiveSheet.Protect Password:="PasswordGoesHere"
Else
If Response = MsgBox("You can't insert a row here!", _
vbCritical, "Warning") Then
Cells(ActiveCell.Row, 13).Select
End If
Cells(ActiveCell.Row, 13).Select
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
when it tries to paste that specific row in the worksheet I get Run-time error '-2147417848 (80010108)': Automation error the object invoked has disconnected from its clients.
Try this: Using With ActiveSheet
Sub Staffing_AddRow()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveCell.Select
'CHANGES BEGIN HERE
With ActiveSheet
If .Cells(ActiveCell.row, 223).Value = "Y" Then
ActiveSheet.Unprotect Password:="PasswordGoesHere"
'------------------------------------
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Selection.Insert Shift:=xlDown
'------------------------------------
.Cells(ActiveCell.row, 13).Select
ActiveSheet.Protect Password:="PasswordGoesHere"
Else
If Response = MsgBox("You can't insert a row here!", _
vbCritical, "Warning") Then
.Cells(ActiveCell.row, 13).Select
End If
.Cells(ActiveCell.row, 13).Select
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End With
End Sub
See also: How to avoid using select statements in macros