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
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
HiJMB17
Thankyou for your very swift reply, I've no doubt that your code is correct and I sound exactly what I was looking for.
However I'm sure I must be doing something wrong because I can't get the code to run on my system.
After reading your reply and looking through your code I'd realised I send you a mixture of 2 different spreadsheet details. I made the real spreadsheet but because I know very little about VBA I made a very small test spreadsheet to play around with so that I wouldn't mess up all my hard work and the cell destinations where from the test spreadsheet with the correct sheet names from the main spreadsheet. So I've included a copy of both the main spreadsheet (with any sensitive details removed)
Also could you advise me on
Within my VBA (Sheet1.RunPartsReorderReport)
1. help me to only clear from row 2 downwards in my Run Reorder Parts Report (is clears the previous report before pasting the next)
2. How to only copy cells (A:L,Q,R,S)
Your code is called (Sheet1.PartsInventoryPartTracker)
It runs about 80/90% of the code then stops at the line:
MsgBox "Error " & Err.Number & ": " & Err.Description
I changed the Value Range From ("F8") To ("Q4:Q250") and the copy Range from("A8,F8") To ("A:L,Q,R,S") I hope this isn't the problem
- JMB17Aug 21, 2020Bronze ContributorI'll take a look at it. But, it may have to wait until this weekend.
- StevieGis01Aug 21, 2020Copper ContributorNo problem, thanks for all your help, I viewed the download I sent you and my -1&+1 buttons don’t work because I had the running when the original workbook was opened, but the codes for them are in the viewer,
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.