下面是我在excel工作表中搜索注释时编写的代码,注释中也包含特殊字符。任何人都可以帮助我摆脱类型不匹配的错误。下面是我粘贴参考的代码
;with CTE AS
(
SELECT t.*, y.enddate,
datediff(MONTH, AsofDate, enddate)%12 QTRY
FROM dbo.BankInfo t CROSS APPLY dbo.yearenddate y
where y.id = t.id
)
select ID,AsOfDate,Assets, CASE WHEN QTRY=0 THEN 4 WHEN QTRY<0 THEN ABS(QTRY)/3 ELSE 4-QTRY/3 END AS QTR
FROM CTE
答案 0 :(得分:1)
问题是Find()
有255个长度限制
For Each rng In range1.SpecialCells(xlCellTypeVisible)
comment_string = Left(rng.Value, 255) ' <<<<Comment text will be stored up to 255 length
match_Row = rng.Row 'comment row will be stored
With comment
.Activate
Columns("AK:BL").Select
Set RangeObj = Selection.Find(What:=comment_string, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) ' to search for the comment in the comment worksheet
If Not RangeObj Is Nothing Then
If RangeObj.Text = rng.Value Then '<<<< be sure the whole text matches
.Range(RangeObj.Address).Select 'Select the cell of the searched value
Column = ActiveCell.Column 'Get the column number of the searched value
Row = ActiveCell.Row ' Get the row number of the searched value
comments_Column_Name = Split(Cells(, Column).Address, "$")(1) ' Trim the column name from the cell address
Comments_Column_Value = .Range("" & comments_Column_Name & 1) ' Get the comment heading
Comments_ProjCode = .Range("A" & Row) 'Get the project code
With matchcomment
.Activate
.Range("C" & match_Row) = Comments_Column_Value ' Paste the comment heading name in the match sheet
.Range("D" & match_Row) = Comments_ProjCode 'Paste the project code in the match sheet
End With
Else
End If
End If
End With
Next rng
答案 1 :(得分:0)
当您不使用Option Explicit
时,问题就出现了。未声明RangeObj
,因此VBA将其“声明”为Variant
。但是,如果可能,它应至少为Object
和Range
类型的对象。
因此,为了确保代码更进一步,请明确声明RangeObj
:
Dim RangeObj as Range
要确保每个变量都声明为explicilty,请在模块顶部写上Option Explicit
。