سؤال

لدي جدول بيانات Excel مثل هذا

id | data for id
   | more data for id
id | data for id
id | data for id
   | more data for id
   | even more data for id
id | data for id
   | more data for id
id | data for id
id | data for id
   | more data for id

أريد الآن تجميع بيانات معرف واحد عن طريق تبديل لون خلفية الصفوف

var color = white
for each row
    if the first cell is not empty and color is white
        set color to green
    if the first cell is not empty and color is green
        set color to white
    set background of row to color

هل يمكن لأي شخص مساعدتي في ماكرو أو بعض رموز VBA

شكرًا

هل كانت مفيدة؟

المحلول

أعتقد أن هذا يفعل ما تبحث عنه.يقلب اللون عندما تغير قيمة الخلية الموجودة في العمود A.يتم تشغيله حتى لا توجد قيمة في العمود B.

Public Sub HighLightRows()
    Dim i As Integer
    i = 1
    Dim c As Integer
    c = 3       'red

    Do While (Cells(i, 2) <> "")
        If (Cells(i, 1) <> "") Then    'check for new ID
            If c = 3 Then
                c = 4   'green
            Else
                c = 3   'red
            End If
        End If

        Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c
        i = i + 1
    Loop
End Sub

نصائح أخرى

أستخدم هذه الصيغة للحصول على مدخلات التنسيق الشرطي:

=IF(B2=B1,E1,1-E1))    [content of cell E2]

حيث يحتوي العمود B على العنصر الذي يجب تجميعه ويكون E عمودًا مساعدًا.في كل مرة تكون فيها الخلية العلوية (B1 في هذه الحالة) هي نفس الخلية الحالية (B2)، يتم إرجاع محتوى الصف العلوي من العمود E.وإلا، فسوف يُرجع 1 ناقص هذا المحتوى (أي أن الناتج سيكون 0 أو 1، اعتمادًا على قيمة الخلية العلوية).

enter image description here

enter image description here

enter image description here

استنادًا إلى إجابة Jason Z، والتي يبدو أنها خاطئة من خلال اختباراتي (على الأقل في Excel 2010)، إليك بعض التعليمات البرمجية التي تناسبني:

Public Sub HighLightRows()
    Dim i As Integer
    i = 2 'start at 2, cause there's nothing to compare the first row with
    Dim c As Integer
    c = 2       'Color 1. Check http://dmcritchie.mvps.org/excel/colors.htm for color indexes

    Do While (Cells(i, 1) <> "")
        If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
            If c = 2 Then
                c = 34   'color 2
            Else
                c = 2   'color 1
            End If
        End If

        Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c
        i = i + 1
    Loop
End Sub

هل يجب عليك استخدام الكود؟إذا كان الجدول ثابتًا، فلماذا لا تستخدم إمكانية التنسيق التلقائي؟

enter image description here

قد يكون من المفيد أيضًا "دمج الخلايا" من نفس البيانات.لذلك ربما إذا قمت بدمج خلايا "البيانات، المزيد من البيانات، وحتى المزيد من البيانات" في خلية واحدة، فيمكنك التعامل بسهولة أكبر مع الحالة الكلاسيكية "كل صف هو صف".

أنا أقترح هذا وحاولت تعديله لاستخدامي.لدي أرقام الطلبات في العمود أ وبعض الطلبات تأخذ صفوفًا متعددة.أريد فقط تبديل اللون الأبيض والرمادي لكل رقم طلب.ما لدي هنا يتناوب في كل صف.

ChangeBackgroundColor() ' ChangeBackgroundColor Macro ' ' Keyboard Shortcut: Ctrl+Shift+B Dim a As Integer a = 1 Dim c As Integer c = 15 'gray Do While (Cells(a, 2) <> "") If (Cells(a, 1) <> "") Then 'check for new ID If c = 15 Then c = 2 'white Else c = 15 'gray End If End If Rows(Trim(Str(a)) + ":" + Trim(Str(a))).Interior.ColorIndex = c a = a + 1 Loop

End Sub

إذا قمت بتحديد خيار القائمة "التنسيق الشرطي" ضمن عنصر القائمة "التنسيق"، فسيتم إعطاؤك مربع حوار يتيح لك إنشاء بعض المنطق لتطبيقه على تلك الخلية.

قد لا يكون منطقك هو نفس الكود أعلاه، وقد يبدو أكثر مثل:

قيمة الخلية هي | يساوي | | و | أبيض ....ثم اختر اللون.

يمكنك تحديد زر الإضافة وجعل الحالة كبيرة حسب حاجتك.

لقد قمت بإعادة صياغة إجابة Bartdude، بالنسبة إلى Light Gray / White استنادًا إلى عمود قابل للتكوين، باستخدام قيم RGB.يتم قلب var المنطقي عندما تتغير القيمة ويستخدم هذا لفهرسة مصفوفة الألوان عبر القيم الصحيحة True وFalse.يعمل بالنسبة لي في عام 2010.اتصل بالفرعي برقم الورقة.

Public Sub HighLightRows(intSheet As Integer)
    Dim intRow As Integer: intRow = 2 ' start at 2, cause there's nothing to compare the first row with
    Dim intCol As Integer: intCol = 1 ' define the column with changing values
    Dim Colr1 As Boolean: Colr1 = True ' Will flip True/False; adding 2 gives 1 or 2
    Dim lngColors(2 + True To 2 + False) As Long   ' Indexes : 1 and 2
          ' True = -1, array index 1.    False = 0, array index 2.
    lngColors(2 + False) = RGB(235, 235, 235) ' lngColors(2) = light grey
    lngColors(2 + True) = RGB(255, 255, 255) '  lngColors(1) = white

    Do While (Sheets(intSheet).Cells(intRow, 1) <> "")
        'check for different value in intCol, flip the boolean if it's different
        If (Sheets(intSheet).Cells(intRow, intCol) <> Sheets(intSheet).Cells(intRow - 1, intCol)) Then Colr1 = Not Colr1
        Sheets(intSheet).Rows(intRow).Interior.Color = lngColors(2 + Colr1) ' one colour or the other
        ' Optional : retain borders (these no longer show through when interior colour is changed) by specifically setting them
        With Sheets(intSheet).Rows(intRow).Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .Color = RGB(220, 220, 220)
        End With
        intRow = intRow + 1
    Loop
End Sub

مكافأة اختيارية:بالنسبة لبيانات SQL، قم بتلوين أي قيم فارغة بنفس اللون الأصفر المستخدم في SSMS

Public Sub HighLightNULLs(intSheet As Integer)
    Dim intRow As Integer: intRow = 2 ' start at 2 to avoid the headings
    Dim intCol As Integer
    Dim lngColor As Long: lngColor = RGB(255, 255, 225) ' pale yellow

    For intRow = intRow To Sheets(intSheet).UsedRange.Rows.Count
        For intCol = 1 To Sheets(intSheet).UsedRange.Columns.Count
            If Sheets(intSheet).Cells(intRow, intCol) = "NULL" Then Sheets(intSheet).Cells(intRow, intCol).Interior.Color = lngColor
        Next intCol
    Next intRow
End Sub

أستخدم هذه القاعدة في Excel لتنسيق الصفوف البديلة:

  1. قم بتمييز الصفوف التي ترغب في تطبيق النمط البديل عليها.
  2. اضغط على "التنسيق الشرطي" -> قاعدة جديدة
  3. حدد "استخدام صيغة لتحديد الخلايا التي سيتم تنسيقها" (الإدخال الأخير)
  4. أدخل القاعدة في قيمة التنسيق: =MOD(ROW(),2)=0
  5. اضغط على "تنسيق"، وقم بإجراء التنسيق المطلوب للصفوف البديلة، على سبيل المثال.تعبئة -> اللون.
  6. اضغط موافق، اضغط موافق.

إذا كنت ترغب في تنسيق الأعمدة البديلة بدلاً من ذلك، فاستخدم =MOD(COLUMN(),2)=0

هاهو!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top