这真的很简单,但是我是VBA的新手。
如果B列中的单元格不为空,我想用灰色填充和边框来格式化J和K列中的单元格(还没有到达K)。我想在工作簿的每个工作表中执行此操作。
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call Format_ForecastingTemplate(ws)
Next
End Sub
Sub Format_ForecastingTemplate(ws As Worksheet)
Dim cell As Range
Dim N As Long
Dim i As Long
N = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To N
If cell <> "" Then
With ActiveSheet.Range(Cells("J"), cell.Row)
.ThemeColor = xlThemeColorDark1
.BorderAround LineStyle:=xlContinuous
End With
End If
Next
End Sub
给我一个错误的行是If cell <> "" Then
。我认为是因为我没有在B列中引用cell
变量?
错误是:Object variable or With block variable not set
赞:
答案 0 :(得分:1)
我将其更改为单个宏,并更改了原始代码
Sub Format_ForecastingTemplate()
Dim cell As Range
Dim N As Long
Dim i As Long
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
N = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To N
'Looks at B to check if empty
If ws.Cells(i, 2).Value <> "" Then
'changes cells J to color and border
ws.Cells(i, 10).Borders.LineStyle = xlContinuous
ws.Cells(i, 10).Interior.ThemeColor = xlThemeColorDark1
ws.Cells(i, 10).Interior.TintAndShade = -0.25
End If
Next i
Next ws
End Sub
您可以更改列号或为K列添加新行 希望这会有所帮助,请保持友好并留下反馈。 :)