code_junkie

Examples of Optimization in VB/VBA/VB.net?

Nov 11th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. Dim data As AnyDataType ' right (strongly typed)'
  2. Dim data As Object ' wrong (weakly typed)'
  3. Dim data ' wrong (weakly typed; defaults to the Object)'
  4.  
  5. Dim config As ConfigStruct ' right (when config holds small amount of data)'
  6. Dim config As ConfigClass ' wrong (when config holds small amount of data)'
  7.  
  8. Range("B1").Formula = "Hello there!"
  9.  
  10. Range("B1").Formula = "Hello there!"
  11.  
  12. Private Sub DoStuff()
  13. Dim objMyFoo As New FooClass
  14.  
  15. ' use objMyFoo
  16.  
  17. ' done with objMyFoo
  18. Set objMyFoo = Nothing
  19.  
  20. ' do some more stuff
  21. End Sub
  22.  
  23. For each ctl as Control in Form1.Controls
  24. Dim string1 as String = ctl.ID
  25. Dim string2 as String = ctl.ToolTip
  26. Dim string3 as String = ctl.Text
  27. Dim string4 as String = ctl.BackColor.ToString()
  28. 'do things with the strings
  29. Next
  30.  
  31. Dim string1 as String
  32. Dim string2 as String
  33. Dim string3 as String
  34. Dim string4 as String
  35.  
  36. For each ctl as Control in Form1.Controls
  37. string1 = ctl.ID
  38. string2 = ctl.ToolTip
  39. string3 = ctl.Text
  40. string4 = ctl.BackColor.ToString()
  41. 'do things with the strings
  42. Next
  43.  
  44. For _x as Integer = 0 To 5
  45.  
  46. Dim _y as Integer
  47.  
  48. If _x % 2 = 0 Then
  49. _y = _x
  50. End If
  51.  
  52. Console.WriteLine(_y)
  53. Next
  54.  
  55. Dim _y as Integer
  56.  
  57. For _x as Integer = 0 To 5
  58.  
  59. _y = 0
  60.  
  61. If _x % 2 = 0 Then
  62. _y = _x
  63. End If
  64.  
  65. Console.WriteLine(_y)
  66. Next
  67.  
  68. ' For Buy & Sell Sheets, Fill all ComboBoxes and Clear contents
  69. For Each initsheet In Worksheets
  70. If (Left$(initsheet.Name, 3) = "Buy" Or Left$(initsheet.Name, 4) = "Sell") Then
  71. initsheet.Activate
  72. For Each cCont In ActiveSheet.OLEObjects
  73. ' Cycle through all controls, filling all ComboBoxes
  74. If TypeOf cCont.Object Is MSForms.ComboBox Then
  75. With cCont
  76. Select Case Left$(.Name, 5)
  77. Case "cmbMW"
  78. .ListFillRange = parSheet.Name & "!" & parSheet.Range("transUnit", Sheet2.Range("transUnit").End(xlDown)).Address
  79. .Object.ListIndex = 0
  80. Case "cmbTy"
  81. .ListFillRange = parSheet.Name & "!" & parSheet.Range("transType", Sheet2.Range("transType").End(xlDown)).Address
  82. .Object.ListIndex = 0
  83. Case "cmbCo"
  84. .ListFillRange = parSheet.Name & "!" & parSheet.Range("transCo", Sheet2.Range("transCo").End(xlDown)).Address
  85. .Object.ListIndex = 0
  86. Case "cmbSe"
  87. .Object.List() = ServerArray
  88. End Select
  89. End With
  90. End If
  91. Next cCont
  92. Range("B6:K30").ClearContents
  93. End If
  94. Next initsheet
Add Comment
Please, Sign In to add comment