VBA 需要代码变量 - 无法分配给此表达式
VBA code variable required - cant assign to this expression
在我的 VBA 代码中,下面的目标是删除任何包含单词 red 的单元格,只要 red 的左右两侧都有空字符串即可。所以“红色”和“深红色”都将被删除。现在我的代码导致编译错误,我不知道如何修复,因为我在 VBA 代码方面没有太多经验。
Sub collapse_columns()
Dim x As Integer
For x = 1 To 4
collapse_column x
Next
End Sub
Sub collapse_column(column_number As Integer)
Dim row As Long
Dim s As Worksheet
Dim last_row As Long
Set s = ActiveSheet ' work on the active sheet
'Set s = Worksheets("Sheet1") 'work on a specific sheet
last_row = ActiveSheet.Cells(s.Rows.Count, column_number).End(xlUp).row
Dim colors_to_delete As String
colors_to_delete = "red"
For row = last_row To 1 Step -1
For Each Color In Split(Cells(row, column_number).Value, " ")
If InStr(1, Cells(row, column_number).Value, colors_to_delete) > 0 Then
Cells(row, column_number).Delete xlUp
Exit For
Next Color
Next row
End Sub
如果我正确理解了您的意图,我相信这可能对您有用。
Sub collapse_columns()
Dim x As Integer
For x = 1 To 4
collapse_column x
Next
End Sub
Sub collapse_column(column_number As Integer)
Dim row As Long
Dim s As Worksheet
Dim last_row As Long
Set s = ActiveSheet ' work on the active sheet
'Set s = Worksheets("Sheet1") 'work on a specific sheet
last_row = s.Cells(s.Rows.Count, column_number).End(xlUp).row
Dim colors_to_delete As String: colors_to_delete = "red"
For row = last_row To 1 Step -1
If InStr(1, " " & s.Cells(row, column_number).Value & " ", " " & colors_to_delete & " ") > 0 Then
s.Cells(row, column_number).Delete xlUp
End If
Next row
End Sub
你得到一个编译错误,因为 Color
实际上是 stdole
库中的常量。
此常量无法赋值,因此出现错误。
局部变量,例如Dim Color As Variant
可以使用,但我不明白 For Each
循环的意义所在,因为您实际上从未在其中使用 Color
。
在我的 VBA 代码中,下面的目标是删除任何包含单词 red 的单元格,只要 red 的左右两侧都有空字符串即可。所以“红色”和“深红色”都将被删除。现在我的代码导致编译错误,我不知道如何修复,因为我在 VBA 代码方面没有太多经验。
Sub collapse_columns()
Dim x As Integer
For x = 1 To 4
collapse_column x
Next
End Sub
Sub collapse_column(column_number As Integer)
Dim row As Long
Dim s As Worksheet
Dim last_row As Long
Set s = ActiveSheet ' work on the active sheet
'Set s = Worksheets("Sheet1") 'work on a specific sheet
last_row = ActiveSheet.Cells(s.Rows.Count, column_number).End(xlUp).row
Dim colors_to_delete As String
colors_to_delete = "red"
For row = last_row To 1 Step -1
For Each Color In Split(Cells(row, column_number).Value, " ")
If InStr(1, Cells(row, column_number).Value, colors_to_delete) > 0 Then
Cells(row, column_number).Delete xlUp
Exit For
Next Color
Next row
End Sub
如果我正确理解了您的意图,我相信这可能对您有用。
Sub collapse_columns()
Dim x As Integer
For x = 1 To 4
collapse_column x
Next
End Sub
Sub collapse_column(column_number As Integer)
Dim row As Long
Dim s As Worksheet
Dim last_row As Long
Set s = ActiveSheet ' work on the active sheet
'Set s = Worksheets("Sheet1") 'work on a specific sheet
last_row = s.Cells(s.Rows.Count, column_number).End(xlUp).row
Dim colors_to_delete As String: colors_to_delete = "red"
For row = last_row To 1 Step -1
If InStr(1, " " & s.Cells(row, column_number).Value & " ", " " & colors_to_delete & " ") > 0 Then
s.Cells(row, column_number).Delete xlUp
End If
Next row
End Sub
你得到一个编译错误,因为 Color
实际上是 stdole
库中的常量。
此常量无法赋值,因此出现错误。
局部变量,例如Dim Color As Variant
可以使用,但我不明白 For Each
循环的意义所在,因为您实际上从未在其中使用 Color
。