I made the code below following good programming practices, using more appropriate variable names with the use of them, besides not using ActiveCell
, Offset
and the like. I hope you prefer it.
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 it exists), 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 keep it apart, without mixing with the rest of the code. I also put the return 0 in case the text can not be found. If you find a category with a value of 0, you have to see which category (s) you did not plan to add to the code.
If there is any question about the code or some snippet that needs to be better explained, just ask me what I answer.