Thema Datum  Von Nutzer Rating
Antwort
Rot Fehler 2015
04.04.2024 08:39:44 Johnny
NotSolved
04.04.2024 09:29:36 Gast61437
NotSolved
04.04.2024 09:39:28 Gast61437
Solved
04.04.2024 09:46:06 Gast61437
Solved
04.04.2024 09:49:47 Gast60012
NotSolved
04.04.2024 13:44:57 Gast94357
NotSolved
08.04.2024 02:59:09 Gast7777
NotSolved

Ansicht des Beitrags:
Von:
Johnny
Datum:
04.04.2024 08:39:44
Views:
487
Rating: Antwort:
  Ja
Thema:
Fehler 2015

Hallo.

Ich habe vom Kollegen (der leider ausgeschieden ist) eine Excel Tabelle (aktuelle Version) mit einem VBA-Script übernommen. Das Script soll die 3 niedrigsten Preise einer Matrix farblich markieren, und zwar nach einem aufsteigenden Prinzip (den kleinsten Preis "grün" dann "gelb" und anschl. "rot").

Es sind 4 verschiedene Bereiche, welche alle gleich aussehen. Die Zahlen in "schwarz" sind Grundpreise, welche nicht beachtet werden sollen. Nur die Zahlen in "blau" sollen verglichen werden. Diese ergeben sich aus Addition des Grundwertes und verschiedener zusätzlicher Werte. Jeden Monat ändern sich die "blauen" Werte und sind somit immer unterschiedlich. Die erste Zelle (Zahl 585,00 €) in der u.a. Tabelle ist F10. Hier ein Beispiel.

 

        585,00 €         654,53 €         556,10 €         642,27 €         523,20 €         528,43 €         510,00 €         607,91 €         628,00 €         739,78 €         518,00 €         648,43 €         550,00 €         694,10 €         807,58 €         856,03 €         782,00 €         905,56 €                  -   €                  -   € 559,00 €         561,68 €         705,00 €         789,95 €
        565,00 €         628,86 €         578,80 €         667,26 €         472,00 €         476,72 €         475,00 €         566,33 €         599,00 €         705,62 €         758,00 €         914,11 €         560,00 €         706,72 €         746,59 €         791,39 €         762,00 €         882,40 €                  -   €                  -   € 629,00 €         632,02 €         700,00 €         784,35 €
        635,00 €         712,32 €         737,60 €         842,10 €         630,70 €         637,01 €         679,00 €         787,51 €         666,00 €         784,55 €         750,00 €         905,25 €         589,00 €         743,32 €         832,16 €         882,09 €         802,00 €         928,72 €                  -   €                  -   € 680,00 €         683,26 €                  -   €                  -   €
        730,00 €         828,56 €         766,60 €         874,03 €         863,60 €         872,24 €         711,00 €         840,54 €         761,00 €         896,46 €         840,00 €     1.004,88 €         748,00 €         943,98 €         991,91 €     1.051,42 €         882,00 €     1.021,36 €                  -   €                  -   € 800,00 €         803,84 €                  -   €                  -   €
        925,00 €     1.058,25 €     1.116,40 €     1.259,16 €     1.239,30 €     1.251,69 €     1.020,00 €     1.193,10 €                  -   €                  -   €         969,60 €     1.148,35 €         842,00 €     1.062,60 €     1.197,31 €     1.269,15 €     1.072,00 €     1.241,38 €                  -   €                  -   € 1.313,00 €     1.319,30 €     1.380,00 €     1.546,29 €
        935,00 €     1.075,16 €     1.196,90 €     1.347,79 €     1.150,40 €     1.161,90 €     1.020,00 €     1.192,78 €                  -   €                  -   €         974,00 €     1.153,22 €         972,00 €     1.226,66 €     1.211,36 €     1.284,05 €     1.072,00 €     1.241,38 €                  -   €                  -   € 1.113,00 €     1.118,34 €     1.160,00 €     1.299,78 €
        925,00 €     1.060,46 €     1.193,30 €     1.343,82 €     1.107,20 €     1.118,27 €     1.020,00 €     1.194,99 €                  -   €                  -   €         969,60 €     1.148,35 €         972,00 €     1.226,66 €     1.251,20 €     1.326,27 €     1.032,00 €     1.195,06 €                  -   €                  -   € 1.025,00 €     1.029,92 €     1.140,00 €     1.277,37 €
        935,00 €     1.071,99 €     1.196,90 €     1.347,79 €     1.161,60 €     1.173,22 €     1.020,00 €     1.199,42 €                  -   €                  -   €         969,50 €     1.148,24 €         972,00 €     1.226,66 €     1.222,44 €     1.295,78 €     1.072,00 €     1.241,38 €                  -   €                  -   € 1.109,00 €     1.114,32 €     1.225,00 €     1.372,61 €
        875,00 €     1.000,58 €     1.273,10 €     1.431,68 €     1.098,20 €     1.109,18 €         942,00 €     1.105,49 €                  -   €                  -   €         969,60 €     1.148,35 €         874,80 €     1.104,00 €     1.142,90 €     1.211,47 €         972,00 €     1.125,58 €                  -   €                  -   € 1.008,00 €     1.012,84 €     1.140,00 €     1.277,37 €
        815,00 €         932,97 €     1.003,90 €     1.135,29 €     1.025,10 €     1.035,35 €         925,00 €     1.075,81 €                  -   €                  -   €         973,00 €     1.152,11 €         799,20 €     1.008,59 €     1.142,90 €     1.211,47 €         942,00 €     1.090,84 €                  -   €                  -   € 945,00 €         949,54 €         970,00 €     1.086,89 €
        895,00 €     1.020,55 €     1.000,90 €     1.131,99 €     1.137,30 €     1.148,67 €         925,00 €     1.086,40 €                  -   €                  -   €         969,60 €     1.148,35 €         799,20 €     1.008,59 €     1.142,90 €     1.211,47 €         972,00 €     1.125,58 €                  -   €                  -   € 1.031,00 €     1.035,95 €     1.045,00 €     1.170,92 €
        855,00 €         972,27 €         937,50 €     1.062,19 €         997,90 €     1.007,88 €         860,00 €     1.013,92 €                  -   €                  -   €         973,00 €     1.152,11 €         756,00 €         954,07 €     1.083,21 €     1.148,20 €         942,00 €     1.090,84 €                  -   €                  -   € 970,00 €         974,66 €         915,00 €     1.025,26 €
        815,00 €         924,04 €         922,40 €     1.045,56 €         931,60 €         940,92 €         773,00 €         912,14 €                  -   €                  -   €         744,00 €         898,61 €         744,00 €         938,93 €     1.060,38 €     1.124,01 €         902,00 €     1.044,52 €                  -   €                  -   € 930,00 €         934,46 €         895,00 €     1.002,85 €
        660,00 €         745,59 €         798,30 €         908,93 €         749,70 €         757,20 €         600,00 €         712,62 €                  -   €                  -   €         867,20 €     1.034,99 €         668,00 €         843,02 €         916,42 €         971,40 €         832,00 €         963,46 €                  -   €                  -   € 758,00 €         761,64 €         810,00 €         907,61 €
        660,00 €         740,74 €         839,50 €         954,29 €         686,80 €         693,67 €         562,00 €         671,27 €                  -   €                  -   €         793,60 €         953,52 €         668,00 €         843,02 €         863,75 €         915,58 €         802,00 €         928,72 €                  -   €                  -   € 732,00 €         735,51 €         715,00 €         801,16 €
        545,00 €         594,28 €         556,40 €         642,60 €         450,00 €         454,50 €         330,00 €         404,34 €                  -   €                  -   €         689,00 €         837,72 €         434,00 €         547,71 €         595,06 €         630,76 €         697,00 €         807,13 €                  -   €                  -   € 544,00 €         546,61 €         570,00 €         638,69 €
        680,00 €         767,10 €         893,00 €     1.013,19 €         751,40 €         758,91 €         709,00 €         829,95 €                  -   €                  -   €         860,80 €     1.027,91 €         692,00 €         873,30 €         916,42 €         971,40 €         832,00 €         963,46 €                  -   €                  -   € 772,00 €         775,71 €         780,00 €         873,99 €
        645,00 €         721,89 €         655,10 €         751,27 €         612,00 €         618,12 €         588,00 €         696,94 €         666,00 €         784,55 €         774,40 €         932,26 €         584,00 €         737,01 €         832,16 €         882,09 €         802,00 €         928,72 €                  -   €                  -   € 689,00 €         692,31 €         730,00 €         817,97 €
        730,00 €         831,05 €         750,80 €         856,63 €         790,40 €         798,30 €         654,00 €         783,09 €         713,00 €         839,91 €         848,00 €     1.013,74 €         695,00 €         877,09 €         967,34 €     1.025,38 €         780,00 €         903,24 €                  -   €                  -   € 785,00 €         788,77 €         795,00 €         890,80 €
        710,00 €         805,80 €         842,20 €         957,26 €         782,75 €         790,58 €         654,00 €         778,83 €         713,00 €         839,91 €     1.006,50 €     1.189,20 €         695,00 €         877,09 €         991,91 €     1.051,42 €         872,00 €     1.009,78 €                  -   €                  -   € 785,00 €         788,77 €         785,00 €         879,59 €
        770,00 €         882,19 €         828,60 €         942,29 €         940,80 €         950,21 €         763,00 €         906,27 €         818,00 €         963,60 €         864,00 €     1.031,45 €         746,00 €         941,45 €     1.060,38 €     1.124,01 €         932,00 €     1.079,26 €                  -   €                  -   € 918,00 €         922,41 €         840,00 €         941,22 €
        710,00 €         809,99 €         751,40 €         857,29 €         792,00 €         799,92 €         818,00 €         949,80 €         790,00 €         930,62 €         741,40 €         895,73 €         722,00 €         911,16 €     1.014,78 €     1.075,67 €         882,00 €     1.021,36 €                  -   €                  -   € 808,00 €         811,88 €         820,00 €         918,81 €
        700,00 €         798,62 €         751,40 €         857,29 €         806,40 €         814,46 €         818,00 €         945,54 €         732,00 €         862,30 €         741,40 €         895,73 €         722,00 €         911,16 €         966,29 €     1.024,26 €         882,00 €     1.021,36 €                  -   €                  -   € 793,00 €         796,81 €         800,00 €         896,40 €
        680,00 €         842,70 €                  -   €                  -   €         845,29 €         853,74 €                  -   €         117,69 €                  -   €                  -   €         985,00 €     1.165,40 €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €                  -   €
        605,00 €         673,97 €         653,70 €         749,72 €         548,80 €         554,29 €         530,00 €         624,72 €                  -   €                  -   €         636,80 €         779,94 €         590,00 €         744,58 €         746,59 €         791,39 €         762,00 €         882,40 €                  -   €                  -   € 649,00 €         652,12 €                  -   €                  -   €
        545,00 €         603,27 €         603,20 €         694,12 €         440,00 €         444,40 €         392,00 €         475,63 €                  -   €                  -   €         592,00 €         730,34 €         516,00 €         651,19 €         689,30 €         730,66 €         650,00 €         752,70 €                  -   €                  -   € 610,00 €         612,93 €                  -   €                  -   €
        505,00 €         541,62 €         566,90 €         654,16 €         350,00 €         353,50 €         300,00 €         360,01 €                  -   €                  -   €         745,60 €         900,38 €         466,00 €         588,09 €         415,79 €         440,74 €         632,00 €         731,86 €                  -   €                  -   € 482,00 €         484,31 €                  -   €                  -   €
        625,00 €         700,12 €         636,20 €         730,46 €         603,50 €         609,54 €         493,00 €         592,77 €                  -   €                  -   €         902,40 €     1.073,96 €         722,00 €         911,16 €         774,31 €         820,77 €         792,00 €         917,14 €                  -   €                  -   € 840,00 €         844,03 €                  -   €                  -   €
        485,00 €         530,16 €         559,00 €         645,46 €         400,00 €         404,00 €         347,00 €         413,64 €         538,00 €         633,76 €         675,20 €         822,45 €         495,00 €         624,69 €         486,02 €         515,18 €         672,00 €         778,18 €                  -   €                  -   € 486,00 €         488,33 €                  -   €                  -   €
        485,00 €         526,81 €         559,00 €         645,46 €         400,00 €         404,00 €         347,00 €         414,11 €         538,00 €         633,76 €         675,20 €         822,45 €         495,00 €         624,69 €         507,89 €         538,37 €         672,00 €         778,18 €                  -   €                  -   € 486,00 €         488,33 €                  -   €                  -   €
        605,00 €         661,79 €         527,30 €         610,56 €         455,00 €         459,55 €         420,00 €         495,46 €         538,00 €         633,76 €         876,80 €     1.045,62 €         613,00 €         773,61 €         576,57 €         611,17 €         712,00 €         824,50 €                  -   €                  -   € 629,00 €         632,02 €                  -   €                  -   €
        255,00 €         262,65 €         385,40 €         454,33 €         300,00 €         303,00 €         300,00 €         340,74 €                  -   €                  -   €         392,00 €         508,94 €         315,00 €         397,53 €         219,11 €         232,26 €         352,00 €         407,62 €                  -   €                  -   € 220,00 €         221,06 €                  -   €                  -   €
        470,00 €         497,97 €         508,90 €         590,30 €         350,00 €         353,50 €         420,00 €         478,72 €                  -   €                  -   €         534,00 €         666,14 €         405,00 €         511,11 €         499,69 €         529,67 €         532,00 €         616,06 €                  -   €                  -   € 415,00 €         416,99 €                  -   €                  -   €
        545,00 €         595,25 €         563,60 €         650,52 €         430,40 €         434,70 €         370,00 €         447,60 €         571,00 €         672,64 €         822,40 €         985,40 €         480,00 €         605,76 €         576,57 €         611,17 €         702,00 €         812,92 €                  -   €                  -   € 606,00 €         608,91 €         640,00 €         717,12 €

Im Objekt ist folgender Code hinterlegt:

Private Sub Worksheet_Change(ByVal Target As Range)
    ' Call the ApplyHighlightCode subroutine with the Target argument to update highlights
    ApplyHighlightCode Target
End Sub
Private Sub Workbook_Open()
    Application.EnableEvents = True
End Sub

 

Als Module folgender Code:

' Module1 - This is a public module
Sub ApplyCodeToWorksheets()
    ' Apply the code to the "Test" worksheet
    ApplyHighlightCode ThisWorkbook.Sheets("Test").Range("A1")

    ' Add more worksheets as needed
    ' ApplyHighlightCode ThisWorkbook.Sheets("AnotherWorksheet").Range("A1")
End Sub

Sub ApplyHighlightCode(Target As Range)
    ' Extract the worksheet from the Target range
    Dim ws As Worksheet
    Set ws = Target.Worksheet

    ' Define the ranges for each worksheet
    Dim rng1 As String, rng2 As String, rng3 As String, rng4 As String

    ' Define the ranges for the specific worksheet
    Select Case ws.Name
    
        Case "Test"
            rng1 = "F10:AC43"
            rng2 = "F55:AC88"
            rng3 = "AJ10:BE43"
            rng4 = "AJ55:BE88"
       
        ' Add more cases for other worksheets as needed
    End Select

    ' Define the union of ranges
    Dim rng As Range
    Set rng = Union(ws.Range(rng1), ws.Range(rng2), ws.Range(rng3), ws.Range(rng4))

    Application.ScreenUpdating = False ' Disable screen updating for faster execution

    ' Clear previous highlights
    rng.Interior.ColorIndex = xlNone

    For Each Row In rng.Rows
        ' Initialize variables to keep track of the lowest, second lowest, and third lowest values for the current row
        lowestValue = 0
        secondLowestValue = 0
        thirdLowestValue = 0

        ' Initialize variables to keep track of the cells containing the lowest, second lowest, and third lowest values
        Set lowestCell = Nothing
        Set secondLowestCell = Nothing
        Set thirdLowestCell = Nothing

        ' Populate the array with row values
        valuesArray = Row.Value

        ' Loop through each cell in the current row
        For j = 1 To UBound(valuesArray, 2)
            ' Get the entire column name (e.g., "F", "G", "AH")
            Dim columnName As String
            columnName = Split(Row.Cells(1, j).Address, "$")(1)

            ' Check if the entire column name is in the list of excluded columns
            If Not IsExcludedColumn(columnName) Then
                ' Check if the value is numeric and greater than 1
                If IsNumeric(valuesArray(1, j)) And valuesArray(1, j) > 1 Then
                    ' Compare the value to the current lowest, second lowest, and third lowest values
                    If valuesArray(1, j) < lowestValue Or lowestValue = 0 Then
                        thirdLowestValue = secondLowestValue
                        Set thirdLowestCell = secondLowestCell
                        secondLowestValue = lowestValue
                        Set secondLowestCell = lowestCell
                        lowestValue = valuesArray(1, j)
                        Set lowestCell = Row.Cells(1, j)
                    ElseIf valuesArray(1, j) < secondLowestValue Or secondLowestValue = 0 Then
                        thirdLowestValue = secondLowestValue
                        Set thirdLowestCell = secondLowestCell
                        secondLowestValue = valuesArray(1, j)
                        Set secondLowestCell = Row.Cells(1, j)
                    ElseIf valuesArray(1, j) < thirdLowestValue Or thirdLowestValue = 0 Then
                        thirdLowestValue = valuesArray(1, j)
                        Set thirdLowestCell = Row.Cells(1, j)
                    End If
                End If
            End If
        Next j

        ' Apply formatting to the cells with the lowest, second lowest, and third lowest values
        If Not lowestCell Is Nothing Then lowestCell.Interior.Color = RGB(147, 237, 135) ' Red
        If Not secondLowestCell Is Nothing Then secondLowestCell.Interior.Color = RGB(255, 255, 0) ' Yellow
        If Not thirdLowestCell Is Nothing Then thirdLowestCell.Interior.Color = RGB(252, 192, 200) ' Green
    Next Row

    Application.ScreenUpdating = True ' Re-enable screen updating
End Sub

' Function to check if a column should be excluded
Function IsExcludedColumn(columnName As String) As Boolean
    Dim excludedColumns As String
    excludedColumns = "F,H,J,L,N,P,R,T,V,X,Z,AB,AD,AE,AF,AG,AH,AI,AJ,AL,AN,AP,AR,AT,AV,AX,AZ,BB,BD,BF,BH,BJ"

    ' Check if the columnName is in the list of excluded columns
    IsExcludedColumn = InStr(1, excludedColumns, columnName, vbTextCompare) > 0
End Function


Seit dem die Tabelle etwas ergänzt wurde, funktioniert das Script nicht mehr richtig.

Beim Ausführen des Scriptes kommt ein Fehler und beim debbugen wir in folgender Zeile

 If IsNumeric(valuesArray(1, j)) And valuesArray(1, j) > 1 Then

der Fehler angezeigt:

valuesArray(1, j) = Fehler 2015

 

Ich hoffe, ich habe alles Nötige geschildert. Falls noch etwas gebraucht wird, bitte fragen.

Hat vielleicht jemand eine Idee.

Vielen Dank für die Hilfe.

VG

Johnny


Ihre Antwort
  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen
Thema: Name: Email:



  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen

Thema Datum  Von Nutzer Rating
Antwort
Rot Fehler 2015
04.04.2024 08:39:44 Johnny
NotSolved
04.04.2024 09:29:36 Gast61437
NotSolved
04.04.2024 09:39:28 Gast61437
Solved
04.04.2024 09:46:06 Gast61437
Solved
04.04.2024 09:49:47 Gast60012
NotSolved
04.04.2024 13:44:57 Gast94357
NotSolved
08.04.2024 02:59:09 Gast7777
NotSolved