我有两个分配给按钮的宏,它们的功能基本相同。他们根据活动单元格(“属性名称”)从另一个工作表(“组合”)中的单元格中提取信息,然后在消息框中显示它们。第二个宏是在以“成熟度”开头的行上给我一个运行时错误5。
Sub PropertyInfo()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim ArrayRange As Variant
Dim ActCell As Variant
Set ActCell = ActiveCell
PropertyName = ActiveCell.Value
If IsEmpty(ActiveCell) = True Or IsNumeric(ActiveCell) = True Then
MsgBox "Please select Property Name"
Else
Sheets("Combined").Select
ArrayRange = Sheets("Combined").Range(Range("F1"), Range("F1").SpecialCells(xlLastCell))
Sheets("Pivot").Select
Maturity = Application.WorksheetFunction.VLookup(ActCell, ArrayRange, 36, False)
Lender = Application.WorksheetFunction.VLookup(ActCell, ArrayRange, 41, False)
Originator = Application.WorksheetFunction.VLookup(ActCell, ArrayRange, 42, False)
Address = Application.WorksheetFunction.VLookup(ActCell, ArrayRange, 2, False)
MsgBox "Property Name: " & PropertyName & vbCrLf & vbCrLf & "Loan Maturity: " & Maturity & vbCrLf & vbCrLf & "Lender: " & Lender & vbCrLf & vbCrLf & "Originator: " & Originator & vbCrLf & vbCrLf & "Property Address: " & Address & vbCrLf & vbCrLf & "Note: If dates are blank, the database doesnt have the info."
Application.ScreenUpdating = True
End If
End Sub
最终结果将显示带有到期日,借出方,发起方和地址的消息框
答案 0 :(得分:1)
在这种情况下,无需使用数组。
尝试这样的方法:
Sub PropertyInfo()
Dim lf2
Dim PropertyName As Variant, m, shtCombined As Worksheet
lf2 = vbLf & vbLf
Set shtCombined = Sheets("Combined")
PropertyName = ActiveCell.Value
If Len(PropertyName) = 0 Or IsNumeric(PropertyName) = True Then
MsgBox "Please select Property Name"
Else
'find the matching row number
m = Application.Match(PropertyName, shtCombined.Range("F:F"), 0)
If Not IsError(m) Then '<< found a match?
With shtCombined.Rows(m)
'###adjust the column numbers below...###
MsgBox "Property Name: " & PropertyName & lf2 & _
"Loan Maturity: " & .Cells(41).Value & lf2 & _
"Lender: " & .Cells(41).Value & lf2 & _
"Originator: " & .Cells(41).Value & lf2 & _
"Property Address: " & .Cells(41).Value & lf2 & _
"Note: If dates are blank, the database doesnt have the info."
End With
Else
MsgBox "No match found for '" & PropertyName & "'!"
End If
End If
End Sub