VBA-动态列名-转换为单元格.Value
在我的vba宏中,我有下面一段代码,它遍历每一行并找到一个特定的列,如果该行中的值=“UNGRADED”,请执行某些操作VBA-动态列名-转换为单元格.Value,vba,excel,Vba,Excel,在我的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 col
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...
我现在试图修改这篇文章,通过名称动态搜索一个列标题,标题为“分级/未分级”
目前,这篇文章不起作用,收到一个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
我还不完全清楚你想对你的射程做什么,但我认为这段代码足以让你继续,它将以“$C$1”的形式给出你的分级/未分级标题的单元格地址,如果你需要/希望这样做,你可以将其转入射程
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
Offset
是Range
类型的属性,因此行Set gradedColumn=…
不是13类型不匹配错误的来源。下一行是.ah,感谢用户3964075,Offset“返回一个范围对象,该对象表示从指定范围偏移的范围“。你说得很对,阿莫顿1989试图使用一系列单元格的值,而不是单个单元格的值。我已经编辑了我的答案,删除了那个误导性的陈述,但我认为代码仍然是对他的问题的适当回应。我还向代码中添加了一些代码,以演示如何遍历范围并对其中的每个单元格执行检查。非常好,感谢您澄清了这一点。我已经修改了上面的代码,这为我试图实现的目标提供了一些进一步的见解。你的帖子很有帮助。
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