I know there is a lot of questions and answers regarding using .select
and Error 1004 for object _Worksheet and followed every one of them to the best of my ability, specially from both links below, but I can't seen to know whats wrong with my code.
Excel VBA, getting range from an inactive sheet
How to avoid using Select in Excel VBA?
I keep getting "Method 'range' of object '_Worksheet' failed" error. The way I see it, there is no need to use .Select or .Activate in my code, but as of now it is only working by using it.
On a side note, when using .Select
, the Application.ScreenUpdating = False
doesn't seen to work and my screen goes to every sheet as the code runs. Any reason why?
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
This statement (and similar)
Escopo.Range(Cells(X, "A"), Cells(X, "Y"))
is wrong because Cells is applied to the ActiveSheet.
It should be
Range(Escopo.Cells(X, "A"), Escopo.Cells(X, "Y"))
or
Escopo.Range("A" & X & ":" & "Y" & X)
or
Escopo.Cells(X, "A").Resize(1, 24)