I did the code below following the good programming practices, using more appropriate variable names with their use, and not using ActiveCell
, Offset
and other things like that. I hope you prefer it this way.
Option Explicit
Option Private Module
Sub Principal()
Dim PlanilhaAtual As Worksheet
Dim PlanilhaNova As Worksheet
Set PlanilhaAtual = Worksheets(1)
GerarPlanilha ("Nova")
Set PlanilhaNova = Worksheets("Nova")
Dim UltimaLinha As Long
UltimaLinha = PlanilhaAtual.Cells.Find("*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
' Copiando os valores para a planilha nova
Dim Linha As Long
For Linha = 2 To UltimaLinha
PlanilhaNova.Cells(Linha, 1).Value = PlanilhaAtual.Cells(Linha, 2).Value
PlanilhaNova.Cells(Linha, 2).Value = _
ConverterCategoria(CStr(PlanilhaAtual.Cells(Linha, 1).Value))
PlanilhaNova.Cells(Linha, 3).Value = "yes"
PlanilhaNova.Cells(Linha, 4).Value = PlanilhaAtual.Cells(Linha, 4).Value
PlanilhaNova.Cells(Linha, 5).Value = PlanilhaAtual.Cells(Linha, 2).Value
PlanilhaNova.Cells(Linha, 6).Value = ""
PlanilhaNova.Cells(Linha, 7).Value = PlanilhaAtual.Cells(Linha, 6).Value
PlanilhaNova.Cells(Linha, 8).Value = PlanilhaAtual.Cells(Linha, 7).Value
Next
End Sub
Sub GerarPlanilha(Nome As String)
Dim Planilha As Worksheet
' Excluindo planilha existente se houver
For Each Planilha In Worksheets
If Planilha.Name = Nome Then
Application.DisplayAlerts = False
Planilha.Delete
Application.DisplayAlerts = True
End If
Next
' Criando a planilha nova
Set Planilha = Worksheets.Add(After:=Sheets(Sheets.Count))
Planilha.Name = Nome
' Gerando cabeçalho na planilha nova
Range("A1").Value = "name(pt-br)"
Range("B1").Value = "categories"
Range("C1").Value = "shipping"
Range("D1").Value = "quantity"
Range("E1").Value = "model"
Range("F1").Value = "sku"
Range("G1").Value = "price"
Range("H1").Value = "date_added"
End Sub
Function ConverterCategoria(Texto As String) As Long
' O valor 0 é retornado caso o texto não tenha um valor correspondente
Select Case Texto
Case "Multimídia > Multilaser"
ConverterCategoria = 1
Case "Sestini > Meninos"
ConverterCategoria = 2
Case Else
ConverterCategoria = 0
End Select
End Function
The code contains a main routine (the one you should run), a subroutine to create the new worksheet (and delete, if any) and a function to generate the category value.
The use of the function is better in this case, since new values can be added, so I kept to the part, without mixing with the rest of the code. I also put the 0 return in case the text is not found. If you find any category with value 0, you need to see which (or which) were the categories that are not foreseen(s) and add in the code.
If there are any questions about the code or some excerpt that needs to be better explained, just ask that I answer.
@Cantoni, I’ve been searching and found your answer in this topic. Could help me here too?
– lucasbento
You want to generate a new worksheet in the same document or generate a new workbook (new file)?
– PedroMVM
@Pedromvm independent, the two will meet me.
– lucasbento
"model will have the same Title value", but at line 3 of its expected result is the category name. I am taking the statement, even seeing that columns A and E will contain the same value.
– PedroMVM