• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel Table with Buttons to Change Row data

Hi Guys,

Working on this new project and need assistance with my Code or Macro.

Looking for a way to add some VBA buttons that can Insert row(s), Hightlight (selected row RED)
and Edit any Table row.
The Edit option is trickier because I do not want any changes to the DATA (numerical, values, texts..etc)to be made to table unless EDIT option is clicked first.

Please see attachment.







By the way, how is @Hui doing?
 

Attachments

  • AUTOMATED TABLE.xlsm
    35.6 KB · Views: 6
Last edited by a moderator:
Hello again Prinz,

Here's your file, with desired code. Not sure where Hui is...I noticed that I had (finally) caught up to him in post count. Perhaps there is a holiday in the land down under?
 

Attachments

  • FancyTable.xlsm
    43.7 KB · Views: 9
Hi Luke,
That is some exciting stuff you did on there. Thank you!

Is there anyway to tweak the Insert button?
When I click it, it inserts or more like copies and pastes the previous row (whether it is blank or not).

Plus, is there a way to add another button (with code or not) that can show the most recent changes made to a data or sheet?

For instance, if I made changes, and emailed the same sheet to someone else, can they click on the Button, and it highlights the most recent changes?

Please advise.
 
I must have understood your requirement on the Insert. I thought the point was to put the row where the user specified...if not there, where did you want it inserted?


Tracking changes is very difficult and annoying via code. One problem is that you can't tell the difference between original data, a mistake, a correction, and then final data. So, I wouldn't propose that route.

If you have a workbook in shared mode, then you can turn on Track Changes, and use the built-in feature. (no idea why Shared mode is required...seems like a bad idea to me). That would then let XL do the hard work, and you can review the history. But, Shared mode has it's own set of problems, such as not being able to edit macros or change worksheet structures.

Final idea, which I would suggest, is to use multiple copies, and let end-user compare the two sheets/books. There are plenty of ways to compare spreadsheets. Here's one of my favorite:
http://www.exceltip.com/cells-range...-worksheets-using-vba-in-microsoft-excel.html
 
Hi Luke
Thanks for that link.

Your code includes a comment about "inserting a blank row" but not sure how to go about it. Can you review your code again please? I would rather it inserted a blank row.

Also, I was trying to apply the code to a different table but got an 'error 400'. The insert worked, but the Error keps popping up.
Below is where I changed the table name in the code.

tb = ActiveSheet.ListObjects("Table1")

myCell = Intersect(Selection, .ListObjects("Table1").DataBodyRange).Cells(1, 1)

The idea is to add those 3 buttons in 13 workseets from one file.
So I thought changing the range name is all I needed to do, and then add buttons.

Please advise.
 
For first question, here's the complete code you were talking about?
Code:
Sub RowInsert()

Set changeRng = GetRange
With changeRng
    .Copy
    .INSERT shift:=xlShiftDown
    Range("A1").INSERT shift:=xlShiftDown
    .Offset(-1).Locked = True
    'If you want the new row to be blank, uncomment this line
    '.Offset(-1).ClearContents
End With
Application.CutCopyMode = False

Call LockSheet
End Sub
if you want the new row to be blank, need to uncomment the next line with the Clear contents, to make the whole thing look like
Code:
Sub RowInsert()

Set changeRng = GetRange
With changeRng
    .Copy
    .INSERT shift:=xlShiftDown
    Range("A1").INSERT shift:=xlShiftDown
    .Offset(-1).Locked = True
    'If you want the new row to be blank, uncomment this line
    .Offset(-1).ClearContents
End With
Application.CutCopyMode = False

Call LockSheet
End Sub

For your 2nd question, my guess as to why it doesn't work would be to first check the Table name. If that's wrong, it won't work. Beyond that though, we can make things better/more robust with a few changes (this first one is my mistake). Since we already defined tb as our table, we should never have to do the long way. So, it should be:
Code:
Set tb = ActiveSheet.ListObjects("TaskList")
    With ActiveSheet
        'Check if user has selection within table
        On Error Resume Next
        Set myCell = Intersect(Selection, tb.DataBodyRange).Cells(1, 1)

Note how tb is used in the last line?

But, you mention that you want to use this button on multiple sheets. Would be nice if we could just write 1 code, instead of having multiple for all the different names. Assuming that you only have one table, or at least it's the left/top most on on the sheet, we can use the index number rather than the name.
Code:
Set tb = ActiveSheet.ListObjects(1)
    With ActiveSheet
        'Check if user has selection within table
        On Error Resume Next
        Set myCell = Intersect(Selection, tb.DataBodyRange).Cells(1, 1)

Now our code will work no matter what we name the table (at least, assuming there is table in the worksheet. :)
 
Luke,
The Insert worked like Magic in the original doc you sent.
Just tested it on another Sheet..................You ROCK!!!!!

One day, I hope to give back they way Chandoo.org, you and Hui and others have helped me.

I am grateful!!!

Thanks again Luke!
 
Hi Luke,
I am back again!
I applied this code to a corporate report and got some feedback.
Any chance I can make one more request.

On the insert part, can we also add a highlight color feature, say 'light green' like we did with the highlight button?
So, when they click 'insert', a new row is inserted as it does now, but let it also change color to 'lime green' like it does with the highlight button

Would prefer this sheet to become an Unprotected sheet. The protection option seems to confuse the folks I share the reports with.
Instead of 'Editing", would consider "Undo" previous step(s).
 
Hello again. :)

Light green formatting is simple to add, will add a line to the Insert code.
Commented out the 'lock sheet' commands, so sheet does not get protected.

Undo...is tricky from VB stance. You can't "Undo" a macro, so we can't(*) undo an insert. Also, if you try doing:
Application.Undo
from VB, it undoes the last action...but then if you run it again, it undoes the "undoes", and so you just flip back and forth.
For now, I have the button send the keyboard shortcut of Ctrl+z. Not the mode robust idea, but it "works". Let me know what you think.


(*) Not completely true. You can use Application.OnUndo method, and tell VB what to do if Undo is called on this macro...but requires you to write out a specific script, and can get tricky, keeping track of what's in the stack.
 

Attachments

  • FancyTable2.xlsm
    42.7 KB · Views: 5
Last edited:
Hello Luke,

You are right! the Undo option button is too much work.
Can you resend the file , and have it back to "Edit" , so that option hightlights rows yellow please?



Was thinking though,, what are the odds of creating or tweaking a code so that, unlike the "Insert" option that adds and highlights an entire row,
setting up the Table such that if I made ANY changes to any row other than the newly "inserted" one, the cell or row changes color to yellow or pink, or any lighter color.
Are u seeing where I am going with this?

Cheers!
 
Sure thing. Edit button has been reset (although it's function maynot be needed due to next improvement).

If user changes a cell in table data, row is highlighted yellow. Accomplished via a sheet_change event macro.
 

Attachments

  • FancyTable3.xlsm
    45.1 KB · Views: 15
Luke
This is why I love coming on here.
That works like Magic and much easier and user friendly.
You are a Demi-God!!!!!

Thank u again!
 
Hi Luke
Occasionally, the "insert" option does not work.
I get an error message that highlights the Bold line below. Any reason this happens?

Sub RowInsert()
Set changeRng = GetRange
With changeRng
.Copy
.Insert shift:=xlShiftDown
Range("A1").Insert shift:=xlShiftDown .Offset(-1).Locked = True
.Offset(-1).ClearContents
.Offset(-1).Interior.Color = 5296274 'Light green
 
Oi! o_O I have no idea why that line is in there, actually. Perhaps a carry-over from a previous edit I made?? :oops:
You should be able to just delete the entire bolded line. I wasn't able to duplicate error, but my guess would be that if a merged cells, or part of a table, was in col A, XL wouldn't be able to insert a cell (as you can't shift 1 column of a multi-column table).
 
Back
Top