Sei que há muitas perguntas e respostas sobre o uso .select
do erro 1004 para o objeto _Worksheet e segui cada uma delas da melhor maneira possível, especialmente nos dois links abaixo, mas não consigo descobrir o que há de errado com meu código.
Excel VBA, obtendo intervalo de uma planilha inativa
Como evitar o uso de Select no Excel VBA?
Continuo recebendo o erro "Método 'range' do objeto '_Worksheet' falhou". Da forma como vejo, não há necessidade de usar .Select ou .Activate no meu código, mas, por enquanto, só está funcionando usando-os.
Em uma nota lateral, ao usar .Select
, o Application.ScreenUpdating = False
não parece funcionar e minha tela vai para cada planilha enquanto o código é executado. Alguma razão para isso?
Sub FormatWB()
Dim X As Long
Dim AA As Long
Dim Escopo As Worksheet
Dim Material As Worksheet
Dim PrecoMTL As Worksheet
Dim PrecoRev As Worksheet
Dim Orcamento As Worksheet
Set Escopo = ActiveWorkbook.Worksheets("Escopo")
Set Material = ActiveWorkbook.Worksheets("Material")
Set PrecoMTL = ActiveWorkbook.Worksheets("Preço Material")
Set PrecoRev = ActiveWorkbook.Worksheets("Preço Revestimento")
Set Orcamento = ActiveWorkbook.Worksheets("Orçamento Final")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
X = 4 'Initial Row
Do Until IsEmpty(Escopo.Cells(X, "B"))
'Escopo.Select
With Escopo.Range(Cells(X, "A"), Cells(X, "Y"))
.UnMerge
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.NumberFormat = "@" 'Convert cell to text format
.Font.Bold = False
.Font.Italic = False
.Font.Underline = False
.Font.Name = "Calibri"
.Font.Size = 11
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
'Material.Select
With Material.Range(Cells(X, "A"), Cells(X, "N"))
.UnMerge
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.NumberFormat = "@" 'Convert cell to text format
.Font.Bold = False
.Font.Italic = False
.Font.Underline = False
.Font.Name = "Calibri"
.Font.Size = 11
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
'PrecoMTL.Select
With PrecoMTL.Range(Cells(X, "A"), Cells(X, "P"))
.UnMerge
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.NumberFormat = "@" 'Convert cell to text format
.Font.Bold = False
.Font.Italic = False
.Font.Underline = False
.Font.Name = "Calibri"
.Font.Size = 11
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
'PrecoRev.Select
With PrecoRev.Range(Cells(X, "A"), Cells(X, "O"))
.UnMerge
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.NumberFormat = "@" 'Convert cell to text format
.Font.Bold = False
.Font.Italic = False
.Font.Underline = False
.Font.Name = "Calibri"
.Font.Size = 11
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
'Orcamento.Select
With Orcamento.Range(Cells(X, "A"), Cells(X, "Q"))
.UnMerge
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.NumberFormat = "@" 'Convert cell to text format
.Font.Bold = False
.Font.Italic = False
.Font.Underline = False
.Font.Name = "Calibri"
.Font.Size = 11
.Interior.ColorIndex = 0
.Font.Color = vbBlack
End With
X = X + 1
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub