Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Título: Creación de dos funciones de suma y resta de matrices desarrollado en visual Basic en Excel

Nombre: María Camila García Millán

Código: 2225019

A continuación, el código para programar las dos funciones y ejecutarlas en el userform:

Function sumarM(matriz1 As Variant, matriz2 As Variant)

Dim f1 As Integer, c1 As Integer

Dim f2 As Integer, c2 As Integer

Dim f3 As Integer, c3 As Integer

Dim result() As Variant

ReDim result(1 To fs, 1 To cs)

For i = 1 To fs

For j = 1 To cs

result(i, j) = matriz1.Cells(i, j).Value + matriz2.Cells(i, j).Value

Next

Next

sumarM = result

End Function

Sub SumarMatrices(matriz1 As Range, matriz2 As Range, destino As Range)

Dim f As Integer

Dim c As Integer

Dim resultado() As Double

Dim i As Integer, j As Integer

ReDim resultado(1 To filas, 1 To columnas)


fs = UBound(matriz1, 1)

cs = UBound(matriz1, 2)

fs = UBound(matriz2, 1)

fs = UBound(matriz2, 2)

For i = 1 To fs

For j = 1 To cs

resultado(i, j) = matriz1.Cells(i, j).Value + matriz2.Cells(i, j).Value

Next

Next

EnviarMatriz resultado, destino.Row, destino.Column

End Sub

Sub EnviarMatriz(matriz As Variant, f As Integer, c As Integer)

Dim i As Integer, j As Integer

Dim fs As Integer, cs As Integer

fs = UBound(matriz, 1)

cs = UBound(matriz, 2)

For i = 1 To fs

For j = 1 To cs

Cells(f + i - 1, c + j - 1).Value = matriz(i, j)

Next j

Next i

End Sub

Sub final()
Dim M_1 As Range, M_2 As Range, M_3 As Range

Dim f1 As Integer, c1 As Integer, f2 As Integer, c2 As Integer, f3 As Integer, c3 As Integer

Dim fs As Integer, cs As Integer

Dim i As Integer, j As Integer

Dim sumarM As Double

Set M_1 = Range("A1:B2")

Set M_2 = Range("C1:D2")

Set M_3 = Range("E1:F2")

f1 = M_1.Row

c1 = M_1.Column

f2 = M_2.Row

c2 = M_2.Column

f3 = M_3.Row

c3 = M_3.Column

fs = M_1.Rows.Count

cs = M_1.Columns.Count

For i = 1 To fs

For j = 1 To cs

sumarM = M_1.Cells(i, j).Value + M_2.Cells(i, j).Value

M_3.Cells(i, j).Value = sumarM

Next

Next

End Sub
Private Sub B_1_Click()

final

End Sub

Function restarM(matriz1 As Variant, matriz2 As Variant)

Dim f1 As Integer, c1 As Integer

Dim f2 As Integer, c2 As Integer

Dim f3 As Integer, c3 As Integer

Dim result() As Variant

ReDim result(1 To fs, 1 To cs)

For i = 1 To fs

For j = 1 To cs

result(i, j) = matriz1.Cells(i, j).Value - matriz2.Cells(i, j).Value

Next

Next

sumarM = result

End Function

Sub RestarMatrices(matriz1 As Range, matriz2 As Range, destino As Range)

Dim f As Integer

Dim c As Integer

Dim resultado() As Double

Dim i As Integer, j As Integer

ReDim resultado(1 To filas, 1 To columnas)

fs = UBound(matriz1, 1)
cs = UBound(matriz1, 2)

fs = UBound(matriz2, 1)

fs = UBound(matriz2, 2)

For i = 1 To fs

For j = 1 To cs

resultado(i, j) = matriz1.Cells(i, j).Value - matriz2.Cells(i, j).Value

Next

Next

EnviarMatriz resultado, destino.Row, destino.Column

End Sub

Sub EnviarMatriz(matriz As Variant, f As Integer, c As Integer)

Dim i As Integer, j As Integer

Dim fs As Integer, cs As Integer

fs = UBound(matriz, 1)

cs = UBound(matriz, 2)

For i = 1 To fs

For j = 1 To cs

Cells(f + i - 1, c + j - 1).Value = matriz(i, j)

Next j

Next i

End Sub

Sub final()

Dim M_1 As Range, M_2 As Range, M_3 As Range

Dim f1 As Integer, c1 As Integer, f2 As Integer, c2 As Integer, f3 As Integer, c3 As Integer

Dim fs As Integer, cs As Integer


Dim i As Integer, j As Integer

Dim sumarM As Double

Set M_1 = Range("A1:B2")

Set M_2 = Range("C1:D2")

Set M_3 = Range("E1:F2")

f1 = M_1.Row

c1 = M_1.Column

f2 = M_2.Row

c2 = M_2.Column

f3 = M_3.Row

c3 = M_3.Column

fs = M_1.Rows.Count

cs = M_1.Columns.Count

For i = 1 To fs

For j = 1 To cs

sumarM = M_1.Cells(i, j).Value - M_2.Cells(i, j).Value

M_3.Cells(i, j).Value = sumarM

Next

Next

End Sub

Private Sub CommandButton2_Click()

final

End Sub

You might also like