On this line 'PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20"),…
- User
On this line 'PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20"), Columns("AE2:AE20")' in the VBA code below, I am getting an error - application defined or object defined.
Sub DueDates()
Me.Unprotect Password:="edit" ' Unlock the sheet Application.EnableEvents = False Application.ScreenUpdating = False
Dim currentMonth As Long currentMonth = Month(Date) If currentMonth >= 9 And currentMonth <= 12 Then ' Sep to Dec Range("AE1").Value = "Sep - Dec" If Range("I1").Value > 0 Then PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20") End If If Range("J1").Value > 0 Then PerformCopyAndClear Columns("G2:G20"), Columns("B2:B20"), Columns("AE2:AE20") End If ElseIf currentMonth >= 1 And currentMonth <= 3 Then ' Jan to Mar Range("AE1").Value = "Jan - Mar" If Range("J1").Value > 0 Then PerformCopyAndClear Columns("G2:G20"), Columns("B2:B20") End If If Range("H1").Value > 0 Then PerformCopyAndClear Columns("C2:C20"), Columns("D2:D20"), Columns("AE2:AE20") End If ElseIf currentMonth >= 4 And currentMonth <= 8 Then ' Apr to Aug Range("AE1").Value = "Apr - Aug" If Range("H1").Value > 0 Then PerformCopyAndClear Columns("C2:C20"), Columns("D2:D20") End If If Range("I1").Value > 0 Then PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20"), Columns("AE2:AE20") End If End IfApplication.ScreenUpdating = True Application.EnableEvents = True Me.Protect Password:="edit"
End Sub
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant)
Me.Unprotect Password:="edit" Application.EnableEvents = False
sourceRange.Copy
Dim destinationRange As Variant For Each destinationRange In destinationRanges destinationRange.PasteSpecial xlPasteValues Next destinationRange
sourceRange.ClearContents
Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit"
End Sub
- Assistant
The error "Application defined or object defined" is usually caused by a mismatch in the data types being passed to the function.
In this case, the issue is with the data type of the ParamArray destinationRanges() As Variant in the PerformCopyAndClear function. The Columns() function returns a Range object, and it cannot be directly passed as a Variant.
To fix this issue, you can modify the code to treat the destinationRanges as Range objects directly. Here is the updated code:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Range) Me.Unprotect Password:="edit" Application.EnableEvents = False sourceRange.Copy Dim destinationRange As Range For Each destinationRange In destinationRanges destinationRange.PasteSpecial xlPasteValues Next destinationRange sourceRange.ClearContents Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubNow you should be able to call the PerformCopyAndClear function with the Columns() function directly, like in the original code:
PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20"), Columns("AE2:AE20") - User
I am still getting the same error
- Assistant
I see the issue now - the Columns() function returns a range of multiple columns, but the PerformCopyAndClear function expects individual ranges for each destination. To address this, we can loop through the columns in the source range and copy each one individually to the corresponding destination range.
Here's the updated PerformCopyAndClear function:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Range) Me.Unprotect Password:="edit" Application.EnableEvents = False Dim i As Integer For i = 1 To sourceRange.Columns.Count sourceRange.Columns(i).Copy destinationRanges(i - 1).PasteSpecial xlPasteValues sourceRange.Columns(i).ClearContents Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubThis updated function loops through each column in the source range, copies it, pastes the values into the corresponding destination range, and then clears the original data.
Now, you should be able to call the PerformCopyAndClear function with a range of multiple columns as the sourceRange and individual columns as destinationRanges, like so:
- User
I am getting a error: ParamArray must be delcared as an array of Varaiant
- Assistant
I see the issue now. When using a ParamArray in VBA, the parameter must be declared as an array of Variant. To address this, you can declare the destinationRanges parameter as an array of Variant and then convert each element to a Range within the function.
Here's the updated code:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant) Me.Unprotect Password:="edit" Application.EnableEvents = False Dim i As Integer For i = LBound(destinationRanges) To UBound(destinationRanges) sourceRange.Copy destinationRanges(i).PasteSpecial xlPasteValues sourceRange.ClearContents Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubNow, you should be able to call the PerformCopyAndClear function with a range of multiple columns as the sourceRange and individual columns as destinationRanges, like in the original code:
PerformCopyAndClear Columns("E2:E20"), Columns("F2:F20"), Columns("AE2:AE20")This modification should resolve the error you were encountering. If you have any further questions or issues, please let me know.
- User
still getting the same error
- Assistant
I apologize for the oversight. Here's an updated version of the code that should work correctly:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant) Me.Unprotect Password:="edit" Application.EnableEvents = False sourceRange.Copy Dim i As Integer For i = LBound(destinationRanges) To UBound(destinationRanges) If TypeName(destinationRanges(i)) = "String" Then Dim destRange As Range Set destRange = Range(destinationRanges(i)) destRange.PasteSpecial xlPasteValues Else destinationRanges(i).PasteSpecial xlPasteValues End If Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" sourceRange.ClearContents End SubIn this updated version, the code includes a check to handle the case where destinationRanges are passed as strings (using the Range function to convert them to Range objects). This should help in resolving the error you were encountering.
You can now call the PerformCopyAndClear function with a range of multiple columns as the sourceRange and individual column ranges (or column range strings) as destinationRanges, like so:
- User
still the same error
- Assistant
I apologize for the confusion earlier. Let's simplify the implementation by passing the destination ranges as array elements directly, instead of using the Columns function.
Here's the updated code:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant) Me.Unprotect Password:="edit" Application.EnableEvents = False sourceRange.Copy Dim i As Integer For i = LBound(destinationRanges) To UBound(destinationRanges) If TypeName(destinationRanges(i)) = "Range" Then sourceRange.Copy destinationRanges(i).PasteSpecial xlPasteValues sourceRange.ClearContents End If Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubNow, you should call the PerformCopyAndClear function with a range of multiple columns as the sourceRange and individual ranges as destinationRanges, like this:
PerformCopyAndClear Range("E2:E20"), Range("F2:F20"), Range("AE2:AE20")This simplified version should address the error you were encountering. Please try this updated code and let me know if it solves the issue.
- User
still th same error
- Assistant
I'm sorry for the persistence of the error. Let me provide a different approach that may help resolve the issue:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant) Me.Unprotect Password:="edit" Application.EnableEvents = False sourceRange.Copy Dim destinationRange As Range Dim i As Integer For i = LBound(destinationRanges) To UBound(destinationRanges) If TypeName(destinationRanges(i)) = "Range" Then Set destinationRange = destinationRanges(i) destinationRange.PasteSpecial xlPasteValues sourceRange.ClearContents End If Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubIn this adjusted code, each element in the destinationRanges array is now checked to ensure it is a Range object before performing the copy and clear operation. This should help avoid the type mismatch error.
When calling the PerformCopyAndClear function, ensure you pass the ranges as Range objects, like this:
PerformCopyAndClear Range("E2:E20"), Range("F2:F20"), Range("AE2:AE20") - User
I am still getting the application-defined or object-defined error
- Assistant
I see that the error persists, and I apologize for the inconvenience. Another method that we can try is to adjust the way the destinationRanges are passed and handled in the PerformCopyAndClear function.
I will modify the code with this new approach:
Private Sub PerformCopyAndClear(sourceRange As Range, ParamArray destinationRanges() As Variant) Me.Unprotect Password:="edit" Application.EnableEvents = False sourceRange.Copy Dim destinationRange As Range Dim i As Integer For i = LBound(destinationRanges) To UBound(destinationRanges) If TypeName(destinationRanges(i)) <> "Range" Then Set destinationRange = Range(destinationRanges(i)) Else Set destinationRange = destinationRanges(i) End If destinationRange.PasteSpecial xlPasteValues sourceRange.ClearContents Next i Application.CutCopyMode = False Application.EnableEvents = True Me.Protect Password:="edit" End SubPlease try calling the PerformCopyAndClear function with ranges specified as Range objects:
PerformCopyAndClear Range("E2:E20"), Range("F2:F20"), Range("AE2:AE20")