VBA clearContents function return value changed?

BARROSO TRISTAN, JAVIER RICARDO 20 Puntos de reputación
2024-06-14T08:04:36.67+00:00

Hello,

Until today we had a script which would check if clearContents were ok:

$r = $worksheet.UsedRange.clearContents()
if (-not $r) {
    throw "No se ha podido limpiar las celdas"
}

Today it started to fail and throw were being executed

I finally removed the check and the throw. I am not sure why this behaviour was changed

Thank you

Preguntas y respuestas (Q&A) de Microsoft
Preguntas y respuestas (Q&A) de Microsoft
Use esta etiqueta para compartir sugerencias, solicitudes de características y errores con el equipo de Microsoft Q&A. El equipo de Microsoft Q&A evaluará sus comentarios periódicamente y proporcionará actualizaciones a lo largo del proceso.
401 preguntas
0 comentarios No hay comentarios
{count} votos

1 respuesta

Ordenar por: Lo más útil
  1. Gao Chen 4,550 Puntos de reputación Proveedor de Microsoft
    2024-12-21T00:09:58.3766667+00:00

    Hello BARROSO TRISTAN, JAVIER RICARDO,

    Welcome to Microsoft Q&A!

    We are very sorry for the delay in giving you a response to your topic in this forum. If you have the same inquiry, please kindly note we do not support scripts however the clearContents method might fail for several reasons, including changes in the worksheet's state or environment. Here are a few potential causes and solutions:

    • Worksheet Protection: If the worksheet is protected, the clearContents method might fail. Ensure that the worksheet is unprotected before running the script.
    • Range Issues: If the UsedRange is not correctly defined or if it includes cells that cannot be cleared (e.g., merged cells or cells with data validation), the method might fail.
    • PowerShell Updates: Recent updates to PowerShell or the Excel application might have introduced changes that affect how the clearContents method works.
    • Error Handling: The way errors are handled in your script might need adjustment. Instead of removing the check and throw, consider adding more detailed error handling to understand why the method fails.

    Here's an updated version of your script with enhanced error handling:

    try {
        $r = $worksheet.UsedRange.clearContents()
        if (-not $r) {
            throw "No se ha podido limpiar las celdas"
        }
    } catch {
        Write-Error "Error clearing contents: $_"
    }
    

    This script uses a try-catch block to catch any exceptions and provide more detailed error messages. This can help you diagnose the issue more effectively. If the problem persists, you might want to check for any recent changes in your environment or consult the PowerShell and Excel documentation for any updates or known issues.

    Additionally, please kindly note that if we do not receive a response to this thread within 4 business days, we will consider it closed. In case you have the same question, and the information provided was not useful, feel free to create another thread and we will be more than happy to help you.

    Regards,

    Gao


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comentarios No hay comentarios

Su respuesta

Las respuestas pueden ser marcadas como Respuestas aceptadas por el autor de la pregunta, lo que indica a los usuarios que la respuesta resolvió su problema.