pencilpusher
New Member
- Joined
- Sep 2, 2014
- Messages
- 5
Hi everyone,
I just discovered Macros recently, and have been messing with the macro recorder and meshing it with some internet research, but I have gotten stuck on how to fix the code I highlighted in bold.
The entire code filters a persons name by their account code and will ultimately pull up how much they are costing vs the available budget. If the budget is greater than or equal to 0, then it gets an "OK", the budget at the time of check is hard-coded in (in this case, copy and paste-values'd), then the date of the check is entered.
The problem I have is with the "OK", budget amount, and date section of the code.
If I just want to give an OK if the line passes budget, the code works perfectly. That is, pass budget will paste an "OK", a fail will leave it blank. But, if I also want to add the budget amount and date, weird things start happening. With the code below, a fail (no "OK") will still post the budgeted amount and date of budget check. I've tried in another version to rewrite this as an IF statement (IF cell = OK, then put budget amount and date) but if I do this, the "OK" text string will never show up, even if the line passes budget review.
Does anyone know how the highlighted code should be fixed? As I said at the beginning; if after the filtering, the line passes budget review, then the macro should paste an "OK". Then it should copy the budgeted amount and paste-value the amount into another cell, and finally put the date that this was done.
I just discovered Macros recently, and have been messing with the macro recorder and meshing it with some internet research, but I have gotten stuck on how to fix the code I highlighted in bold.
The entire code filters a persons name by their account code and will ultimately pull up how much they are costing vs the available budget. If the budget is greater than or equal to 0, then it gets an "OK", the budget at the time of check is hard-coded in (in this case, copy and paste-values'd), then the date of the check is entered.
The problem I have is with the "OK", budget amount, and date section of the code.
If I just want to give an OK if the line passes budget, the code works perfectly. That is, pass budget will paste an "OK", a fail will leave it blank. But, if I also want to add the budget amount and date, weird things start happening. With the code below, a fail (no "OK") will still post the budgeted amount and date of budget check. I've tried in another version to rewrite this as an IF statement (IF cell = OK, then put budget amount and date) but if I do this, the "OK" text string will never show up, even if the line passes budget review.
Code:
Sub Macro2()
'
' Macro2 Macro
' Complex Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
'notes which cell I clicked on first - at macro end, will return to this cell, then down 1 cell
x = ActiveCell.Address
' Start at Name, move two cells over; copy contents and filter
Selection.Offset(0, 2).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=8, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
' Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=9, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
'Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=10, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
'Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=11, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
'Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=12, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
'Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=13, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
'Move 1 cell over; copy contents and filter
Selection.Offset(0, 1).Select
Selection.Copy
Range(Cells.Address).AutoFilter Field:=14, Criteria1:= _
ActiveCell.Value, Operator:=xlAnd
Application.CutCopyMode = False
[B]'Check Amount per Filtered Name vs Budget
'Declare "availablebudget" for Long (what is Long?) and "result" as text variable
Dim availablebudget As Long, result As String
'This is cell R471 (471st row, 18th column) which is equal to (budget)-(Filtered Name amount)
availablebudget = Cells(471, 18).Value
'Declare "iRow" as an integer and returns the current row of the active cell
iRow = ActiveCell.Row
'If budget is greater than 0, paste text into cell
If availablebudget >= 0 Then result = "OK"
Cells(iRow, 17).Value = result
'add budgeted amount
'Move 4 cells over right to Budget column, copy it
Selection.Offset(0, 4).Select
Selection.Copy
'Move 1 cell over to right and paste special value
Selection.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'add date
'Declare "iRowDate" as an integer and returns the current row of the active cell
iRowDate = ActiveCell.Row
'Put date in 20th column [cell = (row#, column#)]
Cells(iRowDate, 20).Value = Date[/B]
'clear filters and exit
ActiveSheet.ShowAllData
'Return to start position
Range(x).Select
'move down 1 cell
Selection.Offset(1, 0).Select
End Sub
Does anyone know how the highlighted code should be fixed? As I said at the beginning; if after the filtering, the line passes budget review, then the macro should paste an "OK". Then it should copy the budgeted amount and paste-value the amount into another cell, and finally put the date that this was done.