今天看啥  ›  专栏  ›  完美Excel

VBA初学者程序示例:匹配多个条件并执行相应操作

完美Excel  · 公众号  · Excel  · 2024-05-19 21:41
学习Excel技术,关注微信公众号:excelperfect标签:VBA这是在vbaexpress.com上看到的一个示例,相对较简单,但是运用了数组及一些VBA语句,初学者可以参考。如下图1所示,在名为“Test_Data”的工作表中,我想查找A列中的“Apple”“Banana”,同时B列中对应为“SS”或“PP”,将满足这两个条件的行设置红色背景。图1打开VBE,插入一个标准模块,输入下面的代码:Sub Set_Patterns() Dim i As Long Dim d As Range Dim Source As Worksheet Dim fruit As Variant Dim rSS As Range Dim rPP As Range  Set Source = ActiveWorkbook.Worksheets("Test_Data")  fruit = Array("Apple", "Banana", "Cherry") For i = LBound(fruit) To UBound(fruit) Set rSS = Nothing Set rPP = Nothing    For Each d In Source.Range("A2:A10").Cells If d = fruit(i) Then Select Case d.Of ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照