[英]Excel VBA - Get top cell from column where top cell is merged
I have a macro where I am going through columns and identifying whether the "top" cell (row 7, because there are various irrelevant headers) matches certain specified values, and then performing various actions. 我有一个宏,我在其中遍历各列并确定“顶部”单元格(第7行,因为存在各种不相关的标头)是否与某些指定值匹配,然后执行各种操作。
The problem is that some of the headers are merged cells. 问题在于某些标头是合并的单元格。 This means that the code correctly recognises only columns which align with the left-most column which the header cell straddles. 这意味着代码只能正确识别与标题单元格跨越的最左列对齐的列。 Clearly, I need to change this to resolve this. 显然,我需要对此进行更改以解决此问题。
I don't know how to get this to record a value for, eg, column D and Column E, where a merged cell in both column D and E says "manager" or "director". 我不知道如何获取该值来记录D列和E列的值,其中D列和E列中的合并单元格都显示“经理”或“导演”。
For the moment, I've just included a made-up action ("y = 22") because I'm trying to get the basic principle right before progressing. 目前,我只是添加了一个虚构的动作(“ y = 22”),因为我试图在继续学习之前弄清楚基本原理。
Sub LabourCalc()
Dim x As Variant
Dim y As Variant
Workbooks("XXX").Activate
Sheets("XXX").Activate
For x = 1 To 10
If InStr(Cells(7, x).Value, "MANAGER") _
Or InStr(Cells(7, x).Value, "manager") _
Or InStr(Cells(7, x).Value, "Manager") _
Or InStr(Cells(7, x).Value, "DIRECTOR") _
Or InStr(Cells(7, x).Value, "Director") _
Or InStr(Cells(7, x).Value, "director") Then
y = 22
End If
Next x
End Sub
Use the Range.MergeArea
property. 使用Range.MergeArea
属性。
Returns a Range object that represents the merged range containing the specified cell. 返回一个Range对象,该对象表示包含指定单元格的合并范围。 If the specified cell isn't in a merged range, this property returns the specified cell. 如果指定的单元格不在合并范围内,则此属性返回指定的单元格。
So for example, if D7:E7
is a merged cell: 因此,例如,如果D7:E7
是合并的单元格:
Cells(7, 4).MergeArea.Cells(1, 1)
refers to D7
Cells(7, 4).MergeArea.Cells(1, 1)
引用D7
Cells(7, 5).MergeArea.Cells(1, 1)
also refers to D7
. Cells(7, 5).MergeArea.Cells(1, 1)
也引用D7
。 Cell E7
is empty. 单元格E7
为空。 And if F7
is not a merged cell: 如果F7
不是合并单元格:
Cells(7, 6).MergeArea.Cells(1, 1)
refers to F7
. Cells(7, 6).MergeArea.Cells(1, 1)
引用F7
。 As pointed out by @ValonMiller in the comments, you can simplify the multiple InStr
instances by first converting the contents of Cells(7, x)
to uppercase using UCase
. 正如@ValonMiller在注释中指出的那样,您可以通过首先使用UCase
将Cells(7, x)
的内容转换为大写字母来简化多个InStr
实例。
Your final loop then could look like: 然后,您的最终循环如下所示:
With Workbooks("XXX").Sheets("XXX")
For x = 1 To 10
With .Cells(7, x).MergeArea.Cells(1, 1)
If InStr(UCase(.Value), "MANAGER") Or InStr(UCase(.Value), "DIRECTOR") Then
' Do your stuff here
End If
End With
Next x
End With
All those comments and no one provided an actual working answer. 所有这些评论,没有人提供实际的工作答案。 Try this: 尝试这个:
Sub LabourCalc()
Dim wb as Workbook
Set wb = Workbooks("XXX")
Dim ws as Worksheet
Set ws = wb.Worksheets("XXX")
For x = 1 To 10
Select Case UCase$(ws.Cells(7,x).MergeArea.Cells(1,1))
Case is = "MANAGER","DIRECTOR"
'do stuff here
End Select
Next
End Sub
If you need to check if the Manager or Director appear within the cell do this: 如果您需要检查管理器或控制器是否出现在单元格中,请执行以下操作:
Dim checkValue as String
checkValue = UCase$(ws.Cells(7,x).MergeArea.Cells(1,1))
Select Case Instr(checkValue,"MANAGER") > 0 Or Instr(checkValue,"DIRECTOR") > 0
Case is = True
'do stuff
End Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.