H HHSH Unregistered / Unconfirmed GUEST, unregistred user! 2003-04-16 #1 用delphi操作Excel时怎样实现查找功能? 注意,是在Excel中,就象Locate方法一样,找到某字符串后,得到其单元格的行列值。
宁 宁柯 Unregistered / Unconfirmed GUEST, unregistred user! 2003-04-16 #5 Sub Macro1() Cells.Find(What:="YKing", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , MatchByte:=False, SearchFormat:=False).Activate End Sub 上面是在Excel当中查找YKing这个字符串的宏,如果找到的话,这个Cell就是ActiveCell了 至于怎么知道ActiveCell的行和列我也不知道!
Sub Macro1() Cells.Find(What:="YKing", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , MatchByte:=False, SearchFormat:=False).Activate End Sub 上面是在Excel当中查找YKing这个字符串的宏,如果找到的话,这个Cell就是ActiveCell了 至于怎么知道ActiveCell的行和列我也不知道!
A ai2ming Unregistered / Unconfirmed GUEST, unregistred user! 2003-04-16 #7 看一下我的试验结果,应该可以实现你要的功能.. procedure TForm1.Button1Click(Sender: TObject); var ExcelApp: OleVariant; ExcelBook: OleVariant; ExcelSheet: OleVariant; FindResult: OleVariant; i,j: integer; begin ExcelApp := CreateOleObject('Excel.Application'); ExcelApp.Visible := True; ExcelBook := ExcelApp.WorkBooks.Add; ExcelSheet := ExcelBook.WorkSheets[1]; ExcelSheet.Cells[1,1].Value := 'aaa'; ExcelSheet.Cells[1,2].Value := 'bbb'; ExcelSheet.Cells[1,3].Value := 'ccc'; FindResult := ExcelSheet.Cells.Find('aaa'); if VarIsEmpty(FindResult) then ShowMessage('Not Found!') else begin i := FindResult.Row; j := FindResult.Column; ShowMessage(Format('Row: %d, Column: %d',[i, j])); end; end;
看一下我的试验结果,应该可以实现你要的功能.. procedure TForm1.Button1Click(Sender: TObject); var ExcelApp: OleVariant; ExcelBook: OleVariant; ExcelSheet: OleVariant; FindResult: OleVariant; i,j: integer; begin ExcelApp := CreateOleObject('Excel.Application'); ExcelApp.Visible := True; ExcelBook := ExcelApp.WorkBooks.Add; ExcelSheet := ExcelBook.WorkSheets[1]; ExcelSheet.Cells[1,1].Value := 'aaa'; ExcelSheet.Cells[1,2].Value := 'bbb'; ExcelSheet.Cells[1,3].Value := 'ccc'; FindResult := ExcelSheet.Cells.Find('aaa'); if VarIsEmpty(FindResult) then ShowMessage('Not Found!') else begin i := FindResult.Row; j := FindResult.Column; ShowMessage(Format('Row: %d, Column: %d',[i, j])); end; end;