Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-29-2017, 08:51 AM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default Insert Row with Command Button Include Formulas and Merged Cells

I am very new to VBA, and I understand coding. I need some help on a VBA code in Excel 2013.



A portion of my spreadsheet includes rows for quoting items. My end users are not very Excel savvy, so I have researched the code and cobbled together enough to create and use a button that will insert a row in the row number of their choice and include the formulas of the previous row (see below).

Where I'm getting stuck is keeping two sets of cells merged in the new row. Cells D(rowNum) through G(rowNum) should be merged, and H(rowNum) through J(rowNum). I've done lots of research on VBA code to insert a row and keep the merged cells, but I cannot figure out how to integrate it into my existing code. (This is where my lack of basic VBA rules knowledge comes in.)

I would really appreciate any help you can give, and if there's any additional information I need to provide, please let me know.

Thanks!!

Code:
Private Sub CommandButton1_Click()
Dim rowNum As Integer
On Error Resume Next
rowNum = Application.InputBox(Prompt:="Enter Row Number where you want to add a row:", _
Title:="Insert Quote Row", Type:=1)
Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown

Rows(rowNum + 1).Copy

Rows(rowNum).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False


End Sub
Reply With Quote
  #2  
Old 08-29-2017, 10:19 AM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Add a second paste line
Code:
Rows(rowNum).PasteSpecial Paste:=xlFormats
You might also want to add
Code:
If rowNum = 0 Then Exit Sub
right after the InputBox.
Reply With Quote
  #3  
Old 08-29-2017, 10:59 AM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

Thanks for the fast reply, NoSparks!

Using the Paste Special Formats code is actually one of the first things I tried (and researched). What I've learned is that while you can copy and paste formats on the user end of Excel and maintain merged cells, that doesn't seem to work in VBA.

(I did try it again with your exact code just in case I was wrong, and sadly it did not work.)

Thank you for the 'If 0 Then End' code though! I did add that and that does work.

If you have any other suggestions about keeping the merged cells, I'd love to see them and give them a try!

Quote:
Originally Posted by NoSparks View Post
Add a second paste line
Code:
Rows(rowNum).PasteSpecial Paste:=xlFormats
You might also want to add
Code:
If rowNum = 0 Then Exit Sub
right after the InputBox.
Reply With Quote
  #4  
Old 08-29-2017, 11:28 AM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

That worked on what I setup to test with otherwise I wouldn't have suggested it.
It's unlikely I setup every thing the way you have it.
If you attach a sample workbook where this doesn't work and I'll check out what's happening.

If you remove
Code:
On Error Resume Next
does Excel indicate any errors ?
Reply With Quote
  #5  
Old 08-29-2017, 11:33 AM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

My apologies!! You are correct!

I forgot the first rule of working on a project someone else designed: MAKE SURE THEY DID EVERYTHING RIGHT!

The row I was using as my "test case" did not have the cells merged properly.

Problem solved! The Paste Special Formats code works!

And now that I've gotten deeper into user testing, I've come across another challenge. Maybe you can help me with that too???

The insert is copying and pasting the values. Ideally, the new row would be blank OTHER THAN THE FORMULAS (hence the c/p formulas code).

I'm just starting to research this one, so I may just stumble across the answer myself (in between every other fire I have to put out), but there's nothing like picking the brain of someone with experience when opportunity affords.

Thanks again!!!

Quote:
Originally Posted by NoSparks View Post
That worked on what I setup to test with otherwise I wouldn't have suggested it.
It's unlikely I setup every thing the way you have it.
If you attach a sample workbook where this doesn't work and I'll check out what's happening.

If you remove
Code:
On Error Resume Next
does Excel indicate any errors ?
Reply With Quote
  #6  
Old 08-29-2017, 12:00 PM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Glad you got things sorted out.
Reply With Quote
  #7  
Old 08-29-2017, 12:21 PM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

NoSparks, do you mind if I ask for your help one more time on this code?

I researched the issue of clearing the contents of the cells containing values only, and found code that works. The only problem is that my code clears the contents of the new row AND the source row. What change do I make to my code so that it clears the contents of ONLY the new row?

Code:
Private Sub CommandButton1_Click()
     Dim rowNum As Integer
    On Error Resume Next
    rowNum = Application.InputBox(Prompt:="Enter Row Number where you want to add a row:", _
                                    Title:="Insert Quote Row", Type:=1)
    If rowNum = 0 Then Exit Sub
    Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown
    
    Rows(rowNum + 1).Copy
    
    Rows(rowNum).PasteSpecial Paste:=xlPasteFormats
    Cells(rowNum + 1, 1).EntireRow.SpecialCells(xlCellTypeConstants).ClearContents
    Rows(rowNum).PasteSpecial Paste:=xlFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
Reply With Quote
  #8  
Old 08-29-2017, 01:24 PM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

I actually don't know why that would be clearing both rows as it's only being applied to the original row (rowNum+1)
although it is prior to pasting the contents even tho it's already copied.

I'd relocate the instruction and only apply it to the new row.
Code:
Private Sub CommandButton1_Click()
     Dim rowNum As Integer
     
'don't let Excel tell me about errors
'On Error Resume Next

'get rowNum to deal with
rowNum = Application.InputBox(Prompt:="Enter Row Number where you want to add a row:", _
                                Title:="Insert Quote Row", Type:=1)

'incase user closes, cancels or doesn't enter a row number
If rowNum = 0 Then Exit Sub

'prevent screen flash
Application.ScreenUpdating = False

'insert new row
Rows(rowNum).Insert Shift:=xlDown

'copy original row
Rows(rowNum + 1).Copy

'paste formulas of original row
Rows(rowNum).PasteSpecial Paste:=xlFormulas, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=False

'paste formatting of original row
Rows(rowNum).PasteSpecial Paste:=xlPasteFormats

'clear cells with constants in newly inserted row
Cells(rowNum, 1).EntireRow.SpecialCells(xlCellTypeConstants).ClearContents

'stop the marching ants
Application.CutCopyMode = False

'remove selection from entire new row
Range("A" & rowNum).Select

'turn screen updating back on
Application.ScreenUpdating = True

End Sub
Hope that helps.
Reply With Quote
  #9  
Old 08-29-2017, 02:00 PM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

Works like a charm! And much faster than the other solution I had.

I only had to UNCOMMENT the error code.

Thanks again! You're fabulous!

Quote:
Originally Posted by NoSparks View Post
I actually don't know why that would be clearing both rows as it's only being applied to the original row (rowNum+1)
although it is prior to pasting the contents even tho it's already copied.

I'd relocate the instruction and only apply it to the new row.
Code:
Private Sub CommandButton1_Click()
     Dim rowNum As Integer
     
'don't let Excel tell me about errors
'On Error Resume Next

'get rowNum to deal with
rowNum = Application.InputBox(Prompt:="Enter Row Number where you want to add a row:", _
                                Title:="Insert Quote Row", Type:=1)

'incase user closes, cancels or doesn't enter a row number
If rowNum = 0 Then Exit Sub

'prevent screen flash
Application.ScreenUpdating = False

'insert new row
Rows(rowNum).Insert Shift:=xlDown

'copy original row
Rows(rowNum + 1).Copy

'paste formulas of original row
Rows(rowNum).PasteSpecial Paste:=xlFormulas, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=False

'paste formatting of original row
Rows(rowNum).PasteSpecial Paste:=xlPasteFormats

'clear cells with constants in newly inserted row
Cells(rowNum, 1).EntireRow.SpecialCells(xlCellTypeConstants).ClearContents

'stop the marching ants
Application.CutCopyMode = False

'remove selection from entire new row
Range("A" & rowNum).Select

'turn screen updating back on
Application.ScreenUpdating = True

End Sub
Hope that helps.
Reply With Quote
  #10  
Old 08-29-2017, 02:20 PM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Quote:
I only had to UNCOMMENT the error code.
Why? It would be better to fix the error or handle it differently, what is Excel telling you is wrong?
Reply With Quote
  #11  
Old 08-29-2017, 02:22 PM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

Run-time error '1004':
No cells were found.

Highlighting:
Code:
 Cells(rowNum, 1).EntireRow.SpecialCells(xlCellTypeConstants).ClearContents


Quote:
Originally Posted by NoSparks View Post
Why? It would be better to fix the error or handle it differently, what is Excel telling you is wrong?
Reply With Quote
  #12  
Old 08-29-2017, 02:55 PM
NoSparks NoSparks is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

I'm not able to reproduce that error so I guess if nothing is getting screwed up because of it then leave well enough alone.

Good luck.
Reply With Quote
  #13  
Old 08-30-2017, 06:15 AM
TileGal TileGal is offline Insert Row with Command Button Include Formulas and Merged Cells Windows 7 64bit Insert Row with Command Button Include Formulas and Merged Cells Office 2013
Novice
Insert Row with Command Button Include Formulas and Merged Cells
 
Join Date: Jan 2017
Posts: 9
TileGal is on a distinguished road
Default

Thanks again for all your help!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Row with Command Button Include Formulas and Merged Cells Cannot get macro button to sort with merged cells on sheet lonniepoet Excel Programming 3 02-04-2016 04:33 PM
Create a command button that will insert another drop down menu Tinamation Word VBA 1 11-06-2015 03:11 PM
Insert Row with Command Button Include Formulas and Merged Cells Table will not allow sorting because "cells are merged". I can't find the merged cells. wendyloooo Word Tables 1 05-26-2015 01:19 PM
Accidentally merged cells? Fraser Excel 2 02-16-2014 10:15 PM
Insert Row with Command Button Include Formulas and Merged Cells Command Button cksm4 Word VBA 7 02-27-2011 08:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft