Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2005, 11:53 PM
Authorized User
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to merge a set of cells?

Hi,

I am using VB.NET to code for Excel.
(but even if someone can suggest a solution in VB6 i would appreciate)

Following is the problem i am facing:

Starting from cell N5, i have to merge a number of cells in my excel sheet

How many cells have to be merged, can be known only at runtime.

How can i accomplish this merging?

i was using range object, but how to specify the range is where i am stuck.

if i use something like
objRange = DirectCast(objXlSheet.columns("N5:lastCOl"),Excel. Range)

how to get last column (lastCol) ???
calculation wise,It would be (N5 + someNumber calculated at runtime)

Thanks ,
Jell
 
Old August 8th, 2005, 07:56 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

The following code is created in VB6, and you can put it wherever you want.

[code]
Dim xlApp As Excel.Application
Dim xlWrb As Excel.Workbook
'
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.UserControl = True
Set xlWrb = xlApp.Workbooks.Open("C:\yourdirectory\yourfile.xl s")
xlWrb.Sheets("Sheet1").Select 'alter sheet, etc to your own use
endrow = 8
endcol = 17
With xlWrb.Sheets("Sheet1")
    .Range(.Cells(5, 14), .Cells(endrow, endcol)).Select
    Selection.Merge
End With

[\code]

Hope this helps
Max
 
Old August 8th, 2005, 11:09 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here are several options for finding the last column.

Option 1 use special cells command...
    mylastcol = ActiveCell.SpecialCells(xlLastCell).Column
    MsgBox (mylastcol)

Option 2 use reverse column location
    mylastcol = Range("IV1").End(xlToLeft).Column
    MsgBox (mylastcol)


Option 3 use this to locate the last column that is not blank
On Error Resume Next
myFoundLastColumn = False
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.EntireColumn.Select

Do Until myFoundLastColumn = True

    Selection.Find(What:="*", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext) _
        .Activate
    If ActiveCell = False Then
        myFoundLastColumn = False
        ActiveCell.Offset(0, -1).EntireColumn.Select
    Else
        myFoundLastColumn = True
        mylastcol = ActiveCell.Column
        MsgBox (mylastcol)
    End If
Loop

i think you can add the code in like this ...
objRange = DirectCast(objXlSheet.columns("N5:" & lastCOl & "798"),Excel.Range)


cheers

Matt
 
Old August 9th, 2005, 01:15 AM
Authorized User
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Matt,Max
Thanks a lot for your response. Meanwhile I was also able to figure out the way to do it.
With your response my solution got confirmed.
Thanks Again!

Jell






Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge cells and WrapText together Softnut Excel VBA 2 December 13th, 2007 12:27 AM
how to merge cells in MS Flex Grid control manishdev Beginning VB 6 0 June 6th, 2007 09:03 AM
Merge Cells in Grid tiyyob .NET Framework 2.0 1 August 1st, 2006 04:42 AM
Merge and Split Cells in Matrix johnnychow.hike BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 October 25th, 2004 10:17 PM
Merge Cells in MshFlexgrid malavshah11 VB How-To 0 February 11th, 2004 07:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.