jueves, 25 de agosto de 2022

Calcula l'àrea 2

 Pren el radi =2 cm


Ara resol:

https://www.mongge.com/ejercicios/761





Matriu f x c -> una sola columna a partir de files amb cel·les no buides

 Sub Macro1()

Dim CantFila As Long
Dim CantCol As Long
Dim c As Single, f As Single, m As Single, n As Single
Dim a As Variant

m = 26 'a partir de la fila 26
n = 1

CantFila = Cells(Rows.Count, 2).End(xlUp).Row
CantCol = Cells(2, Columns.Count).End(xlToLeft).Column

For f = 1 To CantFila Step 1
    For c = 1 To CantCol Step 1

    If (Cells(f, c).Value <> "") Then
        a = Cells(f, c).Value
        Cells(m, 1).Value = a
        m = m + 1
    
    Else
        f = f + 1
        c = 1
        a = Cells(f, c).Value
        Cells(m, 1).Value = a
        m = m + 1
      
    End If

    Next c
Next f

End Sub


Array resize - arranjament matriu comparant tres primers caràcters





 Sub Macro1()

Dim CantFila As Long
Dim CantCol As Long
Dim c As Single, f As Single, m As Single, n As Single
Dim Ultim As String
Dim a As Variant


m = 26
n = 1
Ultim = Left(Cells(1, 1).Value, 3)
Debug.Print (Ultim)


CantFila = Cells(Rows.Count, 2).End(xlUp).Row
CantCol = Cells(2, Columns.Count).End(xlToLeft).Column


For f = 1 To CantFila Step 1
    For c = 1 To CantCol Step 1

    If (Left(Cells(f, c).Value, 3) = Ultim) Then
        Debug.Print (Left(Cells(f, c).Value, 3))
        a = Cells(f, c).Value
        Cells(m, n).Value = a
        n = n + 1
        Ultim = Left(Cells(f, c).Value, 3)
    
    ElseIf ((Left(Cells(f, c).Value, 3) <> Ultim) And Left(Cells(f, c).Value, 3) = "") Then
        f = f + 1
        c = 1
        
        If (Left(Cells(f, c).Value, 3) = Ultim) Then
        a = Cells(f, c).Value
        Cells(m, n).Value = a
        n = n + 1
        Ultim = Left(Cells(f, c).Value, 3)
        End If
    
    Else
        
        m = m + 1
        n = 1
        a = Cells(f, c).Value
        Cells(m, n).Value = a
        Ultim = Left(Cells(f, c).Value, 3)
    
    End If

    Next c
Next f

End Sub