I'm attempting to combine duplicated rows in a table while summing the numbers in the last column, then creating a new summarized table below.
Only the first duplicated row is being summed. This value then appears in all of the rows below.
Example Table - five Columns
Sub CombineDupesV3()
Dim x As Long
Dim r As Long
Dim arr() As Variant
Dim dic As Object
Const DELIM As String = "|"
Set dic = CreateObject("Scripting.Dictionary")
x = Cells(Rows.Count, 1).End(xlUp).Row
arr = Cells(1, 1).Resize(x, 5).Value
For x = LBound(arr, 1) + 1 To UBound(arr, 1)
If dic.exists(arr(x, 1)) Then
arr(x, 5) = arr(x, 5) + CDbl(Split(dic(arr(x, 1)), DELIM)(3))
Else
dic(arr(x, 1)) = arr(x, 2) & DELIM & arr(x, 3) & DELIM & arr(x, 4) & DELIM & arr(x, 5)
End If
dic(arr(x, 1)) = arr(x, 2) & DELIM & arr(x, 3) & DELIM & arr(x, 4) & DELIM & arr(x, 5)
Debug.Print "X = " & x
Next x
r = UBound(arr, 1) + 2
Application.ScreenUpdating = False
Cells(r, 1).Resize(, 5).Value = Cells(1, 1).Resize(, 5).Value
r = r + 1
For x = 0 To dic.Count - 1
Cells(r + x, 1).Value = dic.keys()(x)
Cells(r + x, 2).Resize(, 4).Value = Split(dic.items()(x), DELIM)
Cells(r + x, 5).Value = CDbl(Cells(r, 5).Value)
Debug.Print "R = " & r
Next x
Application.ScreenUpdating = True
Erase arr
Set dic = Nothing
End Sub
The conversion line in the last loop should address the correct row value r + x
For x = 0 To dic.Count - 1
Cells(r + x, 1).Value = dic.keys()(x)
Cells(r + x, 2).Resize(, 4).Value = Split(dic.items()(x), DELIM)
'>> convert string to double <<
Cells(r + x, 5).Value = CDbl(Cells(r + x, 5).Value)
Next x
Further hints:
Try to fully qualify all range references in order to avoid unwanted results as unqualified cell addresses refer to the active sheet by default which needn't be the one you have in mind :-)
You should either redefine the data range definition or the target range as they might conflict if you run code twice.
In Excel, how I can merge values of Column B based on common values on Column A?
Basically what I need is some thing like this
You can use this UDF:
Function TEXTJOINIFS(rng As Range, delim As String, ParamArray arr() As Variant)
Dim rngarr As Variant
rngarr = Intersect(rng, rng.Parent.UsedRange).Value
Dim condArr() As Boolean
ReDim condArr(1 To Intersect(rng, rng.Parent.UsedRange).Rows.Count) As Boolean
Dim i As Long
For i = LBound(arr) To UBound(arr) Step 2
Dim colArr() As Variant
colArr = Intersect(arr(i), arr(i).Parent.UsedRange).Value
Dim j As Long
For j = LBound(colArr, 1) To UBound(colArr, 1)
If Not condArr(j) Then
Dim charind As Long
charind = Application.Max(InStr(arr(i + 1), ">"), InStr(arr(i + 1), "<"), InStr(arr(i + 1), "="))
Dim opprnd As String
If charind = 0 Then
opprnd = "="
Else
opprnd = Left(arr(i + 1), charind)
End If
Dim t As String
t = """" & colArr(j, 1) & """" & opprnd & """" & Mid(arr(i + 1), charind + 1) & """"
If Not Application.Evaluate(t) Then condArr(j) = True
End If
Next j
Next i
For i = LBound(rngarr, 1) To UBound(rngarr, 1)
If Not condArr(i) Then
TEXTJOINIFS = TEXTJOINIFS & rngarr(i, 1) & delim
End If
Next i
TEXTJOINIFS = Left(TEXTJOINIFS, Len(TEXTJOINIFS) - Len(delim))
End Function
You would call it like this:
=IF(MATCH(A1,A:A,0)=ROW(A1),TEXTJOINIFS(B:B,", ",A:A,A1),"")
Now it does not matter if the data is sorted or not it will only put the output in column C where the value in Column A first appears.
Use an array formula:
=TEXTJOIN(", ",TRUE,IF(A$1:A$15=A1,B$1:B$15,""))
(Use CTRL-SHIFT-ENTER instead of ENTER to enter the formula)
Here is my untested code of course.
The code below uses 2 loops to add the information.
dim X as integer
dim X2 as integer
dim match as string
X = 1
do while sheets("sheet1").range("A" & X).value <> ""
sheets("sheet1").range("C" & X).value = sheets("sheet1").range("B" & X).value
match = sheets("sheet1").range("A" & X).value
X2 = X + 1
do while sheets("sheet1").range("A" & X2).value = match
sheets("sheet1").range("C" & X).value = sheets("sheet1").range("C" & X).value + ", " + sheets("sheet1").range("B" & X2).value
X2 = X2 + 1
loop
X = X2
X = X + 1
Loop
I need help for Excel convert between 2 values, for example:
I have value number "27-30" I want to convert to "27,28,29,30"
And value character "S-XL" I want to convert to "S, M, L, XL"
The numbers 28,29 and so on can be looped easily but for sizes like S, M and L, you need a lookup table.
Column A contains your sizes and Column E, the lookup for non-numeric sizes,
The code is here, the result on Column B,
Sub sizes()
Dim i As Long, j As Long, str As String, rownum As Long
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If IsNumeric(Left(Cells(i, 1), InStr(Cells(i, 1), "-") - 1)) Then
str = Left(Cells(i, 1), InStr(Cells(i, 1), "-") - 1)
For j = Left(Cells(i, 1), InStr(Cells(i, 1), "-") - 1) + 1 To _
Mid(Cells(i, 1), InStr(Cells(i, 1), "-") + 1, 999)
str = str & " , " & j
Next j
Cells(i, 2) = str
Else
rownum = Application.WorksheetFunction.Match(Left(Cells(i, 1), InStr(Cells(i, 1), "-") - 1), Range("E:E"), 0)
str = Cells(rownum, 5)
rownum = rownum + 1
Do Until (Cells(rownum, 5) = Mid(Cells(i, 1), InStr(Cells(i, 1), "-") + 1, 999))
str = str & " , " & Cells(rownum, 5)
rownum = rownum + 1
Loop
str = str & " , " & Cells(rownum, 5)
Cells(i, 2) = str
End If
Next i
End Sub
Maybe I'm being too fussy, but my macro takes about 1 second to run in a powerfull laptop (with little data). But it will run on average-slow performance pc's.
Is there a way to optimize this code? Do you think Select Case is slowing down the execution? If so, how can I improve it?
Sorry for the extension of the code.
Thank you.
Private Sub crear_Click()
Dim ctrl As Control, ctrl2 As Control, aler As Variant, ws As Worksheet, ws2 As Worksheet, ultimafila As Double, ultimaFila2 As Double, i As Integer, pPage As MSForms.Page, N As Double, selectedItems As String, valorProbabilidad As Integer, valorImpacto As Integer, valorMagnitud As Integer, resta As Long, ultimaFila3 As Long, j As Long, ultimaFila4 As Long, k As Double, l As Double
Set ws = Worksheets("Valoración"): Set ws2 = Worksheets("lista_riesgos")
ultimafila = ws.ListObjects("Riesgos").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
ultimaFila2 = ws2.ListObjects("consolidadoRiesgos").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
ultimaFila3 = ws2.ListObjects("consolidadoRiesgos").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
ultimaFila4 = ws2.ListObjects("Riesgo").Range.Columns(2).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
resta = 0.5
With Me
For Each ctrl In .Controls
If Left(ctrl.Name, 5) = "texto" Then
If Trim(ctrl.Value & vbNullString) = vbNullString Then
aler = Replace(ctrl.Name, "texto", "alerta")
.Controls(aler).Visible = True
End If
ElseIf Left(ctrl.Name, 5) = "lista" Then
For N = 0 To listaObjetivos.ListCount - 1
If listaObjetivos.Selected(N) Then GoTo algoSeleccionado
Next N
aler = Replace(ctrl.Name, "lista", "alerta")
.Controls(aler).Visible = True
GoTo salir
algoSeleccionado:
aler = Replace(ctrl.Name, "lista", "alerta")
.Controls(aler).Visible = False
GoTo continuar
salir:
End If
Next ctrl
Exit Sub
End With
continuar:
Select Case Me.textoFrecuencia
Case "Casi seguro"
valorProbabilidad = 5
Case "Probable"
valorProbabilidad = 4
Case "Posible"
valorProbabilidad = 3
Case "Improbable"
valorProbabilidad = 2
Case "Raro"
valorProbabilidad = 1
End Select
Select Case Me.textoImpacto
Case "Catastrófico"
valorImpacto = 5
Case "Mayor"
valorImpacto = 4
Case "Moderado"
valorImpacto = 3
Case "Menor"
valorImpacto = 2
Case "Insignificante"
valorImpacto = 1
End Select
valorMagnitud = valorProbabilidad * valorImpacto
With ws
.Unprotect Password:="pAtRiCiA"
For Each ctrl In Me.Controls
If Left(ctrl.Name, 5) = "texto" Then
.Cells(ultimafila, ctrl.TabIndex) = ctrl.Value
End If
Next ctrl
For i = 0 To listaObjetivos.ListCount - 1
If listaObjetivos.Selected(i) = True Then
ws.Cells(ultimafila, (i) + 6) = "X"
'selectedItems = selectedItems & listaObjetivos.List(i) & (i) & vbNewLine
End If
Next i
Select Case valorMagnitud
Case Is >= 15
.Cells(ultimafila, 25) = "Extremo"
Case 8 To 14
.Cells(ultimafila, 25) = "Alto"
Case 4 To 7
.Cells(ultimafila, 25) = "Medio"
Case 1 To 3
.Cells(ultimafila, 25) = "Aceptable"
End Select
.Rows(ultimafila).AutoFit
.Rows(ultimafila).RowHeight = .Cells(ultimafila, 1).Height + 12
.Protect Password:="pAtRiCiA", DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
With ws2
.Unprotect Password:="pAtRiCiA"
.Cells(ultimaFila2, 1) = (valorProbabilidad * valorProbabilidad * valorProbabilidad) + valorImpacto
.Cells(ultimaFila2, 2) = Me.textoCodigo
.ListObjects("Riesgo").DataBodyRange.Columns(1).ClearContents
For k = 1 To ultimaFila3
Select Case .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 1).Value
Case 2
If .ListObjects("Riesgo").DataBodyRange.Cells(1, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(1, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(1, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(1, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 3
If .ListObjects("Riesgo").DataBodyRange.Cells(2, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(2, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(2, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(2, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 4
If .ListObjects("Riesgo").DataBodyRange.Cells(3, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(3, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(3, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(3, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 5
If .ListObjects("Riesgo").DataBodyRange.Cells(4, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(4, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(4, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(4, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 6
If .ListObjects("Riesgo").DataBodyRange.Cells(5, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(5, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(5, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(5, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 9
If .ListObjects("Riesgo").DataBodyRange.Cells(6, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(6, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(6, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(6, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 10
If .ListObjects("Riesgo").DataBodyRange.Cells(7, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(7, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(7, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(7, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 11
If .ListObjects("Riesgo").DataBodyRange.Cells(8, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(8, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(8, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(8, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 12
If .ListObjects("Riesgo").DataBodyRange.Cells(9, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(9, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(9, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(9, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 13
If .ListObjects("Riesgo").DataBodyRange.Cells(10, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(10, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(10, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(10, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 28
If .ListObjects("Riesgo").DataBodyRange.Cells(11, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(11, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(11, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(11, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 29
If .ListObjects("Riesgo").DataBodyRange.Cells(12, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(12, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(12, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(12, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 30
If .ListObjects("Riesgo").DataBodyRange.Cells(13, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(13, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(13, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(13, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 31
If .ListObjects("Riesgo").DataBodyRange.Cells(14, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(14, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(14, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(14, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 32
If .ListObjects("Riesgo").DataBodyRange.Cells(15, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(15, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(15, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(15, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 65
If .ListObjects("Riesgo").DataBodyRange.Cells(16, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(16, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(16, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(16, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 66
If .ListObjects("Riesgo").DataBodyRange.Cells(17, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(17, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(17, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(17, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 67
If .ListObjects("Riesgo").DataBodyRange.Cells(18, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(18, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(18, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(18, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 68
If .ListObjects("Riesgo").DataBodyRange.Cells(19, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(19, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(19, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(19, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 69
If .ListObjects("Riesgo").DataBodyRange.Cells(20, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(20, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(20, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(20, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 126
If .ListObjects("Riesgo").DataBodyRange.Cells(21, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(21, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(21, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(21, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 127
If .ListObjects("Riesgo").DataBodyRange.Cells(22, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(22, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(22, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(22, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 128
If .ListObjects("Riesgo").DataBodyRange.Cells(23, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(23, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(23, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(23, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 129
If .ListObjects("Riesgo").DataBodyRange.Cells(24, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(24, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(24, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(24, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
Case 130
If .ListObjects("Riesgo").DataBodyRange.Cells(25, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(25, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(25, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(25, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(k + 1, 2)
End If
End Select
Next k
.Protect Password:="pAtRiCiA", DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
For j = 0 To listaObjetivos.ListCount - 1
listaObjetivos.Selected(j) = False
Next
Me.textoCodigo = Null
Me.textoTipo = Null
Me.textoResponsable = Null
Me.textoDescripcion = Null
Me.textoDetalle = Null
Me.textoControles = Null
Me.textoFrecuencia = Null
Me.textoEscala = Null
Me.textoImpacto = Null
End Sub
Your many Select Case statements would indeed eat up a lot of time. At a quick glance, there is a firm relationship between the Case and the result. The following example shows how you could compress all your Select statements in the K-loop into a single statement.
Dim R As Long
R = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 1).Value
If .ListObjects("Riesgo").DataBodyRange.Cells(1, 1) = Empty Then
.ListObjects("Riesgo").DataBodyRange.Cells(R - 1, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 2)
Else
.ListObjects("Riesgo").DataBodyRange.Cells(R - 1, 1) = .ListObjects("Riesgo").DataBodyRange.Cells(R - 1, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 2)
End If
Unfortunately, the relationship isn't always -1. Therefore I suggest that you declare an array before you enter the K-loop, like this:-
Dim Clm() As Variant
Clm = Array(2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 28)
The numbers in the array are exactly your 'Case' conditions. You should extend this list up to 130 which is your last 'Case'. With the help of this tool you can now replace all the Case statements with just one:-
Dim Clm() As Variant ' Place your Dim statements
Dim C As Long, R As Long ' at the top of your code
Clm = Array(2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 28)
' start the K-loop here
C = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 1).Value
R = Application.Match(C, Clm, 0)
With .ListObjects("Riesgo").DataBodyRange
If .Cells(1, 1) = Empty Then
.Cells(R, 1) = .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 2)
Else
.Cells(R, 1) = .Cells(R, 1) & " " & .ListObjects("consolidadoRiesgos").DataBodyRange.Cells(K + 1, 2)
End If
End With
An error will occur if a match isn't found. Match will return the number of the element in the array which, it so happens, is the row number you need. You could modify this if required. The point is that the Match function returns a consecutive number from a range of random numbers.