|
-
Dec 28th, 2009, 06:19 AM
#1
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
-
Dec 28th, 2009, 06:46 AM
#2
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
-
Dec 28th, 2009, 06:51 AM
#3
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
-
Dec 28th, 2009, 03:19 PM
#4
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
-
Dec 29th, 2009, 01:42 AM
#5
Re: Insert Row in Excel and Merge Cells (VS2005)
That does not work well.
I managed with this one.
vb Code:
Private Sub InsertRow(ByVal sheet As Excel.Worksheet, ByVal rowcount As Integer) Dim range As Excel.Range Dim row As Excel.Range Dim rowindex As Integer = 29 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 sheet.Range(String.Format("G{0}:I{0}", rowindex)).Merge() 'Increase row index rowindex += 1 Next End If End Sub
But I am not sure is this the correct method
Please mark you thread resolved using the Thread Tools as shown
-
Jan 4th, 2010, 12:58 PM
#6
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
-
Jan 4th, 2010, 10:18 PM
#7
Re: Insert Row in Excel and Merge Cells (VS2005)
Hard to write it shorter:
vb Code:
Dim aRange As Excel.Range
Dim r As Integer
Dim N As Integer: N = 12 ' = rowcount - _TRIPREPORTROWS
With sheet
'-- don't know what is your _RowRange & _RowIndex, "B10" is just a sample
r = .Range("B10").Row ' = .Range(_RowRange & _RowIndex).Row
'-- insert N rows above row r
.Rows(r).Resize(N).Insert
'-- the first inserted row now becomes row r
'-- merge columns G:I on each new inserted row
For Each aRange In .Rows(r).Resize(N).Columns("G:I").Rows
aRange.Merge
Next
End With
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|