excel vba - VBA calling a variable by name inside a loop -
ok here's deal: need comprehensive check in .csv file (comparing 1 in current sheet external one). decided divide list 10 equal sections (deciles). in each decile choose random value belonging section , use row number compare 2 sets of data.
where things fall apart inside function. looking way go through each decile (starting rand0) , have vba check whether values of .csv , data sheet in workbook equal. if not - function (called get_param) executed.
i dont quite understand how have vba go through function dec = 0 9 - in essence row number rand0 row number rand9 , perform inequality check (in second if function). rand & dec part not work. looking clues on how fix or on new implementation same thing.
a few more details: n number of rows in .csv file (equal couple of thousand). np number of rows in file (should equal n - if not, execute function). paramlocation designated automatically - should located in specific location.
sub check_changes_param() dim dec integer call public_dims call deciles set parambook = workbooks.open(paramlocation) '==========checks if parameters.xlsm exists in correct location========== if paramlocation = "" msgbox "the parameters.xlsm file not exist or in incorrect location. please ensure located in " & paramlocation else dec = 0 dec = 9 if thisworkbook.sheets("data").cells(rand & dec, 11) <> parambook.sheets("data").cells(rand & dec, 11) or n <> np call get_param exit end if next dec end if end sub public sub deciles() rand0 = int((n / 10) * 1) * rnd + 1 rand1 = int((n / 10) * 2 - (n / 10) * 1 + 1) * rnd + (n / 10) * 1 rand2 = int((n / 10) * 3 - (n / 10) * 2 + 1) * rnd + (n / 10) * 2 rand3 = int((n / 10) * 4 - (n / 10) * 3 + 1) * rnd + (n / 10) * 3 rand4 = int((n / 10) * 5 - (n / 10) * 4 + 1) * rnd + (n / 10) * 4 rand5 = int((n / 10) * 6 - (n / 10) * 5 + 1) * rnd + (n / 10) * 5 rand6 = int((n / 10) * 7 - (n / 10) * 6 + 1) * rnd + (n / 10) * 6 rand7 = int((n / 10) * 8 - (n / 10) * 7 + 1) * rnd + (n / 10) * 7 rand8 = int((n / 10) * 9 - (n / 10) * 8 + 1) * rnd + (n / 10) * 8 rand9 = int(n - (n / 10) * 9 + 1) * rnd + (n / 10) * 9 end sub
try instead:
sub check_changes_param() dim dec integer call public_dims dim deciles variant deciles = decilesarray() set parambook = workbooks.open(paramlocation) '==========checks if parameters.xlsm exists in correct location========== if paramlocation = "" msgbox "the parameters.xlsm file not exist or in incorrect location. please ensure located in " & paramlocation else dec = 0 ubound(deciles) if thisworkbook.sheets("data").cells(deciles(dec), 11) <> parambook.sheets("data").cells(deciles(dec), , 11) or n <> np call get_param exit end if next dec end if end sub public function decilesarray() variant randomize() rand0 = int((n / 10) * 1) * rnd + 1 rand1 = int((n / 10) * 2 - (n / 10) * 1 + 1) * rnd + (n / 10) * 1 rand2 = int((n / 10) * 3 - (n / 10) * 2 + 1) * rnd + (n / 10) * 2 rand3 = int((n / 10) * 4 - (n / 10) * 3 + 1) * rnd + (n / 10) * 3 rand4 = int((n / 10) * 5 - (n / 10) * 4 + 1) * rnd + (n / 10) * 4 rand5 = int((n / 10) * 6 - (n / 10) * 5 + 1) * rnd + (n / 10) * 5 rand6 = int((n / 10) * 7 - (n / 10) * 6 + 1) * rnd + (n / 10) * 6 rand7 = int((n / 10) * 8 - (n / 10) * 7 + 1) * rnd + (n / 10) * 7 rand8 = int((n / 10) * 9 - (n / 10) * 8 + 1) * rnd + (n / 10) * 8 rand9 = int(n - (n / 10) * 9 + 1) * rnd + (n / 10) * 9 decilesarray= array(rand0,rand1,rand2,rand3,rand4,rand5,rand6,rand7,rand8,rand9) end sub
Comments
Post a Comment