Forum Discussion
Vba code help
- Aug 19, 2020
Cleaning up the code is often a matter of removing "select" and "activate" and joining whatever was selected/activated with the action. Instead of
Range("A1").Select
Selection.Value = 10
it can be shortened to just
Range("A1").Value = 10
Just my two cents, but since these two procedures have a lot of overlap, you could set up a third procedure to do the work and just pass it the value by which to increase/decrease the cell. And, if you had cells other than F8 that this process worked on, you could pass it the applicable cell(s) on the Parts List worksheet.
Or, you could have the user select the cells they want to increment/decrement and have the macro work on whatever range the user has selected?
Public Sub MinusOnePartClickButtonTest() ChangePartCount -1 End Sub Public Sub AddOnePartClickButtonTest() ChangePartCount 1 End Sub Public Sub ChangePartCount(changeValue As Integer) Dim destWkSht As Worksheet Dim destCell As Range Dim timeStampCell As Range On Error GoTo ErrHandler If changeValue < 0 Then Set destWkSht = Sheets("Parts Removed") ElseIf changeValue > 0 Then Set destWkSht = Sheets("Parts Added") Else Exit Sub End If With destWkSht Set destCell = .Range("A" & .Rows.Count).End(xlUp).Offset(1) Set timeStampCell = Intersect(.Range("O:O").EntireColumn, destCell.EntireRow) End With With ThisWorkbook.Worksheets("Parts List") .Unprotect ("Password") ' Unlock Sheet .Range("F8").Value = .Range("F8").Value + changeValue .Range("A8,F8").Copy destCell .Protect ("Password") ' Lock Sheet End With timeStampCell.Value = Now Application.CutCopyMode = False ExitProc: Set destCell = Nothing Set destWkSht = Nothing Exit Sub ErrHandler: MsgBox "Error " & Err.Number & ": " & Err.Description Resume ExitProc End Sub
Thanks again you great
- JMB17Aug 21, 2020Bronze ContributorThe range reference ("A8,F8") will work, but ("A:L,Q,R,S") will not (no worries, we can fix it).
I see that you have increment/decrement buttons in row 5, columns O and P. Are these buttons dedicated to row 5 (i.e. - in your master version, do you have buttons for each row that increment/decrement the current stock level only for the item in that specific row)? I guessing that is the case based on your original post regarding having 4k buttons for 2k parts.
If so, I would agree with Riny that seems pretty overwhelming. Particularly when you consider making updates (adding parts or removing discontinued parts), as each button is hardcoded to a specific row (so, if you insert/delete rows, you have to update the vba code for all of the cells that got moved up/down and delete/add buttons and their respective vba code).
If you are open to suggestions, I would just have 2 buttons (increment/decrement) above the column that change the stock quantity in the same row as the active cell. So, the user selects the row they want to change, then clicks the button, and the stock quantity changes in column Q of that same row. Then you are free to update your table w/o having to update any of your increment/decrement code.- StevieGis01Aug 21, 2020Copper ContributorYes that’s correct, I’ve 2 buttons in every row that control the stock level cell then copy the row to the appropriate sheet ie. -1 button go to parts removed sheet, and +1 button go to parts added. It was a nightmare creating all the buttons, but that’s all I knew at the time.
Your suggestion sounds perfect, but I wouldn’t even know where to start, would it be able when the cell is selected and the correct button clicked will the stock level change up or down depending on which button is pressed. Then after the level changes the row is copied the to appropriate sheet and time stamp the new row in the new sheet also? If all that is possible great, will this code still allow the run parts reorder report to work,
This sound like a lot of work for yourself on my behalf.
Any help any greatly appreciated, it’s nice to be able to speak with Vba guru, my google searches only throw back the same things but described differently.
Thanks again for all you help- JMB17Aug 23, 2020Bronze Contributor
Take a look at the file and see if that is what you have in mind. I added a worksheet with comments to provide some additional explanation of what I did. Hopefully, it makes sense.
Excel has quite a few ways to refer to worksheet ranges, instead of using their cell address, that can help make the vba procedures less susceptible to breaking because a user (or you) moved things around (like cut/paste the table fields because you want them to appear in a different order, or inserting rows above your header row, or deleting some rows/columns - depending on what's deleted).