我正在尝试将此Excel VBA代码转换为Delphi:

ActiveSheet.Rows(r & ":5000").WrapText = True
ActiveSheet.Rows(r & ":5000").AutoFit


但是在Delphi的Excel2010单元中,_Worksheet.Rows是一个对象,而不是函数或数组对象,我也找不到任何Items属性或类似属性。

uses
  Excel2010;

procedure Test;
var
  Sheet: ExcelWorksheet;
  R: Integer;
begin
  R := 3;
  Sheet.Rows[R.ToString + ':5000'].WrapText := True;
  // Sheet.Rows.WrapText := True;
end;


编译器消息为:

[dcc32 Error] Unit1.pas(110): E2149 Class does not have a default property


VBA代码的正确翻译是什么?

如何在Excel中访问一定范围的行?

最佳答案

使用早期绑定,选择A列(行3..5000)并使用EntireRow,例如:

uses Excel2010;

procedure TForm14.Button1Click(Sender: TObject);
var
  Excel: ExcelApplication;
  Wbook: ExcelWorkbook;
  Sheet: ExcelWorksheet;
begin
  Excel := CoExcelApplication.Create;
  Wbook := Excel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
  Sheet := Wbook.ActiveSheet as ExcelWorksheet;

  Excel.Visible[LOCALE_USER_DEFAULT] := True;

  Sheet.Range['A3','A5000'].EntireRow.WrapText := True;
  Sheet.Range['A3','A5000'].EntireRow.AutoFit;

  //...
end;

10-07 19:31
查看更多