Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default How to detect merged cells with C#

Hi,

I do Excel programming first time, and I create a
program to detect merged cells , by this way:

1.Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.Application();

2.Get every cells in the worksheet, and check it is merged cell, or not.

€»There is one worksheet in the Excel only.
but the worksheet has 60 columns and 10000 rows.
and it take long time to finish detecting a worksheet.

Is there any better way to do it?

Best,
Mars

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to detect merged cells with C#

What version of excel are you supporting?

xl2002 (it's there in xl2003 for sure!) added an option to search by format.
Before excel offered that Edit|find option, the only way I know is to loop
through the .usedrange.


Try this manually in excel.

Start a new workbook.
Merge a few cells (mark them nicely with a fill color???).
Then do Edit|find
Click on the Options button if you don't see them.
Click on the Format button
Remove everything that you don't want, but make sure "merge" is selected on the
Alignment tab.

Then you could use Find in your code to look for just merged cells.

If I remember correctly, VBA's .FindNext doesn't remember the format settings,
so you'll have to use successive .Find's.

In excel's VBA, I'd use:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim myMergedCells As Range
Dim FoundCell As Range
Dim FirstAddress As String

Set wks = ActiveSheet

With wks
'clear any existing formatting that was used
Application.FindFormat.Clear

'just the merged cells
With Application.FindFormat
.MergeCells = True
End With

Set FoundCell = .Cells.Find(What:="", _
After:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=True)

If FoundCell Is Nothing Then
'not one unlocked cell!
Else
'keep track of where the first one was, so we can quit
'when we find this again.
FirstAddress = FoundCell.Address

'start building the range of unlocked cells
Set myMergedCells = FoundCell

Do
Set FoundCell = .Cells.Find(What:="", _
After:=FoundCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=True)

If FoundCell Is Nothing Then
Exit Do
End If

If FoundCell.Address = FirstAddress Then
Exit Do
End If

'add to the growing range
Set myMergedCells = Union(myMergedCells, FoundCell)

Loop

End If
End With

If myMergedCells Is Nothing Then
MsgBox "None found!"
Else
Application.Goto myMergedCells ', scroll:=True
MsgBox myMergedCells.Address
End If
End Sub


Mars wrote:

Hi,

I do Excel programming first time, and I create a
program to detect merged cells , by this way:

1.Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.Application();

2.Get every cells in the worksheet, and check it is merged cell, or not.

€»There is one worksheet in the Excel only.
but the worksheet has 60 columns and 10000 rows.
and it take long time to finish detecting a worksheet.

Is there any better way to do it?

Best,
Mars


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with pasting special merged cells to merged cells ritpg[_2_] Excel Programming 3 March 9th 10 07:14 PM
Autofit Merged cell Code is changing the format of my merged cells JB Excel Discussion (Misc queries) 0 August 20th 07 02:12 PM
how do i link merged cells to a merged cell in another worksheet. ibbm Excel Worksheet Functions 3 April 27th 06 11:40 PM
Sorting merged cellsHow do I sort merged cells not identically siz Laval Excel Worksheet Functions 1 November 3rd 04 09:40 PM
VBA: How to detect whether two cells are currently merged Lazer Excel Programming 3 April 16th 04 02:35 AM


All times are GMT +1. The time now is 08:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"