Results 1 to 7 of 7

Thread: Insert Row in Excel and Merge Cells (VS2005)

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Thumbs up Insert Row in Excel and Merge Cells (VS2005)

    Hi All,
    I am using this code to insert one row and trying to merge cells (G21:I31).

    Code:
    Private Sub InsertRow(ByVal sheet As Excel.Worksheet, ByVal rowcount As Integer)
                Dim range As Excel.Range
                Dim row As Excel.Range
                Dim remark As Excel.Range
                Dim newRow As Excel.Range
                Dim _newrow As Integer = rowcount - _TRIPREPORTROWS
                If rowcount > _TRIPREPORTROWS Then
                    'Select the Last DataRow
                    range = sheet.Range(_RowRange & _RowIndex)
                    row = range.EntireRow
                    For index As Integer = 1 To _newrow 'Insert N new Rows
                        row.Insert(xlShiftDown) 'Insert one Row
                        newRow = sheet.Range(_RowRange & _RowIndex) 'Get the New Row
                        remark = newRow.EntireRow.Range("G32:I32") 'Get the new Row Range
                        remark.Cells.Merge() 'Merge
    
                    Next
                End If
    
            End Sub
    It is merging G54:I54 .. Any ideas ?
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Insert Row in Excel and Merge Cells (VS2005)

    i guess remark is a range relative to newrow range, rather than to sheet range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Insert Row in Excel and Merge Cells (VS2005)

    Is there any method available to merge here it self ?

    Code:
    newRow = sheet.Range(_RowRange & _RowIndex) 'Get the New Row
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Insert Row in Excel and Merge Cells (VS2005)

    try
    newrow.range("g1:i1).mergecells
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Insert Row in Excel and Merge Cells (VS2005)

    That does not work well.
    I managed with this one.

    vb Code:
    1. Private Sub InsertRow(ByVal sheet As Excel.Worksheet, ByVal rowcount As Integer)
    2.             Dim range As Excel.Range
    3.             Dim row As Excel.Range
    4.             Dim rowindex As Integer = 29
    5.             Dim _newrow As Integer = rowcount - _TRIPREPORTROWS
    6.             If rowcount > _TRIPREPORTROWS Then
    7.                 'Select the Last DataRow
    8.                 range = sheet.Range(_RowRange & _RowIndex)
    9.                 row = range.EntireRow
    10.                 For index As Integer = 1 To _newrow 'Insert N new Rows
    11.                     row.Insert(xlShiftDown) 'Insert one Row
    12.                     sheet.Range(String.Format("G{0}:I{0}", rowindex)).Merge()
    13.                     'Increase row index
    14.                     rowindex += 1
    15.                 Next
    16.             End If
    17.  
    18.         End Sub

    But I am not sure is this the correct method
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Insert Row in Excel and Merge Cells (VS2005)

    Hi Dana

    What is the value of _TRIPREPORTROWS, _RowRange and _RowIndex?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Insert Row in Excel and Merge Cells (VS2005)

    Hard to write it shorter:
    vb Code:
    1. Dim aRange As Excel.Range
    2. Dim r  As Integer
    3. Dim N  As Integer: N = 12  ' = rowcount - _TRIPREPORTROWS
    4.  
    5. With sheet
    6.     '-- don't know what is your _RowRange & _RowIndex, "B10" is just a sample
    7.     r = .Range("B10").Row ' = .Range(_RowRange & _RowIndex).Row
    8.     '-- insert N rows above row r
    9.     .Rows(r).Resize(N).Insert
    10.     '-- the first inserted row now becomes row r
    11.     '-- merge columns G:I on each new inserted row
    12.     For Each aRange In .Rows(r).Resize(N).Columns("G:I").Rows
    13.         aRange.Merge
    14.     Next
    15. End With
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width