Advertisement
Guest User

Untitled

a guest
Apr 15th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. Application.EnableCancelKey = xlInterrupt
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3. Dim tblManual As ListObject
  4. On Error Resume Next
  5. Set tblManual = Me.ListObjects("tbl_manualBought")
  6. On Error GoTo 0
  7. If tblManual Is Nothing Then Exit Sub
  8.  
  9. Dim refreshNeeded As Boolean
  10. refreshNeeded = False
  11.  
  12. ' Check if rows have been added or deleted:
  13. Static lastRowCount As Long
  14. Dim newRowCount As Long
  15. If Not tblManual.DataBodyRange Is Nothing Then
  16. newRowCount = tblManual.DataBodyRange.Rows.Count
  17. Else
  18. newRowCount = 0
  19. End If
  20.  
  21. Dim previousRowCount As Long
  22. previousRowCount = lastRowCount
  23. If lastRowCount = 0 Then
  24. previousRowCount = newRowCount
  25. End If
  26.  
  27. Dim rngIntersect As Range
  28.  
  29. ' Distinguish between row deletion and row addition:
  30. If newRowCount < previousRowCount Then
  31. ' Row(s) deleted – Refresh should occur:
  32. refreshNeeded = True
  33. Set rngIntersect = tblManual.DataBodyRange
  34. ElseIf newRowCount > previousRowCount Then
  35. ' Row added – Do not refresh.
  36. ' Limit the check to the already existing rows:
  37. If Not tblManual.DataBodyRange Is Nothing Then
  38. Set rngIntersect = Application.Intersect(Target, tblManual.DataBodyRange.Resize(previousRowCount))
  39. End If
  40. ' No automatic refresh!
  41. Else
  42. ' Row count unchanged – perform the normal change check:
  43. Set rngIntersect = Application.Intersect(Target, tblManual.DataBodyRange)
  44. End If
  45.  
  46. ' Define the columns that should be checked:
  47. Dim keyCols As Variant
  48. keyCols = Array("Item Name", "Game", "Amount", "Price", "Currency", "RLM / SCM?", "Date")
  49.  
  50. ' Check if the change occurred in a range of the table:
  51. If Not rngIntersect Is Nothing Then
  52. Dim cell As Range, headerCell As Range
  53. Dim tblRowIndex As Long, colIdx As Long, headerName As String
  54.  
  55. ' Loop through all changed cells in tbl_manualBought:
  56. For Each cell In rngIntersect.Cells
  57. tblRowIndex = cell.Row - tblManual.DataBodyRange.Row + 1
  58. colIdx = cell.Column - tblManual.Range.Columns(1).Column + 1
  59. Set headerCell = tblManual.HeaderRowRange.Cells(1, colIdx)
  60. headerName = CStr(headerCell.Value)
  61.  
  62. Dim j As Long, rowComplete As Boolean
  63. rowComplete = False
  64. For j = LBound(keyCols) To UBound(keyCols)
  65. If headerName = keyCols(j) Then
  66. rowComplete = True
  67. Dim colName As Variant, findHeader As Range, checkCell As Range
  68. ' Check all key columns in this row:
  69. For Each colName In keyCols
  70. Set findHeader = tblManual.HeaderRowRange.Find(What:=colName, LookIn:=xlValues, LookAt:=xlWhole)
  71. If findHeader Is Nothing Then
  72. rowComplete = False
  73. Exit For
  74. Else
  75. colIdx = findHeader.Column - tblManual.Range.Columns(1).Column + 1
  76. Set checkCell = tblManual.DataBodyRange.Cells(tblRowIndex, colIdx)
  77. If Len(Trim(CStr(checkCell.Value))) = 0 Then
  78. rowComplete = False
  79. Exit For
  80. End If
  81. End If
  82. Next colName
  83.  
  84. ' If the entire row (in the relevant columns) is filled, then refresh should occur:
  85. If rowComplete Then
  86. refreshNeeded = True
  87. Exit For
  88. End If
  89. End If
  90. Next j
  91. If refreshNeeded Then Exit For
  92. Next cell
  93. End If
  94.  
  95. ' If a refresh is needed, update tbl_CombinedBought:
  96. If refreshNeeded Then
  97. Dim wsCombined As Worksheet
  98. Dim tblCombined As ListObject
  99. Set wsCombined = ThisWorkbook.Worksheets("Bought")
  100. Set tblCombined = wsCombined.ListObjects("tbl_CombinedBought")
  101.  
  102. If Not tblCombined.QueryTable Is Nothing Then
  103. tblCombined.QueryTable.Refresh BackgroundQuery:=False
  104. Else
  105. tblCombined.Refresh
  106. End If
  107. End If
  108.  
  109. ' Update the stored row count for the next run:
  110. lastRowCount = newRowCount
  111. Application.EnableCancelKey = xlDisabled
  112. End Sub
  113.  
  114.  
  115.  
  116. ---
  117.  
  118. Application.EnableCancelKey = xlInterrupt
  119. Option Explicit
  120.  
  121. ' Helper function for refreshing the QueryTable of a table on a specific worksheet.
  122. Private Function RefreshQueryTableInSheet(ws As Worksheet, tblName As String) As Boolean
  123. Dim lo As ListObject
  124. On Error Resume Next
  125. Set lo = ws.ListObjects(tblName)
  126. On Error GoTo 0
  127.  
  128. If lo Is Nothing Then
  129. MsgBox "The table '" & tblName & "' wasn't found in the sheet '" & ws.Name & "'", vbExclamation
  130. RefreshQueryTableInSheet = False
  131. Else
  132. lo.QueryTable.BackgroundQuery = False
  133. lo.QueryTable.Refresh
  134. RefreshQueryTableInSheet = True
  135. End If
  136. End Function
  137.  
  138. ' Helper subroutine for the button macros:
  139. ' Refreshes the table and checks the auto value to optionally call another macro.
  140. Private Sub RefreshButtonTable(ws As Worksheet, tblName As String, autoVarName As String, macroToCall As String)
  141. Dim autoVal As Variant
  142. If RefreshQueryTableInSheet(ws, tblName) Then
  143. autoVal = Evaluate(autoVarName)
  144. If Not IsError(autoVal) Then
  145. If IsNumeric(autoVal) And autoVal = 1 Then
  146. Application.Run macroToCall
  147. End If
  148. End If
  149. End If
  150. End Sub
  151.  
  152. ' -------------------------------
  153. ' Public macros – still callable separately
  154. ' -------------------------------
  155.  
  156. Public Sub RefreshCurrencyConversions()
  157. Dim ws As Worksheet
  158. Set ws = ThisWorkbook.Worksheets("Prebackend")
  159. RefreshQueryTableInSheet ws, "tbl_CurrencyConversion"
  160. End Sub
  161.  
  162. Public Sub RefreshCompletePricing()
  163. Dim ws As Worksheet
  164. Set ws = ThisWorkbook.Worksheets("Prebackend")
  165. RefreshQueryTableInSheet ws, "tbl_CompletePricing"
  166. End Sub
  167.  
  168. Public Sub RefreshCombinedBought()
  169. Dim ws As Worksheet
  170. Set ws = ThisWorkbook.Worksheets("Bought")
  171. RefreshQueryTableInSheet ws, "tbl_CombinedBought"
  172. End Sub
  173.  
  174. Public Sub RefreshCombinedSold()
  175. Dim ws As Worksheet
  176. Set ws = ThisWorkbook.Worksheets("Sold")
  177. RefreshQueryTableInSheet ws, "tbl_CombinedSold"
  178. End Sub
  179.  
  180. Public Sub Refreshbutton_tbl_Buff163SaleImport()
  181. If RefreshQueryTableInSheet(ThisWorkbook.Worksheets("Prebackend"), "tbl_Buff163SaleHistory") Then
  182. RefreshButtonTable ActiveSheet, "tbl_Buff163SaleImport", "var_Buff163SaleAutoImport_numberized", "RefreshCombinedSold"
  183. End If
  184. End Sub
  185.  
  186. Public Sub Refreshbutton_tbl_Buff163PurchasesImport()
  187. If RefreshQueryTableInSheet(ThisWorkbook.Worksheets("Prebackend"), "tbl_Buff163PurchasesHistory") Then
  188. RefreshButtonTable ActiveSheet, "tbl_Buff163PurchasesImport", "var_Buff163PurchasesAutoImport_numberized", "RefreshCombinedBought"
  189. End If
  190. End Sub
  191.  
  192. Public Sub Refreshbutton_tbl_SCMPurchasesImport()
  193. If RefreshQueryTableInSheet(ThisWorkbook.Worksheets("Prebackend"), "tbl_SCMallHistory") Then
  194. RefreshButtonTable ActiveSheet, "tbl_SCMPurchasesImport", "var_SCMPurchasesAutoImport_numberized", "RefreshCombinedBought"
  195. End If
  196. End Sub
  197.  
  198. Public Sub Refreshbutton_tbl_SCMSaleImport()
  199. If RefreshQueryTableInSheet(ThisWorkbook.Worksheets("Prebackend"), "tbl_SCMallHistory") Then
  200. RefreshButtonTable ActiveSheet, "tbl_SCMSaleImport", "var_SCMSaleAutoImport_numberized", "RefreshCombinedSold"
  201. End If
  202. End Sub
  203.  
  204. Public Sub Refreshbutton_tbl_CSFloatPurchasesImport()
  205. RefreshButtonTable ActiveSheet, "tbl_CSFloatPurchasesImport", "var_CSFloatPurchasesAutoImport_numberized", "RefreshCombinedBought"
  206. End Sub
  207.  
  208. Public Sub Refreshbutton_tbl_CSFloatSaleImport()
  209. RefreshButtonTable ActiveSheet, "tbl_CSFloatSaleImport", "var_CSFloatSaleAutoImport_numberized", "RefreshCombinedSold"
  210. End Sub
  211.  
  212. Public Sub Refreshbutton_tbl_CSDealsPurchasesImport()
  213. RefreshButtonTable ActiveSheet, "tbl_CSDealsPurchasesImport", "var_CSDealsPurchasesAutoImport_numberized", "RefreshCombinedBought"
  214. End Sub
  215.  
  216. Public Sub Refreshbutton_tbl_CSDealsSaleImport()
  217. RefreshButtonTable ActiveSheet, "tbl_CSDealsSaleImport", "var_CSDealsSaleAutoImport_numberized", "RefreshCombinedSold"
  218. End Sub
  219.  
  220. Public Sub RefreshCompletePricingAndAgeAndCCYConversions()
  221. Dim ws As Worksheet
  222. Set ws = ThisWorkbook.Worksheets("Prebackend")
  223.  
  224. ' First, refresh the table "tbl_CompletePricing"
  225. If RefreshQueryTableInSheet(ws, "tbl_CompletePricing") Then
  226. ' If the refresh was successful, refresh the QueryTables "pCSROIPricingage", "pGeneralPricingAge", and "tbl_CurrencyConversion"
  227. Call RefreshQueryTableInSheet(ws, "pCSROIPricingage")
  228. Call RefreshQueryTableInSheet(ws, "pGeneralPricingAge")
  229. Call RefreshQueryTableInSheet(ws, "tbl_CurrencyConversion")
  230. End If
  231. Application.EnableCancelKey = xlDisabled
  232. End Sub
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement