PhysiologyWeb Logo  Search
Share on Facebook Share on Twitter Share on LinkedIn Share on Pinterest Email Copy URL
PhysiologyWeb Loading...

Delphi Code - Merge Rows, Columns, and Cells in Microsoft Excel
Here is a simple Delphi procedure that opens a new Microsoft Excel file (i.e., Excel workbook) and demonstrates how to merge rows, columns, and cells. The Excel Range object can be used to easily refer to a range of rows (e.g., [5:10]), columns (e.g., [B:D]), and cells (e.g., [D15:L30]) to be merged. A few additional range operations are also demonstrated. The file is then saved. The most relevant parts of the code are highlighted. Comments are included to explain the code. If you copy and paste the code into your program, be sure to change the form and procedure names to match your setup. We recommend that you run the code and open and examine the Excel file generated. For additional Excel automation solutions, see Microsoft Excel Automation with Delphi.
procedure TForm1.MergeCellsInMicrosoftExcel(Sender: TObject);
var
ExcelFileName: String;
ExcelApplication, ExcelWorkbook, ExcelWorksheet: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

ExcelFileName := 'C:\PhysiologyWeb\delphi_code_examples\excel_file_merge_cells.xlsx'; //replace file name with the name of your file

ExcelApplication := Null;
ExcelWorkbook := Null;
ExcelWorksheet := Null;

try
//create Excel OLE
ExcelApplication := CreateOleObject('Excel.Application');
except
ExcelApplication := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelApplication) = False then
begin
try
ExcelApplication.Visible := True; //set to False if you do not want to see the activity in the background
ExcelApplication.DisplayAlerts := False; //ensures message dialogs do not interrupt the flow of your automation process. May be helpful to set to True during testing and debugging.

//Open Excel Workbook
try
ExcelWorkbook := ExcelApplication.WorkBooks.Add(-4167);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.workbooks.add
//https://docs.microsoft.com/en-us/office/vba/api/excel.xlsheettype
except
ExcelWorkbook := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorkbook) = False then
begin
//connect to Excel Worksheet using either the ExcelApplication or ExcelWorkbook handle
try
ExcelWorksheet := ExcelWorkbook.WorkSheets[1]; //[1] specifies the first worksheet
//reference
//https://docs.microsoft.com/en-us/office/vba/api/Excel.Worksheets.Item
except
ExcelWorksheet := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorksheet) = False then
begin
ExcelWorksheet.Select;

//add text to A1 cell [row,column]
ExcelWorksheet.Cells[1,1] := 'Microsoft Excel - Merge Cells A1 through L1';
//merge cells A1 through L1
ExcelWorksheet.Range['A1:L1'].Merge;
//set font size to 20
ExcelWorksheet.Range['A1:L1'].Font.Size := 20;
//make font bold
ExcelWorksheet.Range['A1:L1'].Font.Bold := True;
//change font color to blue
ExcelWorksheet.Range['A1:L1'].Font.Color := clBlue;
//set background color to yellow
ExcelWorksheet.Range['A1:L1'].Interior.Color := clYellow;
//align content to center
ExcelWorksheet.Range['A1:L1'].HorizontalAlignment := -4108;

//merge entire rows - merge rows 5 through 10
ExcelWorksheet.Cells[5,1] := 'Merge Rows 5 trough 10';
//merge rows 5 through 10
ExcelWorksheet.Range['5:10'].Merge;
//align content to middle of the merged cells
ExcelWorksheet.Range['5:10'].VerticalAlignment := -4108;

//merge cells D15 through L30
ExcelWorksheet.Cells[15,4] := 'Merge Cells D15 trough L30';
//merge cells D15 through L30
ExcelWorksheet.Range['D15:L30'].Merge;
//horizontal alignment to center
ExcelWorksheet.Range['D15:L30'].HorizontalAlignment := -4108;
//vertical alignment to middle
ExcelWorksheet.Range['D15:L30'].VerticalAlignment := -4108;
//set background color and font size, type, and color
ExcelWorksheet.Range['D15:L30'].Font.Name := 'Lucida Handwriting';
ExcelWorksheet.Range['D15:L30'].Font.Size := 20;
ExcelWorksheet.Range['D15:L30'].Font.Bold := True;
ExcelWorksheet.Range['D15:L30'].Font.Color := clNavy;
ExcelWorksheet.Range['D15:L30'].Interior.Color := clSilver;

//references
//https://learn.microsoft.com/en-us/office/vba/api/excel.range.merge

//select A1 cell
ExcelWorksheet.Cells[1,1].Select; //selects Cell A1

ExcelWorkbook.SaveAs(ExcelFileName);
//Note: If a file with the new name already exists, it overwrites it. Write additional code to address as desired.
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
end;
end;
finally
ExcelApplication.Workbooks.Close;
ExcelApplication.DisplayAlerts := True;
ExcelApplication.Quit;

ExcelWorksheet := Unassigned;
ExcelWorkbook := Unassigned;
ExcelApplication := Unassigned;
end;
end;
end;






Posted: Sunday, December 1, 2024