im getting an "application-define or object-define" error when i try to copy some objects.
I used to make a .select and .copy of certain range and then do a .paste in the place I wanted to copy the range. Althouht this worked well, i would like to just pass the value and avoid the .copy .paste method.
So, i'm making a few changes on the code and i cannot eliminate the "application-define or object-define" error.
Sub PreencherFacturador()
Application.Calculation = xlManual
Dim ano, mes1, mes2, mes3, dia, provisorio, iniciomes, maxreativa, capacitiva As Double
Dim LastRow As Long
Dim CPE, nome1, nome2, strFile, DIRECT As String
Dim data As Date
Dim Rng As Range
Dim ptTable As PivotTable
Dim pi As PivotItem
Dim ecer As Object
Dim sgl As Object
' Preencher facturador
CPE = Sheets("Dados").Cells(15, 3).Value
numproposta = Sheets("Dados").Cells(4, 3).Value
cliente = Sheets("Dados").Cells(10, 3).Value
ano = Year(Sheets("Dados").Cells(4, 5).Value)
nome1 = ActiveWorkbook.Name
If CPE = "" Then
MsgBox "CPE não encontrado."
Exit Sub
End If
Set ecer = ActiveWorkbook.Sheets("Cálculos")
Application.StatusBar = "Preenchendo facturador. Por favor aguarde."
Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("Cálculos").Range("G3:L35046").ClearContents
'Consumos mes Janeiro a Agosto
For mes1 = 1 To 8
ChDrive "F"
ChDir "F:\Data3\SCF\SCFfiles\Backup"
strFile = "*" & CPE & "_" & ano & "0" & mes1 + 1 & "*.sgl"
If Len(Dir(strFile)) Then
Workbooks.Open Filename:=Dir(strFile)
'Set the workbook and the sheet i want
Set sgl = ActiveWorkbook.ActiveSheet
nome2 = ActiveWorkbook.Name
If Range("A2").Value = "" Then
Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'HERE IT WORKS FINE
sgl.Range(Cells(4, 4), Cells(LastRow - 1, 9)).Select
dia = Right(Range("B4").Value, 2)
Windows(nome1).Activate
data = dia & "-" & "0" & mes1 & "-" & ano
With Sheets("Cálculos").Range("D:D")
Set Rng = .Find(What:=data, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.GoTo Rng, True
iniciomes = Rng.Row
End If
End With
'HERE IT DOESNT
sgl.Range(Cells(4, 4), Cells(LastRow - 1, 9)).Select
Call CopyValues(sgl.Range(Cells(4, 4), Cells(LastRow - 1, 9)), ecer.Sheets ("Cálculos").Cells(iniciomes, 7))
The CopyValues method is this:
Sub CopyValues(rngSource As Range, rngTarget As Range)
rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count).Value = rngSource.Value
End Sub
I don't get where the error is because in one part of the code the object selection do just fine and in the othe part it doesnt. (i've marked where the code works and where it doesnt with a comment)
Thanks in advance,
André