在我的vba宏中,我有以下代码段,它遍历每一行并找到一个特定的列,如果该行中的值=" UNGRADED",则执行某些操作...
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 'Find the final row.
For x = 1 To FinalRow 'Iterate through the rows
gradedColumn = Cells(x, 14).Value ' Find the value for the row in column N.
If gradedColumn = "UNGRADED" Then
'Do something...
我现在正在尝试修改此段落,以按名称动态搜索列标题,标题为“已分级/未分级”#34;。
Dim gradedColumn As Range
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 'Find the final row.
For x = 1 To FinalRow 'Iterate through the rows
Set gradedColumn = Range( _
Range("A1:S1").Find("Graded / Ungraded").Offset(1), _
Range("A1:S1").Find("Graded / Ungraded").Offset(1).End(xlDown))
'Find the column "Graded / Ungraded".
If gradedColumn = "UNGRADED" Then
'Do something...
此刻,此段落不起作用并收到13类型的不匹配错误。设置gradedColumn变量时,我假设需要将.Value
函数放在某处,但我不确定如何继续。
根据答案更新。我修改了我的代码,现在似乎正在运行。
Set gradedColumn = Range( _
Range("A1:S1").Find("Graded / Un").Offset(1), _
Range("A1:S1").Find("Graded / Un").Offset(1).End(xlDown))
x = 2
For Each mycell In gradedColumn
If mycell = "UNGRADED" Then
Cells(x, 5).Resize(1, 6).Copy
Sheets("SicknessRecordUngraded").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row +1
Cells(NextRow, 1).Select ' Find the next row.
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("SicknessRecord").Select
End If
x = x + 1
Next mycell
答案 0 :(得分:2)
我并不完全清楚你尝试用你的范围做什么,但我认为这段代码足以让你继续,这将为你提供你的GRADED单元格的地址/ UNGRADED标题格式为" $ C $ 1",如果您需要/希望这样做,您可以将其传入Range。
Dim gradedColumn As Range
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 'Find the final row.
For x = 1 To FinalRow 'Iterate through the rows
titleCell = Range("A1:S1").Find("Graded / Ungraded").Address
lastCellInGradedColum = Range("A1:S1").Find("Graded / Ungraded").End(xlDown).Address
' do something
Next x
'alternatively, iterate through the cells in your range like this:
Set gradedColumn = Range( _
Range("A1:S1").Find("Graded / Ungraded").Offset(1), _
Range("A1:S1").Find("Graded / Ungraded").Offset(1).End(xlDown))
For Each mycell In gradedColumn
If mycell = "UNGRADED" Then
'Do something...
End If
Next mycell