Hi Everybody, I am having a problem with Excel VBA codes.
The code that i am trying to do, makes a prediction with the chart. But, i don't know why, every time that i run the code, excel just start not to responding and crash it all.
(I am from Brazil, so, sorry for any english mistake);
It starts to do this after i make that condition with "if":
i = 1
loopp:
If i < fatorm Then
Worksheets("LDT").Cells(i, 7).Value = 1
GoTo loopp
Else
End If
fator = Worksheets("LDT").Range(Sheets("LDT").Cells(1, 7), Sheets("LDT").Cells(i, 7))
-----------------------------
Code:
Private Sub GraficoCapacitancia_Click()
Dim rng As Range
Dim cht As Object
Dim a, b
Dim CurrentSheet As Worksheet
Dim sht As Worksheet
a = Worksheets("LDT").Cells(3, 22)
Dim s As Series
'Your data range for the chart
Set rng = Sheets("LDT").Range(Sheets("LDT").Cells(1, 2), Sheets("LDT").Cells(a, 2))
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines
cht.Name = "Capacitancia"
Dim dias, meddias, fatorm, d, fator, i
meddias = Worksheets("LDT").Cells(2, 19).Value
dias = InputBox("Digite quantos dias adiante a previsão será feita")
d = Day(Now)
fatorm = (dias * meddias) + (Worksheets("LDT").Cells(3, 22).Value)
i = 1
loopp:
If i < fatorm Then
Worksheets("LDT").Cells(i, 7).Value = 1
GoTo loopp
Else
End If
fator = Worksheets("LDT").Range(Sheets("LDT").Cells(1, 7), Sheets("LDT").Cells(i, 7))
Application.ScreenUpdating = False
Application.EnableEvents = False
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
For Each cht In sht.ChartObjects
cht.Activate
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlPolynomial, Order:=2, Forward:=0, Backward:=0, DisplayEquation:=True, DisplayRSquared:=False).Select
ActiveChart.SeriesCollection(1).XValues = fator
Next cht
Next sht
With ActiveSheet.Shapes("Capacitancia")
.Left = Range("I1").Left
.Top = Range("I1").Top
End With
End Sub
The code that i am trying to do, makes a prediction with the chart. But, i don't know why, every time that i run the code, excel just start not to responding and crash it all.
(I am from Brazil, so, sorry for any english mistake);
It starts to do this after i make that condition with "if":
i = 1
loopp:
If i < fatorm Then
Worksheets("LDT").Cells(i, 7).Value = 1
GoTo loopp
Else
End If
fator = Worksheets("LDT").Range(Sheets("LDT").Cells(1, 7), Sheets("LDT").Cells(i, 7))
-----------------------------
Code:
Private Sub GraficoCapacitancia_Click()
Dim rng As Range
Dim cht As Object
Dim a, b
Dim CurrentSheet As Worksheet
Dim sht As Worksheet
a = Worksheets("LDT").Cells(3, 22)
Dim s As Series
'Your data range for the chart
Set rng = Sheets("LDT").Range(Sheets("LDT").Cells(1, 2), Sheets("LDT").Cells(a, 2))
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines
cht.Name = "Capacitancia"
Dim dias, meddias, fatorm, d, fator, i
meddias = Worksheets("LDT").Cells(2, 19).Value
dias = InputBox("Digite quantos dias adiante a previsão será feita")
d = Day(Now)
fatorm = (dias * meddias) + (Worksheets("LDT").Cells(3, 22).Value)
i = 1
loopp:
If i < fatorm Then
Worksheets("LDT").Cells(i, 7).Value = 1
GoTo loopp
Else
End If
fator = Worksheets("LDT").Range(Sheets("LDT").Cells(1, 7), Sheets("LDT").Cells(i, 7))
Application.ScreenUpdating = False
Application.EnableEvents = False
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
For Each cht In sht.ChartObjects
cht.Activate
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlPolynomial, Order:=2, Forward:=0, Backward:=0, DisplayEquation:=True, DisplayRSquared:=False).Select
ActiveChart.SeriesCollection(1).XValues = fator
Next cht
Next sht
With ActiveSheet.Shapes("Capacitancia")
.Left = Range("I1").Left
.Top = Range("I1").Top
End With
End Sub