我知道有很多关于使用.select
和对象 _Worksheet 的错误 1004 的问题和答案,我尽我所能遵循了每一个问题和答案,特别是从下面的两个链接,但我似乎不知道我的代码出了什么问题。
我不断收到“对象‘_Worksheet’的方法‘range’失败”错误。在我看来,我的代码中没有必要使用 .Select 或 .Activate,但到目前为止,只有使用它才能正常工作。
顺便提一下,使用时.Select
,Application.ScreenUpdating = False
似乎不起作用,代码运行时我的屏幕转到每张表。有什么原因吗?
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