Nils_Junker
Board Regular
- Joined
- Jun 2, 2023
- Messages
- 80
- Office Version
- 365
- Platform
- Windows
Hi everybody,
i have a strange occuring problem.
sometimes when I run my vba code it works and sometimes error "400" comes up.
In the following you can see my code.
The problem I have is that i don't know where the problem is, because if i do the code step by step it is always working.
most of the time when i click on my button for the vba code it works until somewhere that point:
but i don'T know where exactly. or could it be that my code is working but some other thing isn't. I already checked the VBA-security and normally there shouldn't be a problem.
Thanks for your help!
i have a strange occuring problem.
sometimes when I run my vba code it works and sometimes error "400" comes up.
In the following you can see my code.
VBA Code:
Sub Main()
Dim dashboardSheet As Worksheet
Dim table1Sheet As Worksheet
Dim chart As chartObject
Dim chart1 As chartObject
Set dashboardSheet = ThisWorkbook.Sheets("Dashboard")
Set table1Sheet = ThisWorkbook.Sheets("Tabelle1")
For i = 1 To 35
table1Sheet.Columns("C").Delete Shift:=xlToLeft
Next i
'Sverweis für verbindung storage type
table1Sheet.range("C1").Value = "storage type"
table1Sheet.range("C3").formula = "=VLOOKUP(A3,MasterData!A:B,2,FALSE)"
table1Sheet.range("C3").AutoFill Destination:=table1Sheet.range("C3:C177")
table1Sheet.range("C190").Value = "GEN"
table1Sheet.range("C191").Value = "CLD"
table1Sheet.range("C192").Value = "NAR"
table1Sheet.range("C193").Value = "HSE"
table1Sheet.range("C190:C193").Copy
table1Sheet.range("C194").PasteSpecial Paste:=xlPasteValues
table1Sheet.range("C198").PasteSpecial Paste:=xlPasteValues
table1Sheet.range("C202").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
table1Sheet.range("C206").Value = "GEN-with factor"
table1Sheet.range("C207").Value = "CLD-with factor"
table1Sheet.range("C208").Value = "NAR-with factor"
table1Sheet.range("C209").Value = "HSE-with factor"
table1Sheet.range("C210").Value = "GEN-max capacity"
table1Sheet.range("C211").Value = "CLD-max capacity"
table1Sheet.range("C212").Value = "NAR-max capacity"
table1Sheet.range("C213").Value = "HSE-max capacity"
'datenvalidierung auf dashboard
Dim Liste As range
Dim rng As range
Set table1Sheet = ThisWorkbook.Sheets("Tabelle1")
Set Liste = table1Sheet.range("D2:BD2")
Set dashboardSheet = ThisWorkbook.Sheets("Dashboard")
Set rng = dashboardSheet.range("P17")
rng.Validation.Delete
With rng.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & Liste.Parent.Name & "!" & Liste.Address
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
'berechnugnen für diagramme erstellen
table1Sheet.range("D190").formula = "=SUMIF($C$3:$C$177,""GEN"",D3:D177)"
table1Sheet.range("D191").formula = "=SUMIF($C$3:$C$177,""CLD"",D3:D177)"
table1Sheet.range("D192").formula = "=SUMIF($C$3:$C$177,""NAR"",D3:D177)"
table1Sheet.range("D193").formula = "=SUMIF($C$3:$C$177,""HSE"",D3:D177)"
table1Sheet.range("D190:D193").AutoFill Destination:=table1Sheet.range("D190:BD193")
table1Sheet.range("D194").formula = "=D190*1"
table1Sheet.range("D195").formula = "=D191*1.622"
table1Sheet.range("D196").formula = "=D192*1.111"
table1Sheet.range("D197").formula = "=D193*1.111"
table1Sheet.range("D194:D197").AutoFill Destination:=table1Sheet.range("D194:BD197")
table1Sheet.range("D198").Value = "872"
table1Sheet.range("D199").Value = "158"
table1Sheet.range("D200").Value = "80"
table1Sheet.range("D201").Value = "5"
table1Sheet.range("D198:D201").AutoFill Destination:=table1Sheet.range("D198:BD201")
table1Sheet.range("D202").Formula2 = "=IF(Dashboard!$A$6=Tabelle1!C202,Tabelle1!D190:BD190,NA())"
table1Sheet.range("D202").AutoFill Destination:=table1Sheet.range("D202:D205")
table1Sheet.range("D206").Formula2 = "=IF(Dashboard!$A$6=Tabelle1!C202,Tabelle1!D194:BD194,NA())"
table1Sheet.range("D206").AutoFill Destination:=table1Sheet.range("D206:D209")
table1Sheet.range("D210").Formula2 = "=IF(Dashboard!$A$6=Tabelle1!C202,Tabelle1!D198:BD198,NA())"
table1Sheet.range("D210").AutoFill Destination:=table1Sheet.range("D210:D213")
table1Sheet.range("C214").Value = "GEN"
table1Sheet.range("C215").Value = "CLD"
table1Sheet.range("C216").Value = "NAR"
table1Sheet.range("C217").Value = "HSE"
table1Sheet.range("C218").Value = "GEN"
table1Sheet.range("C219").Value = "CLD"
table1Sheet.range("C220").Value = "NAR"
table1Sheet.range("C221").Value = "HSE"
table1Sheet.range("D214").formula = "=D190/D198"
table1Sheet.range("D215").formula = "=D191/D199"
table1Sheet.range("D216").formula = "=D192/D200"
table1Sheet.range("D217").formula = "=D193/D201"
table1Sheet.range("D218").Formula2Local = "=INDIREKT(ADRESSE(190;VERGLEICH(Dashboard!$P$17;Tabelle1!D2:BD2;0)+3))"
table1Sheet.range("D219").Formula2Local = "=INDIREKT(ADRESSE(191;VERGLEICH(Dashboard!$P$17;Tabelle1!D2:BD2;0)+3))"
table1Sheet.range("D220").Formula2Local = "=INDIREKT(ADRESSE(192;VERGLEICH(Dashboard!$P$17;Tabelle1!D2:BD2;0)+3))"
table1Sheet.range("D221").Formula2Local = "=INDIREKT(ADRESSE(193;VERGLEICH(Dashboard!$P$17;Tabelle1!D2:BD2;0)+3))"
'average berechnen
For i = 3 To 177
table1Sheet.range("BE" & i).Formula2Local = "=MITTELWERTWENN(D" & i & ":BD" & i & "; ""<>0"")"
Next i
'GEN
table1Sheet.range("D222").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""GEN"")*$BE$3:$BE$177;ZEILE(A1));(($C$3:$C$177=""GEN"")*$BE$3:$BE$177);0))"
table1Sheet.range("D223").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""GEN"")*$BE$3:$BE$177;2);($C$3:$C$177=""GEN"")*$BE$3:$BE$177;0))"
table1Sheet.range("D224").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""GEN"")*$BE$3:$BE$177;3);($C$3:$C$177=""GEN"")*$BE$3:$BE$177;0))"
'cld
table1Sheet.range("D225").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""CLD"")*$BE$3:$BE$177;ZEILE(A1));(($C$3:$C$177=""CLD"")*$BE$3:$BE$177);0))"
table1Sheet.range("D226").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""CLD"")*$BE$3:$BE$177;2);($C$3:$C$177=""CLD"")*$BE$3:$BE$177;0))"
table1Sheet.range("D227").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""CLD"")*$BE$3:$BE$177;3);($C$3:$C$177=""CLD"")*$BE$3:$BE$177;0))"
'nar
table1Sheet.range("D228").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""NAR"")*$BE$3:$BE$177;ZEILE(A1));(($C$3:$C$177=""NAR"")*$BE$3:$BE$177);0))"
table1Sheet.range("D229").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""NAR"")*$BE$3:$BE$177;2);($C$3:$C$177=""NAR"")*$BE$3:$BE$177;0))"
table1Sheet.range("D230").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""NAR"")*$BE$3:$BE$177;3);($C$3:$C$177=""NAR"")*$BE$3:$BE$177;0))"
'hse
table1Sheet.range("D231").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""HSE"")*$BE$3:$BE$177;ZEILE(A1));(($C$3:$C$177=""HSE"")*$BE$3:$BE$177);0))"
table1Sheet.range("D232").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""HSE"")*$BE$3:$BE$177;2);($C$3:$C$177=""HSE"")*$BE$3:$BE$177;0))"
table1Sheet.range("D233").Formula2Local = "=INDEX($B$3:$B$177;VERGLEICH(AGGREGAT(14;6;($C$3:$C$177=""HSE"")*$BE$3:$BE$177;3);($C$3:$C$177=""HSE"")*$BE$3:$BE$177;0))"
'ab hier average zahl finden
table1Sheet.range("E222").Formula2Local = "=SVERWEIS(D222;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E223").Formula2Local = "=SVERWEIS(D223;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E224").Formula2Local = "=SVERWEIS(D224;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E225").Formula2Local = "=SVERWEIS(D225;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E226").Formula2Local = "=SVERWEIS(D226;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E227").Formula2Local = "=SVERWEIS(D227;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E228").Formula2Local = "=SVERWEIS(D228;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E229").Formula2Local = "=SVERWEIS(D229;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E230").Formula2Local = "=SVERWEIS(D230;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E231").Formula2Local = "=SVERWEIS(D231;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E232").Formula2Local = "=SVERWEIS(D232;$B$3:$BE$177;56;FALSCH)"
table1Sheet.range("E233").Formula2Local = "=SVERWEIS(D233;$B$3:$BE$177;56;FALSCH)"
'ab hier max zahl finden
table1Sheet.range("F222").Formula2Local = "=MAX(WENN($B$3:$B$177=D222;$D$3:$BD$177))"
table1Sheet.range("F223").Formula2Local = "=MAX(WENN($B$3:$B$177=D223;$D$3:$BD$177))"
table1Sheet.range("F224").Formula2Local = "=MAX(WENN($B$3:$B$177=D224;$D$3:$BD$177))"
table1Sheet.range("F225").Formula2Local = "=MAX(WENN($B$3:$B$177=D225;$D$3:$BD$177))"
table1Sheet.range("F226").Formula2Local = "=MAX(WENN($B$3:$B$177=D226;$D$3:$BD$177))"
table1Sheet.range("F227").Formula2Local = "=MAX(WENN($B$3:$B$177=D227;$D$3:$BD$177))"
table1Sheet.range("F228").Formula2Local = "=MAX(WENN($B$3:$B$177=D228;$D$3:$BD$177))"
table1Sheet.range("F229").Formula2Local = "=MAX(WENN($B$3:$B$177=D229;$D$3:$BD$177))"
table1Sheet.range("F230").Formula2Local = "=MAX(WENN($B$3:$B$177=D230;$D$3:$BD$177))"
table1Sheet.range("F231").Formula2Local = "=MAX(WENN($B$3:$B$177=D231;$D$3:$BD$177))"
table1Sheet.range("F232").Formula2Local = "=MAX(WENN($B$3:$B$177=D232;$D$3:$BD$177))"
table1Sheet.range("F233").Formula2Local = "=MAX(WENN($B$3:$B$177=D233;$D$3:$BD$177))"
'ins dashboard einfügen products
dashboardSheet.range("C33").Formula2Local = "=Tabelle1!D222"
dashboardSheet.range("C34").Formula2Local = "=Tabelle1!D223"
dashboardSheet.range("C35").Formula2Local = "=Tabelle1!D224"
dashboardSheet.range("C37").Formula2Local = "=Tabelle1!D225"
dashboardSheet.range("C38").Formula2Local = "=Tabelle1!D226"
dashboardSheet.range("C39").Formula2Local = "=Tabelle1!D227"
dashboardSheet.range("C41").Formula2Local = "=Tabelle1!D228"
dashboardSheet.range("C42").Formula2Local = "=Tabelle1!D229"
dashboardSheet.range("C43").Formula2Local = "=Tabelle1!D230"
dashboardSheet.range("C45").Formula2Local = "=Tabelle1!D231"
dashboardSheet.range("C46").Formula2Local = "=Tabelle1!D232"
dashboardSheet.range("C47").Formula2Local = "=Tabelle1!D233"
'ins dashboard einfügen averages
dashboardSheet.range("G33").Formula2Local = "=Tabelle1!E222"
dashboardSheet.range("G34").Formula2Local = "=Tabelle1!E223"
dashboardSheet.range("G35").Formula2Local = "=Tabelle1!E224"
dashboardSheet.range("G37").Formula2Local = "=Tabelle1!E225"
dashboardSheet.range("G38").Formula2Local = "=Tabelle1!E226"
dashboardSheet.range("G39").Formula2Local = "=Tabelle1!E227"
dashboardSheet.range("G41").Formula2Local = "=Tabelle1!E228"
dashboardSheet.range("G42").Formula2Local = "=Tabelle1!E229"
dashboardSheet.range("G43").Formula2Local = "=Tabelle1!E230"
dashboardSheet.range("G45").Formula2Local = "=Tabelle1!E231"
dashboardSheet.range("G46").Formula2Local = "=Tabelle1!E232"
dashboardSheet.range("G47").Formula2Local = "=Tabelle1!E233"
'ins dashboard einfügen maximale werte
dashboardSheet.range("H33").Formula2Local = "=Tabelle1!F222"
dashboardSheet.range("H34").Formula2Local = "=Tabelle1!F223"
dashboardSheet.range("H35").Formula2Local = "=Tabelle1!F224"
dashboardSheet.range("H37").Formula2Local = "=Tabelle1!F225"
dashboardSheet.range("H38").Formula2Local = "=Tabelle1!F226"
dashboardSheet.range("H39").Formula2Local = "=Tabelle1!F227"
dashboardSheet.range("H41").Formula2Local = "=Tabelle1!F228"
dashboardSheet.range("H42").Formula2Local = "=Tabelle1!F229"
dashboardSheet.range("H43").Formula2Local = "=Tabelle1!F230"
dashboardSheet.range("H45").Formula2Local = "=Tabelle1!F231"
dashboardSheet.range("H46").Formula2Local = "=Tabelle1!F232"
dashboardSheet.range("H47").Formula2Local = "=Tabelle1!F233"
'dezimalstellen entfernen
Set rng = dashboardSheet.range("G33:G47")
rng.NumberFormat = "0"
'als Prozentzahlen definieren
Dim Bereich As range
Set Bereich = table1Sheet.range("D214:D217")
Bereich.NumberFormat = "0.00%"
'Diagramm einfügen
Dim DiagrammObjekt As chartObject
Dim DatenBereich As range
Dim DiagrammBereich As range
Dim Farben As Variant
Set DatenBereich = ThisWorkbook.Sheets("Tabelle1").range("C202:BD213")
Set DiagrammBereich = ThisWorkbook.Sheets("Tabelle1").range("A301")
Set DiagrammObjekt = ThisWorkbook.Sheets("Tabelle1").ChartObjects.Add( _
Left:=DiagrammBereich.Left, _
Width:=500, _
Top:=DiagrammBereich.Top, _
Height:=300)
Farben = Array(RGB(0, 0, 201), RGB(0, 149, 255), RGB(13, 189, 186), RGB(103, 187, 110), RGB(157, 115, 247), RGB(217, 87, 118), RGB(244, 156, 52), RGB(248, 223, 90), RGB(244, 221, 186), RGB(127, 127, 127), RGB(140, 80, 8), RGB(0, 0, 100))
With DiagrammObjekt.chart
.SetSourceData Source:=DatenBereich
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Text = "1. Diagramm"
For i = 1 To .SeriesCollection.Count
.SeriesCollection(i).Format.Line.ForeColor.RGB = Farben(i - 1)
Next i
End With
'ab hier bearbeitung Diagramm 1
Dim wsQuelle As Worksheet
Dim wsZiel As Worksheet
Dim chartQuelle As chartObject
Dim chartZiel As chartObject
Set wsQuelle = ThisWorkbook.Sheets("Tabelle1")
Set wsZiel = ThisWorkbook.Sheets("Dashboard")
Set chartQuelle = wsQuelle.ChartObjects("Diagramm 1")
chartQuelle.Copy
wsZiel.Paste Destination:=wsZiel.range("A1")
Set chartZiel = wsZiel.ChartObjects(wsZiel.ChartObjects.Count)
With chartZiel.chart.Axes(xlCategory, xlPrimary)
.HasTitle = True
.AxisTitle.Text = "weeks"
End With
With chartZiel.chart.Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Text = "palletspots"
End With
chartZiel.chart.HasTitle = False
chartZiel.Top = 92
chartZiel.Left = 0
chartZiel.Width = 480
chartZiel.Height = 300
Set ws = ThisWorkbook.Sheets("Dashboard")
Set chartObj = ws.ChartObjects(ws.ChartObjects.Count)
With chartObj.chart.ChartArea.Border
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
chartObj.chart.Refresh
'ab hier 2. Diagramm
Set DatenBereich = ThisWorkbook.Sheets("Tabelle1").range("C214:D217")
Set DiagrammBereich = ThisWorkbook.Sheets("Tabelle1").range("A302")
Set DiagrammObjekt = ThisWorkbook.Sheets("Tabelle1").ChartObjects.Add( _
Left:=DiagrammBereich.Left, _
Width:=500, _
Top:=DiagrammBereich.Top, _
Height:=300)
With DiagrammObjekt.chart
.SetSourceData Source:=DatenBereich
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = "2. Diagramm"
For i = 1 To .SeriesCollection.Count
.SeriesCollection(i).Format.Fill.ForeColor.RGB = Farben(i - 1)
Next i
End With
Set chartQuelle = wsQuelle.ChartObjects("Diagramm 2")
chartQuelle.Copy
wsZiel.Paste Destination:=wsZiel.range("A1")
Set chartZiel = wsZiel.ChartObjects(wsZiel.ChartObjects.Count)
chartZiel.chart.HasTitle = False
chartZiel.chart.HasLegend = False
chartZiel.Top = 150
chartZiel.Left = 485
chartZiel.Width = 415
chartZiel.Height = 242
Set ws = ThisWorkbook.Sheets("Dashboard")
Set chartObj = ws.ChartObjects(ws.ChartObjects.Count)
With chartObj.chart.ChartArea.Border
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
chartObj.chart.Refresh
Application.CutCopyMode = False
'Kreisdiagramm hinzufügen
Dim rngData As range
Dim cht As chartObject
Set ws = ThisWorkbook.Sheets("Tabelle1")
Set rngData = ws.range("C218:D221")
Set cht = ws.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
cht.chart.ChartType = xlPie
cht.chart.SetSourceData Source:=rngData
For i = 1 To rngData.Rows.Count
cht.chart.SeriesCollection(1).Points(i).Format.Fill.ForeColor.RGB = Farben(i - 1 Mod UBound(Farben) + 1)
Next i
With cht.chart.ChartArea.Border
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
cht.chart.HasTitle = False
cht.chart.SeriesCollection(1).ApplyDataLabels
Dim wsSource As Worksheet
Dim wsDashboard As Worksheet
Dim chtSource As chartObject
Dim chtDashboard As chartObject
Set wsSource = ThisWorkbook.Sheets("Tabelle1")
Set wsDashboard = ThisWorkbook.Sheets("Dashboard")
Set chtSource = wsSource.ChartObjects("Diagramm 3")
chtSource.Copy
wsDashboard.Paste Destination:=wsDashboard.range("A303")
Set chtDashboard = wsDashboard.ChartObjects(wsDashboard.ChartObjects.Count)
chtDashboard.Left = 904
chtDashboard.Top = 255
chtDashboard.Width = 230
chtDashboard.Height = 137
MsgBox "Edits performed"
End Sub
The problem I have is that i don't know where the problem is, because if i do the code step by step it is always working.
most of the time when i click on my button for the vba code it works until somewhere that point:
but i don'T know where exactly. or could it be that my code is working but some other thing isn't. I already checked the VBA-security and normally there shouldn't be a problem.
Thanks for your help!