2
My suggestion is this::
Create a code to fetch cell numbers, as follows::
Function Extrair_Numero(ByRef TEXTO As String, _ Optional ByRef SEQUENCIAL As Integer = 1) As Double Dim i As Integer Dim COUNT As Integer Dim TEMP As String Dim RESULTADO As Double For i = 1 To Len(TEXTO) TEMP = Mid(TEXTO, i, 1) If IsNumeric(TEMP) Then RESULTADO = RESULTADO & TEMP ElseIf RESULTADO > 0 Then COUNT = COUNT + 1 If RESULTADO > 0 And SEQUENCIAL = COUNT Then Extrair_Numero = CDbl(RESULTADO) Exit Function ElseIf COUNT > 0 And SEQUENCIAL > COUNT Then RESULTADO = Empty End If End If Next Extrair_Numero = CDbl(RESULTADO) End Function
This is a function that returns the number from a string, can return the number at a given position, or sequence.
Example: in string "12 13 15 18"
12 would be the first (1), 13 the second (2), 15 the third (3) and so on.
Creates a loop to copy your data to the desired destination.
Sub Copia_Dados() Dim PLANILHA_ORIGEM As String Dim PLANILHA_DESTINO As String Dim COLUNA_CODIGO As String Dim COLUNA_DADOS As String Dim CELULA_DESTINO_CODIGO As String Dim CELULA_DESTINO_DADOS As String Dim rCODIGO As Range Dim rDADOS As Range Dim rCell As Range Dim NUM_INI As Double Dim NUM_FIM As Double Dim i As Integer Application.ScreenUpdating = False ' Define as planilhas PLANILHA_ORIGEM = "Plan1" PLANILHA_DESTINO = "Plan2" ' Define as colunas de código e dados (ORIGEM) COLUNA_CODIGO = "A" COLUNA_DADOS = "B" ' Define as células iniciias para código e dados (DESTINO) CELULA_DESTINO_CODIGO = "A1" CELULA_DESTINO_DADOS = "B1" ' Busca os dados das colunas com códigos e dados Set rCODIGO = Sheets(PLANILHA_ORIGEM).UsedRange.Columns(COLUNA_CODIGO) Set rDADOS = Sheets(PLANILHA_ORIGEM).UsedRange.Columns(COLUNA_DADOS) ' Loop na coluna de código For Each rCell In rCODIGO.Cells ' Buscas os números da célula NUM_INI = Extrair_Numero(rCell.Text, 1) NUM_FIM = Extrair_Numero(rCell.Text, 2) If NUM_INI < NUM_FIM Then For i = NUM_INI To NUM_FIM ' Preenche os dados Sheets(PLANILHA_DESTINO).Range(CELULA_DESTINO_CODIGO).Offset(i - 1, 0).Value = i Sheets(PLANILHA_DESTINO).Range(CELULA_DESTINO_DADOS).Offset(i - 1, 0).Value = Sheets(PLANILHA_ORIGEM).Range(COLUNA_DADOS & rCell.Row).Value Next Else MsgBox "Os números '" & rCell.Text & "' informado em '" & rCell.Address & "' não estão em sequência!", vbInformation, "Erro" Exit Sub End If Next End Sub
Note the variable settings so that data is transferred from the desired source location to the destination location.
I tried to keep it as abstract as possible.
In the current code, if its sequence is not continuous, for example 1 to 10 and then 30 to 40, the target sheet will have a "gap" from 11 to 29, however if this way does not meet can adapt something not to skip the cells, or delete blank cells later.
Smart to have helped!
The initial spreadsheet will always have this same pattern?
– Evert
@Evert The idea is to have this same structure. What could be different at most is that instead of being "1 to 10", it could be "1-10" or something like that. But the whole column will follow the same logic.
– Leon Freire
Okay, we’re gonna need a VBA to do this... I’m gonna try to post something here to help you and see if you can get it into production there.
– Evert
@Evert Thank you so much!
– Leon Freire