Advertisement
TMWNN

NewSale macro

Mar 6th, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub InsertDate()
  2. Dim ts As Date
  3. With Selection
  4. .Value = Date
  5. '.NumberFormat = "yyyy-mm-dd h:mm:ss AM/PM”"
  6. End With
  7. End Sub
  8.  
  9. Sub NewSale()
  10. '
  11. ' NewSale Macro
  12. '
  13. ' Keyboard Shortcut: Ctrl+n
  14.  
  15. ' Disable screen updating. From https://www.reddit.com/r/excel/comments/dqmhl1/two_months_ago_i_knew_nothing_today_this_is/f6aat3h/?context=3
  16. Application.ScreenUpdating = False
  17. Application.EnableEvents = False
  18.  
  19. Dim blnInSalesTable As Boolean
  20.  
  21. OldRow = ActiveCell.Address ' Get current cell's address
  22.  
  23. ' Is cursor in Sales table?
  24. With Selection
  25.     blnInSalesTable = Not Intersect(ActiveCell, [Sales]) Is Nothing
  26. End With
  27.  
  28. Application.Goto reference:="[is.xlsm]IS!Sales[#Totals]" ' Move cursor to Totals row (i.e., just below bottom of table)
  29. Application.CutCopyMode = False
  30.  
  31. ' Insert new row above current.
  32. ' CopyOrigin necessary because My base price/item column wasn't formatted with background fill at table creation
  33. ActiveCell.EntireRow.Insert CopyOrigin
  34.  
  35. If blnInSalesTable Then ' If cursor was in Sales table at macro start,
  36.    NewRow = ActiveCell.Address ' Get current cell's address.
  37.    Range(OldRow).EntireRow.Copy Range(NewRow).EntireRow ' Copy cursor's row over current row
  38. End If
  39.  
  40. ' [Table[ColumnName]].Column] syntax from https://stackoverflow.com/questions/18811431/refer-to-excel-cell-in-table-by-header-name-and-row-number
  41. ActiveSheet.Cells(ActiveCell.Row, [Sales[Date]].Column).Select ' Go to Date column
  42. InsertDate
  43.  
  44. If blnInSalesTable Then ' If cursor was in Sales table at macro start,
  45.    
  46.     ActiveSheet.Cells(ActiveCell.Row, [Sales[Serial '#]].Column).ClearContents ' Blank Serial # column
  47.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Qty]].Column).ClearContents ' Blank Qty column
  48.    
  49.     ActiveSheet.Cells(ActiveCell.Row, [Sales[Sales Tax/VAT]].Column).ClearContents ' Blank Sales Tax/VAT column
  50.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Shipping credit]].Column).ClearContents ' Blank Shipping credit column
  51.    
  52.     ActiveSheet.Cells(ActiveCell.Row, [Sales[Order '#]].Column).ClearContents ' Blank Order # column
  53.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Transaction '#]].Column).ClearContents ' Blank Transaction # column
  54.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Market]].Column).ClearContents ' Blank Market column
  55.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Payment]].Column).ClearContents ' Blank Payment column
  56.    ActiveSheet.Cells(ActiveCell.Row, [Sales[To]].Column).ClearContents ' Blank To column
  57.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Shipping]].Column).ClearContents ' Blank Shipping column
  58.    ActiveSheet.Cells(ActiveCell.Row, [Sales[Combined?]].Column).ClearContents ' Blank Combined? column
  59.    
  60.     CenterOnCell ActiveSheet.Cells(ActiveCell.Row, [Sales[Qty]].Column)  ' center on Qty. Else,
  61. Else
  62.  
  63.     ActiveSheet.Cells(ActiveCell.Row, [Sales[Total fees]].Column).FillDown ' Necessary because column and formula were added after table's creation
  64.  
  65.     CenterOnCell ActiveSheet.Cells(ActiveCell.Row, [Sales[Item]].Column) ' center on Item
  66. End If
  67.  
  68. Application.CutCopyMode = False
  69.  
  70. ' Reenable screen updating
  71. Application.EnableEvents = True
  72. Application.ScreenUpdating = True
  73.  
  74. End Sub
  75.  
  76.  
Tags: excel vba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement