Hey all,
I'm going to preface this message by stating that I'm a complete newbie with the more advanced features of Excel, inparticular VBA, so I’m well aware that the mistake I made was a spectacularlydumb one, and I apologise in advance for taking up your time with it -hopefullyit’s a relatively quick fix!
Long story short, I’ve been asked to build a report that canbe copy/pasted into an email, and that it needs to be in Excel and in aspecific format consisting of a number of smaller tables each with distinctdata. It doesn't make much sense to me to build it this way, but those are my orders!
The users of the form aren’t the most tech-savvy, so I needit to be as idiot-proof as possible. To that end, I created a button for eachtable that would add a new row to the relevant table and automatically merge therelevant cells so it fits the formatting of the table (yep, merged cells aswell!).
Through google and some trial and error, I managed to hacktogether a bit of code that seemed to do the job – at present it’s got threebuttons, one for each of the tables – I’ll include the code below:
So, for those of you that haven’t already facepalmed hardenough to knock yourselves out, you can probably see where I’ve gone wrong here.The first button works just fine, but because I’ve used direct cell references,if I add anything to any of the earlier tables, it causes the later buttons tocopy the wrong row, breaking the whole thing.
I’m guessing I need to somehow get these cell references tobe relative to a certain point, so it takes into account any rows I’ve alreadyadded. I did think about adding an incremental counter to the sheet and justadding one to it every time you hit a button, but that didn’t pan out – if theythen manually delete a row, it won’t de-increment the counter, not to mentionthe fun and games of trying to work out how I’d avoid the reverse problem ofnot breaking the button for the first table if I added rows to the second.
Can any kind soul help me out of the mess I’ve dug formyself?
I'm going to preface this message by stating that I'm a complete newbie with the more advanced features of Excel, inparticular VBA, so I’m well aware that the mistake I made was a spectacularlydumb one, and I apologise in advance for taking up your time with it -hopefullyit’s a relatively quick fix!
Long story short, I’ve been asked to build a report that canbe copy/pasted into an email, and that it needs to be in Excel and in aspecific format consisting of a number of smaller tables each with distinctdata. It doesn't make much sense to me to build it this way, but those are my orders!
The users of the form aren’t the most tech-savvy, so I needit to be as idiot-proof as possible. To that end, I created a button for eachtable that would add a new row to the relevant table and automatically merge therelevant cells so it fits the formatting of the table (yep, merged cells aswell!).
Through google and some trial and error, I managed to hacktogether a bit of code that seemed to do the job – at present it’s got threebuttons, one for each of the tables – I’ll include the code below:
Code:
[LEFT][COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri][/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]Private Sub CommandButton1_Click()[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Rows("21:21").Insert Shift:=xlDown[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B21", "C21").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D21", "H21").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B22").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B21").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D22").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D21").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]End Sub[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri] [/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]Private Sub CommandButton2_Click()[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]Rows("26:26").Insert Shift:=xlDown[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B26", "C26").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D26", "H26").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B27").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B26").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D27").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D26").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]End Sub[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri] [/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]Private Sub CommandButton3_Click()[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]Rows("32:32").Insert Shift:=xlDown[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B32", "C32").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D32", "E32").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("F32", "G32").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("H32","I32").Merge[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B33").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("B32").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D33").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("D32").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("F33").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("F32").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("H33").Copy[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][COLOR=#222222][FONT=Calibri][SIZE=3][COLOR=#222222][FONT=Calibri][COLOR=#000000][COLOR=#000000][FONT=Calibri] Range("H32").PasteSpecial xlPasteFormats[/FONT][/COLOR][/COLOR][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/FONT][/COLOR][COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][FONT=Calibri][SIZE=3][COLOR=#000000][COLOR=#000000][FONT=Calibri]End Sub[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/FONT][/COLOR][/LEFT]
So, for those of you that haven’t already facepalmed hardenough to knock yourselves out, you can probably see where I’ve gone wrong here.The first button works just fine, but because I’ve used direct cell references,if I add anything to any of the earlier tables, it causes the later buttons tocopy the wrong row, breaking the whole thing.
I’m guessing I need to somehow get these cell references tobe relative to a certain point, so it takes into account any rows I’ve alreadyadded. I did think about adding an incremental counter to the sheet and justadding one to it every time you hit a button, but that didn’t pan out – if theythen manually delete a row, it won’t de-increment the counter, not to mentionthe fun and games of trying to work out how I’d avoid the reverse problem ofnot breaking the button for the first table if I added rows to the second.
Can any kind soul help me out of the mess I’ve dug formyself?