473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

doubt on calculating difference vba/excel

Can someone give some advices???
Id like to know if someone have a code for visual basic/ excel to calculate the exact difference of two numbers in a extremely huge sample, such as.
Expand|Select|Wrap|Line Numbers
  1. 34    46    61    70    73    78    88    89    90    95    100
  2. 49    51    55    62    63    64    76    82    84    93    97
  3. 47    48    54    55    57    67    74    79    85    91    99
  4. 49    51    55    60    63    64    67    78    83    93    98
  5. 54    59    65    69    71    73    87    92    98    99    100
  6. 42    47    48    58    72    73    74    75    76    80    93
  7. 50    56    59    64    70    73    85    92    94    97    99
  8. 53    57    59    65    74    75    83    84    94    99    100
  9. 30    32    36    60    67    69    70    73    77    98    100
  10. 51    55    60    63    65    79    81    86    87    95    98
  11. 54    58    64    71    74    78    79    83    87    93    94
  12. 37    39    43    49    52    56    63    65    81    88    89
  13. 59    66    68    75    76    79    82    84    88    91    100
  14. 40    43    52    55    59    76    81    83    85    86    97
  15. 29    31    38    41    48    54    65    73    92    94    97
  16. 46    48    49    51    57    59    63    73    78    91    94
  17. 39    43    47    59    61    78    79    92    96    97    98
  18. 69    70    76    78    79    85    91    92    96    97    100
  19. 36    48    53    67    77    82    86    94    98    99    100
  20. 60    61    65    70    71    72    75    84    90    93    97
  21. 54    59    67    71    73    75    77    89    92    97    99
  22. 42    45    49    56    79    84    85    93    94    95    100
  23. 32    37    40    41    53    60    64    77    78    89    99
  24. 46    48    49    50    51    59    66    75    86    94    95
  25. 59    64    73    82    85    88    92    93    94    95    98
  26. 33    42    48    49    51    53    66    78    81    88    92
  27. 56    58    59    61    71    75    82    84    86    91    98
  28. 56    57    60    64    65    67    68    73    85    89    96
  29. 61    68    69    74    78    84    85    86    87    93    96
  30. 40    42    43    49    60    61    71    72    79    97    100
  31. 43    44    48    52    53    64    68    73    79    94    100
  32. 42    49    66    67    76    79    80    83    92    94    95
  33. 39    49    52    56    60    71    76    82    86    90    93
  34. 44    45    51    54    58    61    66    77    79    82    89
  35. 42    47    48    49    53    54    60    67    85    88    99
  36. 57    58    65    69    71    76    78    85    86    88    89
  37. 43    44    50    52    55    73    74    82    97    98    99
  38. 49    53    55    56    57    58    61    66    83    98    99
  39. 33    38    44    52    57    70    72    78    81    84    100
  40. 55    57    62    67    74    77    80    83    86    90    99
  41. 47    50    52    58    64    65    71    77    82    86    98
  42. 48    64    67    73    74    75    80    83    86    89    99
  43. 53    54    58    75    77    84    86    90    93    94    96
  44. 44    52    54    72    77    78    80    81    87    99    100
  45. 42    57    67    68    69    70    74    80    86    90    100
  46. 41    45    50    63    78    81    93    94    97    98    100
  47. 50    70    75    78    80    82    89    92    95    96    97
  48. 37    45    47    56    64    68    70    74    88    98    100
  49. 61    62    79    80    81    83    85    86    88    95    97
  50. 39    40    42    43    54    66    68    71    73    82    100
  51. 47    51    53    57    60    63    75    82    83    89    98
  52. 50    53    65    69    73    80    81    85    87    89    99
  53. 39    41    42    49    50    56    59    62    82    86    95
  54. 45    53    56    74    86    87    88    91    94    97    98
  55. 50    54    63    64    67    73    74    89    96    97    99
  56. 36    47    56    60    63    74    80    82    88    91    92
  57. 64    67    73    78    79    83    85    93    95    97    98
  58. 51    56    61    66    74    78    80    85    90    92    95
  59. 53    56    63    76    81    83    89    91    92    94    96
  60. 42    43    45    47    60    67    68    76    87    88    93
  61. 53    55    60    61    64    68    70    74    84    88    100
  62. 32    33    36    41    43    44    49    61    62    66    81
  63. 52    54    57    65    69    74    78    82    91    92    99
  64. 38    47    51    54    66    68    69    84    85    93    98
  65. 31    39    45    58    69    72    79    83    86    92    98
  66. 44    54    57    62    66    76    78    88    92    94    99
  67. 49    50    62    64    75    76    85    89    91    94    98
  68. 32    45    46    53    54    57    71    78    82    88    94
  69. 58    69    72    73    76    83    86    94    95    98    100
  70. 46    47    66    80    81    83    89    90    91    94    100
  71. 53    55    59    61    63    66    69    73    81    90    94
  72. 45    47    52    64    68    70    74    87    90    93    100
  73. 71    73    74    81    83    91    92    93    94    98    99
  74. 46    48    49    53    57    61    71    87    92    96    99
  75. 57    59    64    73    76    78    85    87    94    95    98
  76. 50    52    55    56    61    67    68    80    86    96    100
  77. 41    49    56    76    80    83    85    89    91    98    100
  78. 53    63    67    71    72    81    83    84    86    87    94
  79. 37    38    43    47    51    55    67    77    85    86    87
  80. 50    55    77    81    86    88    91    93    95    98    100
  81. 63    65    67    68    74    83    86    87    92    94    97
  82. 52    53    59    62    64    69    79    85    88    90    93
  83. 50    51    61    65    71    74    76    79    83    85    100
  84. 25    42    45    50    55    57    65    68    81    88    95
  85. 36    44    45    47    51    58    60    63    71    84    92
  86. 47    51    59    60    61    64    71    87    96    98    99
  87. 53    54    59    67    83    85    87    93    96    98    99
  88. 47    51    57    60    66    76    77    84    86    94    95
  89. 56    59    60    63    64    67    68    76    83    86    88
  90. 52    55    57    62    66    73    80    88    92    93    94
  91. 35    36    55    58    64    66    67    81    85    91    100
  92. 55    58    60    61    62    63    70    86    89    90    92
  93. 52    53    54    69    74    77    81    82    84    90    94
  94. 47    54    59    60    67    72    83    84    88    90    94
  95. 55    57    69    72    79    84    85    86    87    91    94
  96. 42    51    62    64    65    70    73    75    86    94    95
  97. 44    45    49    50    51    55    57    63    85    92    97
  98. 55    63    64    68    70    72    73    76    84    89    96
  99. 38    39    42    53    62    69    82    89    90    92    97
  100. 48    51    56    58    60    66    67    77    90    91    95
  101. 30    33    39    49    51    59    68    74    80    82    83
  102. 57    62    64    70    72    76    85    89    91    94    95
  103. 58    61    72    77    85    86    91    92    95    97    99
  104. 41    44    49    50    53    64    65    68    70    71    85
  105. 28    34    41    48    54    72    76    86    87    93    100
  106. 40    55    68    72    75    77    78    82    87    88    97
  107. 38    40    45    53    54    61    62    64    69    75    91
  108. 44    48    53    59    61    62    67    85    86    90    97
  109. 49    50    65    77    79    80    89    92    94    98    100
  110. 53    61    62    63    65    66    67    72    75    81    92
  111. 58    59    61    71    72    79    82    86    88    96    98
  112. 58    60    61    63    67    74    78    88    94    97    100
  113. 37    38    41    52    71    81    83    84    87    88    91
  114. 44    48    49    50    71    74    76    80    88    90    91
  115. 32    38    39    40    47    56    63    72    84    88    92
  116. 45    46    47    53    56    59    64    80    82    83    92
  117. 45    46    48    64    65    66    67    73    74    99    100
  118. 41    53    56    60    63    65    74    82    83    90    92
  119. 45    50    52    55    64    69    73    79    81    82    87
  120. 31    35    47    48    56    61    74    75    77    84    94
  121. 59    69    70    71    73    75    82    89    91    99    100
  122. 36    43    58    59    77    82    84    85    86    88    95
  123. 54    58    60    64    65    66    69    76    81    85    90
  124. 40    41    59    64    67    69    70    76    81    84    95
  125. 62    66    68    69    71    77    82    86    92    94    100
  126. 37    38    40    41    46    56    63    68    83    96    97
  127. 45    48    54    59    62    65    67    71    78    79    86
  128. 47    49    52    57    58    59    74    84    86    88    97
  129. 41    43    44    47    60    65    76    78    80    84    90
  130. 38    41    44    49    51    52    76    84    85    94    99
  131. 47    48    55    62    69    70    75    80    90    94    98
  132. 42    45    49    51    58    71    74    76    79    86    96
  133. 52    53    57    62    66    71    75    79    86    92    95
  134. 38    47    48    55    63    65    67    71    73    85    99
  135. 54    58    59    60    68    78    80    84    86    95    97
  136. 47    50    53    54    55    59    71    80    87    89    91
  137. 54    55    64    65    69    74    80    81    82    87    91
  138. 48    52    53    54    58    69    77    79    81    82    88
  139. 43    49    51    55    65    66    71    75    85    89    93
  140. 50    51    56    57    60    62    65    78    83    92    95
  141. 51    56    66    79    84    87    89    90    92    93    99
  142. 51    54    55    60    63    68    82    89    94    96    98
  143. 48    53    54    58    69    76    78    80    81    82    83
  144. 51    59    60    62    66    74    81    85    87    95    99
  145. 43    45    46    50    57    64    70    76    79    85    99
  146. 57    59    60    67    73    75    87    88    92    93    96
  147. 40    48    56    59    65    73    81    82    85    88    98
  148. 43    45    49    60    77    81    84    85    91    98    100
  149. 49    50    55    58    63    66    70    82    85    90    93
  150. 37    38    39    48    50    52    54    55    79    89    96
  151. 48    50    51    56    73    78    81    84    85    90    94
  152. 51    52    60    66    73    75    81    83    86    87    95
  153. 50    51    63    64    67    69    71    75    76    79    97
  154. 51    55    62    65    66    73    79    81    88    92    98
  155. 32    33    34    37    38    42    47    50    70    72    93
  156. 48    50    59    66    68    77    82    85    91    95    98
  157. 54    57    59    62    63    66    81    84    86    88    90
  158. 55    56    62    63    66    70    72    86    89    93    98
  159. 55    61    65    67    68    73    74    75    79    86    96
  160. 48    56    62    65    66    68    83    89    92    94    100
  161. 55    58    60    61    69    73    80    82    83    92    98
  162. 61    62    70    74    80    81    83    86    91    95    98
  163. 33    47    52    53    59    69    78    81    83    85    96
  164. 59    63    64    69    73    77    80    81    84    87    96
  165. 46    49    60    61    65    67    71    76    82    89    92
  166. 38    62    70    72    76    77    81    87    89    92    94
  167. 52    57    59    63    65    76    82    86    95    96    99
  168. 40    46    53    55    57    69    77    79    83    89    100
  169. 54    55    59    65    68    75    78    91    94    95    96
  170. 56    61    65    66    69    73    77    89    97    98    100
  171. 57    60    62    69    71    76    81    84    86    91    92
  172. 46    52    53    55    56    57    67    71    73    91    95
  173. 32    43    44    47    51    52    57    63    73    84    88
  174. 41    58    63    68    69    72    76    83    91    92    100
  175. 57    63    64    75    77    82    83    84    91    92    98
  176. 50    53    55    65    72    75    76    82    89    91    100
  177. 39    42    43    54    71    77    80    83    88    89    98
  178. 42    44    48    51    59    85    91    94    97    98    100
  179. 48    50    52    58    61    64    65    73    83    84    99
  180. 51    54    55    56    64    71    72    74    76    77    95
  181. 41    49    50    63    71    72    78    82    93    95    97
  182. 44    55    61    62    65    67    69    84    85    93    100
  183. 54    59    62    64    65    73    74    79    84    96    97
  184. 50    58    72    86    88    92    93    94    95    96    100
  185. 56    58    60    66    72    73    78    81    83    94    98
  186. 38    40    62    64    68    69    88    90    94    96    97
  187. 42    44    55    58    76    79    80    83    84    88    93
  188. 57    62    64    71    78    86    90    92    94    95    100
  189. 47    48    49    51    52    74    77    79    81    83    86
  190. 46    49    51    53    81    83    84    87    88    90    100
  191. 36    47    50    53    68    69    75    79    80    88    90
  192. 54    56    61    65    68    69    75    77    80    86    88
  193. 49    54    59    61    66    70    74    86    95    96    97
  194. 46    48    62    63    65    72    76    77    85    87    93
  195. 45    48    49    51    52    53    54    61    66    81    92
  196. 34    35    38    48    51    58    66    78    84    93    97
  197. 38    42    52    54    57    58    61    90    91    92    99
  198. 59    64    65    66    68    76    77    78    85    92    93
  199. 38    43    47    68    70    80    83    85    89    91    99
  200. 56    58    66    74    76    79    82    84    90    93    97
  201. 61    62    64    74    75    82    83    87    88    89    96
  202. 60    61    62    64    70    73    80    81    82    85    96
  203. 67    68    71    72    74    83    84    88    90    91    99
  204. 46    49    50    55    59    62    67    70    88    89    96
  205. 57    64    71    74    75    76    80    81    85    86    99
  206. 44    49    58    59    62    65    67    76    80    89    99
  207. 39    46    64    65    70    71    74    77    80    81    90
  208. 44    59    60    66    68    69    77    87    90    95    96
  209. 53    55    57    64    76    77    85    88    90    94    97
  210. 57    60    66    70    76    77    79    83    84    86    98
  211. 44    46    47    50    59    60    61    64    70    87    97
  212. 40    43    44    46    47    54    61    80    89    96    99
  213. 44    47    54    55    56    57    67    71    72    78    82
  214. 50    54    56    65    66    70    74    75    90    96    99
  215. 56    62    64    65    67    69    73    74    85    92    98
  216. 42    44    47    52    53    61    62    64    77    82    99
  217. 59    60    72    74    75    76    77    82    95    96    99
  218. 60    61    65    68    70    72    78    79    80    87    95
  219. 42    58    65    72    75    76    77    78    83    91    94
  220. 45    48    49    56    62    69    72    73    74    81    87
  221. 46    48    49    51    55    66    78    84    91    94    98
  222. 50    53    62    65    75    79    89    92    96    97    99
  223. 48    52    58    72    75    83    92    94    95    97    99
  224. 33    41    43    45    55    64    65    67    74    81    90
  225. 36    41    44    49    54    60    64    65    66    78    99
  226. 29    32    33    42    46    49    52    64    68    87    92
  227. 45    58    61    68    69    71    74    78    85    94    97
  228. 56    60    62    64    67    68    71    86    93    94    96
  229. 64    66    75    78    81    84    85    91    92    94    98
  230. 62    63    64    65    74    79    81    83    85    87    91
  231. 34    38    41    48    51    53    69    70    80    87    100
  232. 33    41    49    53    59    63    68    69    73    87    99
  233. 56    57    64    70    72    78    79    94    97    98    99
  234. 61    66    67    73    78    85    87    90    91    92    96
  235. 50    67    70    77    78    82    83    87    92    95    96
  236. 33    36    47    48    49    60    63    68    78    81    91
  237. 32    44    45    50    51    58    62    83    84    92    93
  238. 48    64    67    69    75    83    84    87    90    91    95
  239. 54    55    58    70    74    84    85    90    97    98    99
  240. 51    60    71    72    73    76    77    84    87    88    89
  241. 35    39    54    55    56    58    73    83    87    88    98
  242. 33    35    45    50    53    55    65    70    76    87    97
  243. 66    68    72    76    77    78    86    90    92    94    96
  244. 49    54    55    60    70    82    87    91    95    98    100
  245. 42    43    48    55    58    68    69    75    76    93    95
  246. 43    44    53    55    71    73    74    78    84    90    97
  247. 44    53    54    62    65    82    86    90    93    96    100
  248. 42    44    48    52    66    69    74    81    82    86    89
  249. 48    53    56    58    60    71    78    80    88    89    94
  250. 57    59    64    65    67    85    91    92    95    97    99
  251. 53    62    65    66    69    72    76    85    94    98    100
  252. 38    47    53    57    61    72    73    74    85    87    88
  253. 24    48    52    54    62    66    72    76    77    95    99
  254. 43    49    50    51    60    66    70    74    77    78    80
  255. 65    66    68    70    71    72    73    75    77    97    100
  256. 39    48    54    58    65    73    77    90    92    98    99
  257. 62    66    67    68    74    76    86    88    89    91    93
  258. 58    59    63    64    73    77    78    79    80    87    94
  259. 54    60    63    66    67    68    72    75    82    84    100
  260. 50    51    56    58    61    62    67    68    90    92    94
  261. 44    47    50    54    61    64    80    92    93    95    100
  262. 53    54    61    64    69    84    92    94    97    98    99
  263. 53    57    59    78    79    83    85    86    93    96    100
  264. 52    53    57    58    59    63    68    73    76    77    81
  265. 53    58    60    69    74    76    85    86    88    90    95
  266. 48    62    69    74    76    78    80    83    93    97    99
  267. 50    51    55    57    61    69    71    72    85    89    96
  268. 38    40    43    54    60    63    64    72    73    85    91
  269. 40    48    54    56    57    59    67    69    83    91    95
  270. 39    43    46    55    58    61    69    71    95    97    100
  271.  
  272. 16 11 88 32 25 0 70 78 73 61 90 89 46 95 6 33 34 21 14 22
  273. 76 63 18 44 84 11 55 93 8 29 82 62 17 64 24 14 37 97 49 51
  274. 34 8 10 11 99 28 67 20 55 74 45 48 85 91 79 57 47 22 17 54
  275. 14 67 12 55 25 60 51 30 41 35 78 83 93 64 98 39 44 49 5 63
for example, I'd like to know:
+ Which are, and how many are, pairs of numbers with the precise difference of 19 within each row in the above table.
+ I am using Excel-2010

Is it possible to do it in Visual Basic in MS EXCEL 2010? How can it be done?


I'm looking forward to receiving good news.

Thanks in advance
Nov 26 '15 #1
23 3691
I have no experience working with VB in Excel, but this VB6 code snippet may give you a start.
This assumes the data is in a two-dimensional grid.

Expand|Select|Wrap|Line Numbers
  1. 'set numbers here to suit
  2.  
  3. dim maxrow as integer   'for rows
  4. dim maxcol as integer   'for column
  5.  
  6. 'example maximum figures
  7. maxrow = 100: maxcol = 40
  8.  
  9. dim row(maxrow) as integer                  'row
  10. dim col(maxcol) as integer                  'column
  11. dim listnumber(maxrow, maxcol) as integer   'data
  12.  
  13. dim a as integer
  14. dim b as integer
  15. dim n as integer
  16.  
  17. for a = 1 to maxrow          'work each row
  18.     for b = 1 to maxcol-1      '
  19.         n = b
  20.         While n<= maxcol     'compare each figure in row against all others
  21.             n = n + 1
  22.             if abs(listnumber(a,n)-listnumber(a,b))=19 then  'use abs in case of -ve number
  23.                 print "Row: ";a; "  Columns:";b; "  and ";n
  24.             endif
  25.         wend
  26.     next b
  27. next a
Nov 26 '15 #2
yeah u got the idea into mind: exact difference of two numbers or ABS is 19. i saw that u wrote it in the code. I TRIED your code in MS EXCEL 2010, but it returned no results, because it displayed error messages: error compilations in the code.
I got almost-nothing skills in programming, I recently have begun study vba 4/5/6 windowns a month ago.

Would you fix the code up, please?

many thanks
Nov 27 '15 #3
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

Your thread has been moved to the excel forum.

You should be aware that this is not a code writing service. We will help you fix code but most of us do not have the time to write it all for you. We expect you to put forth some effort.
Nov 27 '15 #4
thks for your help.

Would you fix the code up, please?

many thanks in advance
Nov 28 '15 #5
Rabbit
12,516 Expert Mod 8TB
You haven't posted any code
Nov 28 '15 #6
Sandradepar - As stated at the top of my reply, I have no experience of VB in Excel. As far as I'm concerned, I tried to help in writing the formulae to scan the data and output the results to give you a start, but I'm not contracted to you and it's up to you to work out how to reference the data.
Nov 28 '15 #7
Sandradepar,
I don't know if you've got any further, but I felt I might offer you a clue. It isn't difficult stuff.

I don't do Excel VBA, but a 5-minute google will show that you can reference the cells quite easily so that line 22 might read:-

Expand|Select|Wrap|Line Numbers
  1. 22.                 if abs(Worksheets("Sheet1").Cells(a,n) - Worksheets("Sheet1").Cells(a,b))=19 then    'use abs in case of -ve number
Dec 7 '15 #8
zmbd
5,501 Expert Mod 4TB
codegazer,
Thank you for your efforts on this... the ball is in SANDRADEPAR's court to make an effort to apply your solution(s) to the situation and show their effort here for more guidance.

We're not a code writing service.
Dec 8 '15 #9
i tried this code but i got limited results (the code omitting some results i dont know how to fix this:

Expand|Select|Wrap|Line Numbers
  1. Sub difference()
  2.  
  3. Dim lngLastRow As Long
  4. Dim lngLastColumn As Long
  5. Dim lngMatch As Long
  6.  
  7. Dim arrMatch(100) As Variant
  8. Dim arrMatchValues(100) As Variant
  9. Dim arrMatchValues2(100) As Variant
  10.  
  11. Const valDif = 19
  12.  
  13. lngMatch = 0
  14.  
  15. Sheets("Plan1").Select
  16.  
  17. lngLastRow = Sheets("Plan1").Range("A1").End(xlDown).Row
  18. lngLastColumn = Sheets("Plan1").Range("A1").End(xlToRight).Column
  19.  
  20.  
  21. For i = 1 To lngLastRow
  22.     For j = 1 To lngLastColumn
  23.             If Cells(i, j + 1).Value - Cells(i, j).Value = valDif Then
  24.  
  25.                 arrMatch(lngMatch) = Cells(i, j).Address
  26.                 arrMatchValues(lngMatch) = Cells(i, j + 1).Value
  27.                 arrMatchValues2(lngMatch) = Cells(i, j).Value
  28.  
  29.                 lngMatch = lngMatch + 1
  30.  
  31.             End If
  32.     Next j
  33. Next i
  34.  
  35. Sheets("Plan2").Select
  36.  
  37. Cells(1, 1).Value = lngMatch & " records found"
  38.  
  39. For i = 0 To lngMatch
  40.  
  41.     Cells(i + 1, 3).Value = arrMatch(i)
  42.     Cells(i + 1, 5).Value = arrMatchValues(i)
  43.     Cells(i + 1, 6).Value = arrMatchValues2(i)
  44.  
  45. Next i
  46.  
  47. End Sub
  48.  
  49.  
try to aplly it in a small sample :

Expand|Select|Wrap|Line Numbers
  1. 54    60    63    66    67    68    72    75    82    84    100
  2. 50    51    56    58    61    62    67    68    90    92    94
  3. 44    47    50    54    61    64    80    92    93    95    100
  4. 53    54    61    64    69    84    92    94    97    98    99
  5. 53    57    59    78    79    83    85    86    93    96    100
  6. 52    53    57    58    59    63    68    73    76    77    81
  7. 53    58    60    69    72    79    85    86    88    91    98
  8. 48    62    69    74    76    78    80    83    93    97    99
  9. 50    51    55    57    61    69    71    72    85    89    96
  10. 38    40    43    59    60    63    64    72    73    85    93
  11. 40    48    54    56    57    59    67    69    73    86    92
  12. 39    43    46    55    58    62  74    82    95    97    100
the code is omitting some results in some lines (it displays only 1 pair per line/row, and no more results) i dont know how to fix that.

Also I have absolutely no experience of VB in Excel.
can someone fix the code up, please?
Does anyone have a better practical idea that solve the problem( in the 1st post on top of the page) more easily ??? Any help is so welcome.

many thanks in advance
Dec 10 '15 #10
zmbd
5,501 Expert Mod 4TB
Let us go back to the dataset in your first-post

Expand|Select|Wrap|Line Numbers
  1. Just row one....
  2. 34    46    61    70    73    78    88    89    90    95    100
Option 1:
|A-B|=|C| such that:
|34-46|=|12|; |34-61|=|27|; |34-70|=|27| etc...
|46-61|=|15|; |46-70|=|24|; |46-73|=|27| etc...
and so forth thru all of the possible combinations of those numbers with only the given row,

OR

Option 2:
A-B, B-C, C-D so that:
|34-46|; |46-61|; |61-70|; etc...

IN either option 1 or 2, returning only those numbers where their absolute difference is 19 within the given row.

Which option is correct?
Dec 10 '15 #11
friend, thanks for your help .
yeah that's the main idea : to find the exact difference of two numbers or their absolute value |A-B|=|C| , that should be 19.

the code that i posted previously is omitting some results in some lines (it displays only 1 pair per line/row, and no more results) i dont know how to fix that.
i tried valDif in the code such as A-B, B-C, C-D, but it is omitting some results in some lines (it displays only 1 pair per line/row, and no more results) .
try to aplly it in a small sample :


54 60 63 66 67 68 72 75 82 84 100
50 51 56 58 61 62 67 68 90 92 94
44 47 50 54 61 64 80 92 93 95 100
53 54 61 64 69 84 92 94 97 98 99
53 57 59 78 79 83 85 86 93 96 100
52 53 57 58 59 63 68 73 76 77 81
53 58 60 69 72 79 85 86 88 91 98
48 62 69 74 76 78 80 83 93 97 99
50 51 55 57 61 69 71 72 85 89 96
38 40 43 59 60 63 64 72 73 85 93
40 48 54 56 57 59 67 69 73 86 92
39 43 46 55 58 62 74 82 95 97 100


You'll see that some results are missing

Also I have very small limited knowledge of VB in Excel.

many thanks
Dec 11 '15 #12
To be clear, can you confirm that you are trying to find ANY two numbers in each row that differ by 19?
Dec 11 '15 #13
zmbd
5,501 Expert Mod 4TB
OK, your mixing formulas again....
absolute value |A-B|=|C| , that should be 19.
(...) i tried valDif in the code such as A-B, B-C, C-D, but it is omitting some results in
However, it appears that you are after Option2

So let's look at the logic:
(I so highly advise that you learn how to properly chart your prgrams, I prefer Nassi-Shneiderman Charts (PDF) for most, if not all, my programming logic)

Now what I would do is set your reference so that [R1C1] actually refers to the start of your dataset and then offset from there checking for null/isblank as one went.

so the logic
+ [Start]
+ [find first cell in data set]
+ [set a pointer to that cell]=[R1C1]
+ [x=0]
+ [n=0]
+ <Start loop - Rows in datatable>
++<Start loop calculations>
+++[value of first cell][R1C1]offset(R+x,C+n)=lcell
+++[value of second cell][R1C1]offset(R+x,C+(n+1))=rcell
+++ [compare lcell-rcell = |19| report pair (to?) if true]
+++ [increment your column offset n]
+++ <end loop calculations if either cell offset returns empty cell>
++ [increment your row offset x]
++ [reset the column offset n to 0]
++ <end loop rows if the offset returns empty cell>
+ [end]

See if the logic chart and the afore mentioned logic will get you to the right solution.

As a note:
The following is copyright by MS Original Link is here: https://support.microsoft.com/en-us/kb/269866 however support is ended for this link so I copy part of it here for academic reasons:

A1 Reference Style vs. R1C1 Reference Style

The A1 Reference Style
By default, Excel uses the A1 reference style, which refers to columns as letters (A through IV, for a total of 256 columns), and refers to rows as numbers (1 through 65,536). These letters and numbers are called row and column headings. To refer to a cell, type the column letter followed by the row number. For example, D50 refers to the cell at the intersection of column D and row 50. To refer to a range of cells, type the reference for the cell that is in the upper-left corner of the range, type a colon (:), and then type the reference to the cell that is in the lower-right corner of the range.
The R1C1 Reference Style
Excel can also use the R1C1 reference style, in which both the rows and the columns on the worksheet are numbered. The R1C1 reference style is useful if you want to compute row and column positions in macros. In the R1C1 style, Excel indicates the location of a cell with an "R" followed by a row number and a "C" followed by a column number.
Dec 11 '15 #14
many thanks to all,
codegazer i 'm trying to find ALL POSSIBLE SOLUTIONS THAT two numbers in each row differ by 19 or
its absolute value is 19 such as 58 - 39 = 19 or |58 - 39|= 19 or |39 - 58|= 19

the code that i posted above is omitting some results in some lines (it displays only 1 pair per line/row in final results, and no more results, it is excluding some results, and i don't know why) and i dont know how to fix that.

as I have said above i have very small limited knowledge of VB in Excel.
Does anyone have a better pragmatical approach that solve the problem more easily ??? Any help is so welcome.

many thanks in advance
Dec 18 '15 #15
zmbd
5,501 Expert Mod 4TB
SANDRADEPAR:

+ We understand your question.

+ What we are waiting for is for you to tell us what it is that you are after. One time it's one thing, the next it appears you want something else.

+ Using the pseudo-code I gave you in post#14, it took me all of 20 minutes, and most of that was hand-checking against your dataset in the original post, to write the script for comparing the adjacent pairs - something you already have. If this isn't what you're after then you need to tell us what you've tried to do to fix the script
Dec 18 '15 #16
Come on, Sandradepar.
You were very nearly there when you posted your code a week ago, but your code only ever compares adjacent pairs because you use j & j+1.
To find all results in each row, you will have to search from j to the end of the row
Dec 18 '15 #17
zmbd
5,501 Expert Mod 4TB
codegazer, I think that is what SANDRADEPAR is after...
(from the post#12, using the data in post #1)
34-46, 46-61, 61-70, 70-73....
49-51, 51-55, 55-62, 62-63....

Which in this data set (in post#1) only occur 8 times and then only once in the rows where the pairs have a difference of 19 - and they are all negative in nature :)
It may be the case that SANDRADEPAR has one solution to the question at hand; however, SANDRADEPAR needs to clarify the actual goal.

What clouds the issue for me is the rows of numbers at the bottom of the example data in post#1:
16 11 || 88 3 || 2 25 0 || 73 61...
Dec 18 '15 #18
many thanks to all, codegazer and zmbd

What i'm trying to say (the main goal) is that to find ALL POSSIBLE SOLUTIONS THAT any two numbers within each line/row differ by 19 or its absolute value is 19 such as 58 - 39 = 19 or |58 - 39|= 19 or |39 - 58|= 19 for each line/row.

the code that i posted above is omitting some results in some lines (it displays only 1 pair per line/row in final results, and no more results, it is excluding some results, and i don't know why) and i dont know how to fix that.
See the code:


Expand|Select|Wrap|Line Numbers
  1.    Sub difference()
  2.  
  3.     Dim lngLastRow As Long
  4.     Dim lngLastColumn As Long
  5.     Dim lngMatch As Long
  6.  
  7.     Dim arrMatch(100) As Variant
  8.     Dim arrMatchValues(100) As Variant
  9.     Dim arrMatchValues2(100) As Variant
  10.  
  11.     Const valDif = 19
  12.  
  13.     lngMatch = 0
  14.  
  15.     Sheets("Plan1").Select
  16.  
  17.     lngLastRow = Sheets("Plan1").Range("A1").End(xlDown).Row
  18.     lngLastColumn = Sheets("Plan1").Range("A1").End(xlToRight).Column
  19.  
  20.  
  21.     For i = 1 To lngLastRow
  22.         For j = 1 To lngLastColumn
  23.                 If Cells(i, j + 1).Value - Cells(i, j).Value = valDif Then
  24.  
  25.                     arrMatch(lngMatch) = Cells(i, j).Address
  26.                     arrMatchValues(lngMatch) = Cells(i, j + 1).Value
  27.                     arrMatchValues2(lngMatch) = Cells(i, j).Value
  28.  
  29.                     lngMatch = lngMatch + 1
  30.  
  31.                 End If
  32.         Next j
  33.     Next i
  34.  
  35.     Sheets("Plan2").Select
  36.  
  37.     Cells(1, 1).Value = lngMatch & " records found"
  38.  
  39.     For i = 0 To lngMatch
  40.  
  41.         Cells(i + 1, 3).Value = arrMatch(i)
  42.         Cells(i + 1, 5).Value = arrMatchValues(i)
  43.         Cells(i + 1, 6).Value = arrMatchValues2(i)
  44.  
  45.     Next i
  46.  
  47.     End Sub
  48.  
  49.  
And try to apply it in a small sample :

Expand|Select|Wrap|Line Numbers
  1. 54 60 63 66 67 68 72 75 82 84 100
  2. 50 51 56 58 61 62 67 68 90 92 94
  3. 44 47 50 54 61 64 80 92 93 95 100
  4. 53 54 61 64 69 84 92 94 97 98 99
  5. 53 57 59 78 79 83 85 86 93 96 100
  6. 52 53 57 58 59 63 68 73 76 77 81
  7. 53 58 60 69 72 79 85 86 88 91 98
  8. 48 62 69 74 76 78 80 83 93 97 99
  9. 50 51 55 57 61 69 71 72 85 89 96
  10. 38 40 43 59 60 63 64 72 73 85 93
  11. 40 48 54 56 57 59 67 69 73 86 92
  12. 39 43 46 55 58 62 74 82 95 97 100
You'll see that some results are missing because as my friend codegazer said:

codegazer: " your code only ever compares adjacent pairs because you use j & j+1.
To find all results in each row, you will have to search from j to the end of the row".
The problem with my code is what u said above, but i'm not able to fix that, because I have very small limited knowledge of VB in Excel.

Can help me?

many thanks
Dec 21 '15 #19
You are nearly there.
A couple of adjustments as below should show the results

Expand|Select|Wrap|Line Numbers
  1. For i = 1 To lngLastRow
  2.  For j = 1 To lngLastColumn-1
  3.     'extra loop to search to end of row
  4.    for n= j+1 to lnglastcolumn
  5.      If abs(Cells(i, n).Value - Cells(i, j).Value) = valDif Then
  6.  
  7.        arrMatch(lngMatch) = Cells(i, j).Address
  8.        arrMatchValues(lngMatch) = Cells(i, n).Value
  9.        arrMatchValues2(lngMatch) = Cells(i, j).Value
  10.  
  11.        lngMatch = lngMatch + 1
  12.  
  13.      End If
  14.    Next n
  15.  Next j
  16. Next i
  17.  
Dec 21 '15 #20
zmbd
5,501 Expert Mod 4TB
Attached is a file with one approach to the solution:

To use:
>always scan files with your antivirus.
Select the upper-right-hand cell of the data-table.
Sheet1!A1
Excel2013>Ribbon>View>Macros>ViewMacros
[LoopAndCompareAdjecentCells]
Does just that, with a row of data compares the adjacent cells
a-b, b-c, c-d, etc...
Repeating for each row

[LoopAndCompareWithinRow]
Does just that, takes each cell in turn and compares with the remaining cells within the row
a-b, a-c, a-d.... b-c, b-d, b-e,.... c-d, c-e, c-f, etc....
Repeating for each row

Following is the source code for module, insert a standard module or copy and paste the following in to the "ThisWorkbook" module of the workbook:

All of the output is being sent to the Immediate pane - open with <ctrl><g>
>> This pane will scroll thru very quickly so not every result will be visible in the pane
LINE 27 and LINE 71
Are the comparison lines for the ABS(LCell-RCell)=19
These are one of the lines you will need to modify to store your results where you need them... I did not code this because you didn't indicate what you wanted done with this information.

Personally, I would either open a text file for output and write the results or insert a new worksheet and insert the values there as found.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit 'DO NOT DUPLICATE THIS LINE IN YOUR MODULE
  2.  
  3. Sub loopandcompareadjecentcells()
  4. Dim zDifference As Long
  5. Dim zStartCell As Range
  6. Dim zLcell As Range
  7. Dim zRcell As Range
  8. Dim zrowoffset As Long
  9. Dim zclmoffset As Long
  10. '
  11. 'using the currently selected cell as the staring point
  12. zrowoffset = 0
  13. zclmoffset = 0
  14. '
  15. 'set up for the first compair and then enter the loop
  16. Set zStartCell = ActiveCell
  17. Set zLcell = zStartCell
  18. Set zRcell = zLcell.Offset(zrowoffset, (zclmoffset + 1))
  19. '
  20. 'Drop out of the procedure if the first cell is empty other wise continue until the first empty cell is found
  21. Do Until IsEmpty(zLcell)
  22. 'Row loop
  23. 'Drop out of the loop is the cell is empty else continue until the first empty cell is found
  24. Debug.Print "row= " & zLcell.Row
  25.     'loop until the first empty cell is found
  26.     Do Until IsEmpty(zLcell) Or IsEmpty(zRcell)
  27.         If Abs((zLcell.Value - zRcell.Value)) = 19 Then Debug.Print Tab(10); zLcell.Value & "-" & zRcell.Value & "="; (zLcell.Value - zRcell.Value)
  28.         zclmoffset = zclmoffset + 1
  29.         Set zLcell = zStartCell.Offset(zrowoffset, zclmoffset)
  30.         Set zRcell = zStartCell.Offset(zrowoffset, (zclmoffset + 1))
  31.     Loop
  32.     zrowoffset = zrowoffset + 1
  33.     zclmoffset = 0
  34.     Set zLcell = zStartCell.Offset(zrowoffset, zclmoffset)
  35.     Set zRcell = zStartCell.Offset(zrowoffset, (zclmoffset + 1))
  36. Loop
  37. If Not zStartCell Is Nothing Then Set zStartCell = Nothing
  38. If Not zLcell Is Nothing Then Set zLcell = Nothing
  39. If Not zRcell Is Nothing Then Set zRcell = Nothing
  40. Debug.Print "done"
  41. End Sub
  42.  
  43. Sub loopandcomparewithinrow()
  44. Dim zDifference As Long
  45. Dim zStartCell As Range
  46. Dim zLcell As Range ' cell on the left ( left - right = value )
  47. Dim zRcell As Range ' cell on the right ( left - right = value )
  48. Dim zrowoffset As Long ' Which row in the data table
  49. Dim zleftoffset As Long ' pointer relative to the table start
  50. Dim zrightoffset As Long ' pointer relative to the table start
  51. '
  52. zrowoffset = 0 ' Start in the first row where the currently selected cell is located
  53. zleftoffset = 0 ' Using the currently slected cell as the starting point in the row
  54. zrightoffset = 1 ' pull the adjcent cell to the starting cell
  55. '
  56. 'set up for the first compair and then enter the loop
  57. Set zStartCell = ActiveCell
  58. Set zLcell = zStartCell
  59. Set zRcell = zLcell.Offset(zrowoffset, (zleftoffset + zrightoffset))
  60. '
  61. 'now if there isn't anything in the first cell then drop out of the procedure otherwise keep going until we have an empty cell
  62. Do Until IsEmpty(zLcell)
  63. 'Row loop
  64. Debug.Print "row= " & zLcell.Row
  65.     'Start left cell loop
  66.     'once again, drop out if nothing is found in the cell else keep going until the first empty cell
  67.     Do Until IsEmpty(zLcell)
  68.         'Start right loop
  69.         'once again, drop out if nothing is found in the cell else keep going until the first empty cell
  70.         Do Until IsEmpty(zRcell)
  71.             If Abs((zLcell.Value - zRcell.Value)) = 19 Then Debug.Print Tab(10); zLcell.Value & "-" & zRcell.Value & "="; (zLcell.Value - zRcell.Value)
  72.             zrightoffset = zrightoffset + 1
  73.             Set zRcell = zStartCell.Offset(zrowoffset, (zleftoffset + zrightoffset))
  74.         Loop 'right cell
  75.         zleftoffset = zleftoffset + 1
  76.         zrightoffset = 1
  77.         Set zLcell = zStartCell.Offset(zrowoffset, zleftoffset)
  78.         Set zRcell = zStartCell.Offset(zrowoffset, (zleftoffset + zrightoffset))
  79.     Loop 'left cell
  80.     zrowoffset = zrowoffset + 1
  81.     zleftoffset = 0
  82.     Set zLcell = zStartCell.Offset(zrowoffset, zleftoffset)
  83.     Set zRcell = zStartCell.Offset(zrowoffset, (zleftoffset + 1))
  84. Loop 'row
  85. If Not zStartCell Is Nothing Then Set zStartCell = Nothing
  86. If Not zLcell Is Nothing Then Set zLcell = Nothing
  87. If Not zRcell Is Nothing Then Set zRcell = Nothing
  88. Debug.Print "done"
  89. End Sub
Attached Files
File Type: zip 964972.zip (28.0 KB, 78 views)
Dec 21 '15 #21
thank you codegazer ,
i inserted the adjustments that u provided me above into the code:
Expand|Select|Wrap|Line Numbers
  1.  Sub difference()
  2.  
  3.     Dim lngLastRow As Long
  4.     Dim lngLastColumn As Long
  5.     Dim lngMatch As Long
  6.  
  7.     Dim arrMatch(100) As Variant
  8.     Dim arrMatchValues(100) As Variant
  9.     Dim arrMatchValues2(100) As Variant
  10.  
  11.     Const valDif = 19
  12.  
  13.     lngMatch = 0
  14.  
  15.     Sheets("Sheet1").Select
  16.  
  17.     lngLastRow = Sheets("Sheet1").Range("A1").End(xlDown).Row
  18.     lngLastColumn = Sheets("Sheet1").Range("A1").End(xlToRight).Column
  19.  
  20.  
  21.      For i = 1 To lngLastRow
  22.  For j = 1 To lngLastColumn - 1
  23.     'extra loop to search to end of row
  24.    For n = j + 1 To lngLastColumn
  25.      If Abs(Cells(i, n).Value - Cells(i, j).Value) = valDif Then
  26.  
  27.        arrMatch(lngMatch) = Cells(i, j).Address
  28.        arrMatchValues(lngMatch) = Cells(i, n).Value
  29.        arrMatchValues2(lngMatch) = Cells(i, j).Value
  30.  
  31.        lngMatch = lngMatch + 1
  32.  
  33.      End If
  34.    Next n
  35.  Next j
  36. Next i
  37.  
  38.     Sheets("Sheet2").Select
  39.  
  40.     Cells(1, 1).Value = lngMatch & " records found"
  41.  
  42.     For i = 0 To lngMatch
  43.  
  44.         Cells(i + 1, 3).Value = arrMatch(i)
  45.         Cells(i + 1, 5).Value = arrMatchValues(i)
  46.         Cells(i + 1, 6).Value = arrMatchValues2(i)
  47.  
  48.     Next i
  49.  
  50.     End Sub
  51.  
I tried it several times , but nothing happened.
Can you see if i made the aproppriate adjustments right in the code?

merry christmas

God bless you forever
Dec 25 '15 #22
many thanks for your help zmbd,
i try to execute your 964972.zip file so many times in my EXCEL 2010.But it didn't work.
I run loopandcompareadjecentcells first, and after i run LoopAndCompareWithinRow . Nothing happened.
It didn't write any results.
All the test that i made in EXCEL 2010 were unsuccessfull.
I don't know if the codes of loopandcompareadjecentcells and LoopAndCompareWithinRow are compatible with old EXCEL 2010 ...
( i can't afford for a new whole OFFICE 2015 nowadays, cause i 'm broken)

Happy christmas

God bless you ad aeternum
Dec 25 '15 #23
zmbd
5,501 Expert Mod 4TB
1) Forgive me for the stupid tech-support questions: When you downloaded the file did you unzip it?

2) This code compiles and runs on a 2007, 2010, and 2013 - 32 and 64bit office installs. (Have a friend with 2007, I have a runtime 2010 on the laptop, and bit 32 and 64bit versions of 2013 on the desktop PCs (although the 64bit is going to be reformatted by my IT-staff over the new year)

3) These are not functions
This code is designed to take the activecell and use that as a starting point.
If you unzip the file, open in Excel, select sheet one, cell A1, and run either of the VBA scripts, it will work (see the instructions)

4) The output is to the Debug window, press <alt><F11> to open the editor, <ctrl><g> to open the immediate pane (IP).

4a) Because the dataset is so large, the output to the IP will scroll off of the top; thus, you will not see all of the results.

4b) To store the results you will need to modify the debug.print lines to store these in the desired location.

5) the actual script is in the post's code block.

-z
Dec 25 '15 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
6
by: Tony Williams | last post by:
SORRY I know we shouldn't do this but I'm desperate for an answer to this and the previous post didn't seem to get a response. I have a table with two fields, txtvalue (a number field) and...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
1
by: yotamaner | last post by:
Hi, I saw that someone asked a similar question before, but it's not what I'm looking for. I have two date fields (check in, check out) and "number of days" field. I want the script to calculate...
0
kiss07
by: kiss07 | last post by:
Hi friends, I have one doubt , whts difference between Time and Timestamp data type in Oracle 9i. Thanks, Kiss07.
2
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007...
4
by: RP | last post by:
I have a date entered in a TextBox. On this TextBox LostFocus, I want to retrieve the year part and then calculate the difference from current year.
3
by: prashantkuppa | last post by:
hi, iam struggling with difference between two dates of format type dd/mm/yyyy i need difference between two dates olddate(dd/mm/yyyy) - newdate(dd/mm/yyyy) and this should be in java script...
2
jkmyoung
by: jkmyoung | last post by:
Calculating Large Exponents Background: This is a quick article as to how to calculate the exponents of large numbers quickly and efficiently. Starting with a basic multiplication algorithm, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.