G'Day、
データを適切に実行するために、ExcelVBAが1つの場所で宣言された定義済みの範囲を効果的に管理する方法について理解を深めるのに役立つ質問があります。このプロジェクトにさらに取り組む前に、どちらの2つのオプション(これまでのところ知っている)が優れているか、または推奨されないベストプラクティスであるかを理解したいだけです。
私が解決している問題は、架空のサプライヤのセット全体で多数の障害を含む小さなテーブルを作成することです。したがって、テーブルは次のようになります(生の形式で申し訳ありません)
「会社名」「故障数」
「クールマシーン」7
「クーラントクォーター」5
「リトルウォータークーラント3
」エアムーバーシステム7
「ジェネラルクーラント」5
「アドミアクーラント」4
私の最初のオプション(Const String)は、次のようなこのモジュール/式です。
Option Explicit
Public Const CountofFailures As String = "J7:J12"
Sub btnRandom()
' Declaration of variables
Dim c As Range
' Provide a random number for failures across Suppliers
For Each c In ActiveSheet.Range(CountofFailures)
c.Value = Random1to10
Next c
End Sub
Function Random1to10() As Integer
'Ensure we have a different value each time we run this macro
Randomize
' Provide a random number from 1 to 10 (Maximum number of Failures)
Random1to10 = Int(Rnd() * 10 + 1)
End Function
2番目のオプション(定義済みの名前)は、次のようなこのモジュール/式です。
Option Explicit
Sub btnRandom()
' Declaration of variables
Dim c As Range
Dim iLoop As Long
' Provide a random number for Suppliers with Defined Range
For Each c In ActiveWorkbook.Names("CountofFailures").RefersToRange
c.Value = Random1to10
Next c
End Sub
Function Random1to10() As Integer
'Ensure we have a different value each time we run this macro
Randomize
' Provide a random number from 1 to 10 (Maximum number of Failures)
Random1to10 = Int(Rnd() * 10 + 1)
End Function
何か提案があります-これが役立つ場合は、後でマクロタイマーテストを行いますか?
セルにリストされている範囲を値としてフェッチする場合、3番目のオプションはありますか?これを実際に行うコードを見たことがありませんか?