I’ve been trying to write some macros to a cross-departmental spreadsheet, which when I press a command button will essentially “archive” a row of work. I also have one which is meant to auto-capitalise a column when people type in it. See below:
This is the Archive macro:
Sub Archive() If MsgBox("Do you want to archive the selected row?" & vbNewLine & vbNewLine & "Row should only be archived after x has passed.", vbYesNo, "Archive") = vbNo Then Exit Sub For Each WSheet In ActiveWorkbook.Worksheets If WSheet.AutoFilterMode Then If WSheet.FilterMode Then WSheet.ShowAllData End If End If For Each DTable In WSheet.ListObjects If DTable.ShowAutoFilter Then DTable.Range.AutoFilter DTable.Range.AutoFilter End If Next DTable Next WSheet 'Declare variables Dim sht1 As Worksheet Dim sht2 As Worksheet Dim lastRow As Long 'Set variables Set sht1 = Sheets("xDepartment") Set sht2 = Sheets("Archive") 'Select Entire Row Selection.EntireRow.Select 'Move row to destination sheet & Delete source row lastRow = sht2.Range("A" & sht2.Rows.Count).End(xlUp).Row With Selection .Copy Destination:=sht2.Range("A" & lastRow + 1) .EntireRow.Delete End With End Sub
The autocapitalisation macro is attached to the specific sheet? (i.e., it’s attached when right-clicking on “xDepartment” and selecting “View code” – not sure if that has something to do with it?). There’s also a macro on this sheet which calculates the date that data in a certain cell is changed:
Private Sub Worksheet_Change(ByVal Target As Range) Dim A1 As Range Set A1 = Range("O:O,Q:Q,T:T,W:W") If Not Intersect(Target, A1) Is Nothing Then Application.EnableEvents = False Target.Value = UCase(Target.Value) Application.EnableEvents = True End If Dim WorkRng As Range Dim Rng As Range Dim xOffsetColumn As Integer Set WorkRng = Intersect(Application.ActiveSheet.Range("O:O, Q:Q"), Target) xOffsetColumn = 1 If Not WorkRng Is Nothing Then Application.EnableEvents = False For Each Rng In WorkRng If Not VBA.IsEmpty(Rng.Value) Then Rng.Offset(0, xOffsetColumn).Value = Now Rng.Offset(0, xOffsetColumn).NumberFormat = "dd/mm/yyyy" Else Rng.Offset(0, xOffsetColumn).ClearContents End If Next Application.EnableEvents = True End If End Sub
The error that comes up is “Run-time error ’13’: Type mismatch”. Do you know why this might be happening?
Thanks in advance!
After your First line in the Archive macro put
Application.EnableEvents = False On error goto Whoops
Then just above your End Sub for that macro put
Whoops: Application.EnableEvents = True
This will turn off the other macro while your archive is running
Your code to move the row from xDepartment worksheet to Archive worksheet includes the line,
This makes the xDepartment worksheet active. The code to actually move the row and remove the original does nothing to change the xDepartment as the ActiveSheet.
With Selection .Copy Destination:=sht2.Range("A" & lastRow + 1) .EntireRow.Delete End With
In your worksheet_change, you have,
Set WorkRng = Intersect(Application.ActiveSheet.Range("O:O, Q:Q"), Target)
So your Archive worksheet’s Worksheet_Change is going to try to work on the xDepartment worksheet.
But you delete the row so it no longer exists; hen ce:
Run-time error ’13’: Type mismatch
Set your WorkRng with,
Set WorkRng = Intersect(Range("O:O, Q:Q"), Target)
It is in a private sub procedure on the Archive’s private code sheet so there is no need to specify a parent worksheet unless you specifically want to work on another worksheet.
Avoid the use of ActiveSheet, Select, Selection and Activate whenever possible and never use them in a worksheet’s private code sheet to refer to that worksheet.