我的代码找到了某个列,然后转到最后一行并显示它。我的问题是application.match没有在合并的单元格上工作。这是我的代码;
Dim rng As Range
Dim LastRow As Long
With ActiveSheet
Set rng = Sheets("COMPAS").Range("A10:" & .Range("ZZ9").End(xlToRight).Address)
col = Application.Match("*Crt.*", rng, 0)
If IsError(col) Then
Sheets("MACRO TEMPLATE").Cells(2, 2) = 0
Else
col = Application.Match("*Crt. Accrual*", rng, 0)
LastRow = Sheets("COMPAS").Cells(1000000, col).End(xlUp).Row
Sheets("MACRO TEMPLATE").Cells(2, 2) = Sheets("COMPAS").Cells(LastRow, col)
End If
End With
我想要找的是" Crt。累积&#34 ;.不像" Allotment - Total"在第9行和第10行(合并)" Crt。累积"在第10行。没有与第9行合并。
我们将不胜感激。
答案 0 :(得分:1)
您的代码存在一些问题:
此语法不能满足您的期望
Set rng = Sheets("COMPAS").Range("A10:" & .Range("ZZ9").End(xlToRight).Address)
你可以使用这样的东西
Set rng = Sheets("COMPAS").Range("A9:ZZ10")
匹配可以搜索一个数组(一行或一列),因此您必须为每一行使用匹配
" COL"需要声明为变体(不确定如何声明)
每次执行匹配时都需要检查错误(在Else块中也是如此)
注意:
此(未经测试的)代码显示了如何在多行上使用Match:
Option Explicit
Sub SubName()
Const HDR1 As String = "*Crt.*"
Const HDR2 As String = "*Crt. Accrual*"
Dim row1 As Range, row2 As Range
Dim col1 As Variant, col2 As Variant
Dim lastRow As Long, macroCell As Range
With Worksheets("COMPAS").UsedRange
Set row1 = .Rows(9)
Set row2 = .Rows(10)
End With
With Application
col1 = .Match(HDR1, row1, 0)
col2 = .Match(HDR2, row1, 0)
If IsError(col1) Then col1 = .Match(HDR1, row2, 0)
If IsError(col2) Then col2 = .Match(HDR2, row2, 0)
End With
Set macroCell = Worksheets("MACRO TEMPLATE").Cells(2, 2)
If IsError(col1) Or IsError(col2) Then
macroCell.Value2 = 0
Else
With Worksheets("COMPAS")
lastRow = .Cells(.UsedRange.Rows.Count + 1, col2).End(xlUp).Row
macroCell.Value2 = .Cells(lastRow, col2).Value2
End With
End If
End Sub