首页 > 解决方案 > 由于语法错误,Excel VBA 代码无法正常工作

问题描述

我正在使用以下代码来检查单元格是否包含单词“星期六”或“星期日”。如果是,我想在其他一些单元格中添加文本。当我执行代码时 Excel 冻结(我认为它卡在循环中?)。

Sub sbFORMAT()

'delete all shiftdata
Sheets("ploegregeling").Activate

Range("ploeginput").UnMerge
Range("ploeginput").ClearContents
Range("ploeginput").ClearComments


'color weekends blue (= add W in cell, conditional formatting adds blue color + text)

Dim dag As Integer
Dim rij As Integer
Dim rij2 As Integer
Dim rij3 As Integer

dag = 3
rij = 5

Do Until dag = 33

rij2 = rij + 2
rij3 = rij2 + 17

If Cells(dag, rij).Value = "Saturday" Or Cells(dag, rij).Value = "Sunday" Then Range("Cells(dag, rij2), Cells(dag, rij3)").Value = "W"

dag = dag + 1

Loop


End Sub

Excel 工作表看起来像这样

代码应添加W到范围内的每个单元格中G7:G24,H7:H24,N7:N24,等等。通过W在单元格中写入,它将变成蓝色(条件格式)。这是通过手动进行的。

标签: excelvbaif-statement

解决方案


推荐阅读