Obsidean_VM/01-Documentation/Scripts Excel - VBA/Excel - VBA - Cell Referenc...

3.4 KiB

Cell Reference Increment Functions

IncrementarCelda Function

Description

Increments a specialized cell reference in the format "LXXX.Y" where:

  • L: A letter
  • XXX: A numeric value
  • Y: A single digit (0-6)

The function increments the Y value and handles carrying over to XXX when Y reaches 7.

Parameters

  • CeldaSuperior: A string in the format "LXXX.Y" (e.g., "A123.4")

Return Value

Returns a string with the incremented reference in the same format.

Logic Flow

  1. Separates the reference into components:
    • First letter
    • Numbers before the decimal point
    • Number after the decimal point
  2. Increments the decimal part (Y)
  3. If Y reaches 7:
    • Resets Y to 0
    • Increments the XXX part
  4. Reconstructs the reference in the original format

Public Function IncrementarCelda(CeldaSuperior As String) As String
    ' Encuentra la posición del punto
    Dim dotPosition As Integer
    dotPosition = InStr(1, CeldaSuperior, ".")

    ' Extrae la primera letra, la parte antes del punto (XXX) y la parte después del punto (Y)
    Dim primeraLetra As String
    Dim parteIzquierda As String
    Dim parteDerecha As String
    primeraLetra = Left(CeldaSuperior, 1)  ' Extrae la primera letra
    parteIzquierda = Mid(CeldaSuperior, 2, dotPosition - 2)
    parteDerecha = Mid(CeldaSuperior, dotPosition + 1, Len(CeldaSuperior) - dotPosition)

    ' Convierte las partes a números
    Dim numeroIzquierda As Integer
    Dim numeroDerecha As Integer
    numeroIzquierda = CInt(parteIzquierda)
    numeroDerecha = CInt(parteDerecha)

    ' Incrementa la parte derecha y reinicia si es necesario
    If numeroDerecha < 7 Then
        numeroDerecha = numeroDerecha + 1
    Else
        numeroDerecha = 0
        numeroIzquierda = numeroIzquierda + 1
    End If

    ' Construye el resultado en el formato "LXXX.Y" donde L es la primera letra original
    IncrementarCelda = primeraLetra & numeroIzquierda & "." & numeroDerecha
End Function

IncrementarCeldaValor Function

Description

Increments a cell reference by a specified value. The function works with references that have letters followed by numbers.

Parameters

  • CeldaSuperior: A string containing letters followed by numbers
  • Incremento: An integer value to add to the numeric portion

Return Value

Returns a string with the original letters and the incremented numeric value.

Logic Flow

  1. Separates the reference into:
    • Letter portion
    • Numeric portion
  2. Converts the numeric portion to a long integer
  3. Adds the increment value
  4. Reconstructs the reference with original letters and new number

Public Function IncrementarCeldaValor(CeldaSuperior As String, Incremento As Integer) As String
    ' Extrae la parte de letras y la parte de números
    Dim letras As String
    Dim numerosComoTexto As String
    Dim i As Integer
    
    ' Encuentra el punto donde comienzan los números después de las letras
    i = 1
    While i <= Len(CeldaSuperior) And Not IsNumeric(Mid(CeldaSuperior, i, 1))
        i = i + 1
    Wend
    
    letras = Left(CeldaSuperior, i - 1)
    numerosComoTexto = Mid(CeldaSuperior, i, Len(CeldaSuperior) - i + 1)
    
    ' Convierte la parte numérica a un número largo y le añade el incremento
    Dim numeroFinal As Long
    numeroFinal = CLng(Val(numerosComoTexto))
    numeroFinal = numeroFinal + Incremento
    
    ' Construye el resultado final
    IncrementarCeldaValor = letras & numeroFinal
End Function