Notifications
Clear all

rotina x function

5 Posts
3 Usuários
0 Reactions
1,209 Visualizações
(@taniaweb)
Posts: 0
New Member
Topic starter
 

Olá a todos
Estou com um probleminha...
Quando coloco uma macro para rodar, uma das funções é inserir a função somase nas células determinadas.
Durante o processamento desta rotina ele pula para uma function definida, que não tem vínculo nenhum com a rotina ativa... Why? Por que isso acontece?
Minha rotina leva muito mais tempo do que o necessário, o que eu posso fazer para impedir isso?

Valeu Pessoal
Obrigada desde já

 
Postado : 21/05/2015 8:46 am
(@wagner-morel-vidal-nobre)
Posts: 4063
Famed Member
 

taniaweb,

Boa tarde!

Anexe seu arquivo e explique melhor que rotina está executando, onde ocorre isso e para qual função está "pulando".

 
Postado : 21/05/2015 9:12 am
(@taniaweb)
Posts: 0
New Member
Topic starter
 

Tenho esta rotina:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub allinoneDos()
Cells(ActiveCell.Row, 1).Select
base = ActiveCell.Address

Range("A1").Select
Cells.Find(What:="Inventory Projected", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 3).Select
inventario = ActiveCell.Address
net = "$d$3"

Range(base).Select
ActiveCell.Offset(0, 3).Select
linha = 0
'=WeeksofSale($D$3:$CC$3;D32)*7
While Cells(ActiveCell.Row, 1).Value <> ""
inicio = ActiveCell.Address
For i = 0 To 77
ActiveCell.formula = "=WeeksofSale(" & Range(net).Offset(linha, i + 1).Address & ":" & Range(net).Offset(linha, 77).Address & _
"," & Range(inventario).Offset(linha, i).Address & ")*7"
ActiveCell.Offset(0, 1).Select
Next i
linha = linha + 1
Range(inicio).Select
ActiveCell.Offset(1, 0).Select
Wend
End Sub

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Private Function WeeksOfSale(Demand As Range, QOH As Double)
Dim endOfFC, qohnegflag As Boolean
Dim cont As Integer
i = 1
WeeksOfSale = 0
totdemand = 0
endOfFC = False
qohnegflag = False

If QOH < 0 Then 'Set negative QOH to positive to calc. negative DOS
qohnegflag = True
QOH = QOH * -1
End If

For i = 1 To Demand.Cells.Count
If Not (QOH - (totdemand + Demand.Cells(1, i).Value)) < 0 Then
totdemand = totdemand + Demand.Cells(1, i).Value
If i = Demand.Cells.Count Then
endOfFC = True
End If
Else
lastFcValue = Demand.Cells(1, i).Value
WeeksOfSale = (i - 1) * 1
i = Demand.Cells.Count
End If
Next i

If endOfFC = True Then
WeeksOfSale = 999
End If

If QOH > 0 And endOfFC = False Then
WeeksOfSale = WeeksOfSale + ((QOH - totdemand) / lastFcValue) * 1
End If
cont = cont + 1
If qohnegflag = True Then
WeeksOfSale = WeeksOfSale * -1
End If
If cont > 1 Then
Exit Function
End If

WeeksOfSale = Round(WeeksOfSale, 1)
End Function

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Quando ele roda a rotina entra nesta function e fica em um looping nela...
Para evitar, estou desabilitando o cálculo automático, mas é uma opção ruim para o resto da rotina...

Obrigada desde já

 
Postado : 21/05/2015 9:30 am
(@gtsalikis)
Posts: 2373
Noble Member
 

Experimente assim:

Sub allinoneDos()

With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With

Cells(ActiveCell.Row, 1).Select
base = ActiveCell.Address

Range("A1").Select
Cells.Find(What:="Inventory Projected", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 3).Select
inventario = ActiveCell.Address
net = "$d$3"

Range(base).Select
ActiveCell.Offset(0, 3).Select
linha = 0
'=WeeksofSale($D$3:$CC$3;D32)*7
While Cells(ActiveCell.Row, 1).Value <> ""
inicio = ActiveCell.Address
For i = 0 To 77
ActiveCell.Formula = "=WeeksofSale(" & Range(net).Offset(linha, i + 1).Address & ":" & Range(net).Offset(linha, 77).Address & _
"," & Range(inventario).Offset(linha, i).Address & ")*7"
ActiveCell.Offset(0, 1).Select
Next i
linha = linha + 1
Range(inicio).Select
ActiveCell.Offset(1, 0).Select
Wend

With Application
.Calculation = xlCalculationAutomatic
.Calculate
.EnableEvents = True
.ScreenUpdating = True
End With

End Sub
 
Postado : 21/05/2015 11:35 am
(@taniaweb)
Posts: 0
New Member
Topic starter
 

Obrigada Gilmar... mas é isso que estou usando hoje... o estranho é que em outras rotinas que chama funções nativas do excel (somase, procv, soma, etc) ele também cai num looping nesta função weekofsale....
às veze acho que o problema está nela...
Estou pesquisando mais opções... se descobrirem algo, gradeço...
Até mais

 
Postado : 22/05/2015 7:14 am