Excel

Eliminar o borrar aleatoriamente filas de datos dentro de una o más columnas en Excel

Hoy me pasó algo extraño. Necesitaba eliminar algunos datos en varias columnas de un libro de Excel y, en lugar de realizar esta tarea manualmente, decidí que una pequeña función de VBA resolvería el problema y sería mucho más rápido de lo que podría hacerlo manualmente.

Básicamente, quería eliminar valores de forma aleatoria dentro de una sola columna o, posiblemente, eliminar filas de datos de forma aleatoria dentro de una serie de columnas. Para mis necesidades, se me ocurrió el siguiente procedimiento:

'---------------------------------------------------------------------------------------
' Procedure : Excel_ClearCellRandomly
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : 
' Purpose   : Blnak/Delete/Clear random rows within the specified Column, or range of Columns
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - 
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sStartColumnLtr   : First Column to blank
' sEndColumnLtr     : Last Column to blank
' lStartOnRowNo     : Which row is the 1st row containing data to clear
'
' Usage:
' ~~~~~~
' Randomly clears rows in Column B, starting at row 12 to the last used row
' Excel_ClearCellRandomly("B", 12)
'
' Randomly clears rows in Column B through F, starting at row 1 to the last used row
' Excel_ClearCellRandomly("B", "F")
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2024-09-17              Initial Release
'---------------------------------------------------------------------------------------
Sub Excel_ClearCellRandomly(sStartColumnLtr As String, _
                            Optional sEndColumnLtr As String, _
                            Optional lStartOnRowNo As Long = 1)
    On Error GoTo Error_Handler
    Dim i                     As Long
    Dim lColStart             As Long
    Dim lColEnd               As Long
    Dim lNumberRows           As Long

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    lColStart = ActiveSheet.Columns(sStartColumnLtr).Column
    If sEndColumnLtr  "" Then lColEnd = ActiveSheet.Columns(sEndColumnLtr).Column
    lNumberRows = ActiveSheet.Cells(ActiveSheet.Rows.Count, lColStart).End(xlUp).Row

    For i = lStartOnRowNo To lNumberRows
        If (Rnd()  0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

Publicaciones relacionadas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Botón volver arriba