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
- StevieGis01Mar 06, 2023Copper ContributorHello again JMB17
I hope all is well with you and your family?
It’s been some time from we spoke last, just letting to know that our code is still working like a dream,
It’s working so we’ll my boss wants me to let it run all our parts but he would like to have barcodes for each product, and to be able to scan a barcode to add or minus a product
Is this doable? Was thinking if I can work out how to create a barcode each +1 and each -1 they could be linked to the VBA buttons would this be true? - JMB17Sep 14, 2020Bronze Contributor
You're welcome. Glad to hear that fixed it.
- StevieGis01Sep 13, 2020Copper ContributorHi JMB17 hope alls well with you and your family.
As always such an easy fix for yourself, that cured it. I feel like a right spanner, I really can’t thank you enough just wish there was a way to repay you for all your time and expertise.
Thanks again
Stevie - JMB17Sep 13, 2020Bronze ContributorSorry for the late response. I saw the notification, but when I clicked on it, there were no new comments. I only saw this when I went to your profile.
If you locate the lines in the code that are applying protection to the various worksheets, you can modify it to allow filtering by adding a comma and AllowFiltering:=True
For example:
Me.Protect Password:=Pword, AllowFiltering:=True
I believe there are 5 places where it is re-protecting worksheets. - StevieGis01Sep 13, 2020Copper ContributorHi JMB17
It’s been a wee while from our last chat, the spreadsheet is working great, but there’s a small issue that I’ve noticed, I’ve auto sort function enabled in the protected/ locked sheets. The auto sort works fine on all the locked sheets until any of our VBA buttons are clicked, after the code runs the auto sort stops working until you manually unlock re-enable and then lock again, but again of any buttons is VBA button is clicked then it stops again. I assume this is fixable, but I can’t work out how to rectify it. Any help would be great, thanks in advance - StevieGis01Sep 11, 2020Copper ContributorHi JMB17
It been a few weeks since we last spoke, our workbook is working well, but I’ve noticed a slight problem. Within each worksheet I have auto filters that work fine when the worksheet is locked but when any of the function buttons are clicked (Vba operates as it should) the worksheets unlock then lock again when code it finished running. My issue is after any vba runs and locks the sheet the auto filters no longer work, I have to unlock the sheet reselect auto sort and the lock the sheet again. - JMB17Aug 25, 2020Bronze Contributor
Yes. I modified the Reorder Parts and Change Inventory macros to also unlock/relock the destination worksheets. You should be able to copy/paste this revised code from the Parts List module into your workbook.
I'm assuming the worksheets all use the same password, which you may have noticed is defined by the constant at the top of the module (PWord). I think it is currently set to vbnullstring, but change that to whatever you need (enclosed in quotes, Const PWord As String = "MyPassword")
Make a copy/backup of your file, just in case.