I'm learning Excel <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help;">VBA</acronym> programming, and I'm currently trying to get over an issue with how to get the difference between two values for a specific block.
I have one sheet which contains 13 columns in total i.e. from A to M.
Below is the description of Columns which will be useful for performing the desired calculation:
Column A contains random integer value. When there is no value present in it , that means the macro should stopped.
The Column "G" contain value of "9" but occurrence of this value is not fixed; it spreads randomly i.e. some cells contain null value (0) and some contains value 9 etc.
Column J contain a value at places only where there is a "9" present in Column "G", otherwise there is no value present in column J. Thus, Column J can contain a specific value only if there is a 9 present in a column G.
Column "K" contains the sum value for each cell and the value keeps on adding to the previous value until there are 9 present in Column G, once we encountered empty cell, the loop starts again looking for new block of 9
Column "M" contains some integer value at random places.
Now my question is , I want to select the first and last value present in Column M for a specific block of 9, once I can access first and last integer value for that block, I need to calculate the difference between those two values and the process has to repeat till there are no more 9 present in the entire worksheet in Column G. Once I get this difference value , then I have to select Final Sum value for that block that present in Column K, Once I get that , I will perform this operation = Difference value / Sum value . The value that we will get after this, will get multiplied by values present in Column J for that specific block and the new set of values after this operation is my ideal result.
This is an example how my worksheet looks like, so there are cells in Column J which contains any value that can be any integer or decimal value or may be even 0 and there may be cells in column G and J which may contain no data.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]G[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]M[/TD]
[TD]Difference[/TD]
[TD]Divide[/TD]
[TD]Final Result[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[TD]100 (200-100)[/TD]
[TD]14 (100/7.0)[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]9[/TD]
[TD]1.2[/TD]
[TD]1.2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3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Column G to M is given and Column Difference, Divide and Final result has to be computed by macro for whole worksheet. The macro will run till there is some value present in Column A.
Below is the code that I am trying , till now, this is just able to look for the first value in Column M but not able to locate the second value.
I have one sheet which contains 13 columns in total i.e. from A to M.
Below is the description of Columns which will be useful for performing the desired calculation:
Column A contains random integer value. When there is no value present in it , that means the macro should stopped.
The Column "G" contain value of "9" but occurrence of this value is not fixed; it spreads randomly i.e. some cells contain null value (0) and some contains value 9 etc.
Column J contain a value at places only where there is a "9" present in Column "G", otherwise there is no value present in column J. Thus, Column J can contain a specific value only if there is a 9 present in a column G.
Column "K" contains the sum value for each cell and the value keeps on adding to the previous value until there are 9 present in Column G, once we encountered empty cell, the loop starts again looking for new block of 9
Column "M" contains some integer value at random places.
Now my question is , I want to select the first and last value present in Column M for a specific block of 9, once I can access first and last integer value for that block, I need to calculate the difference between those two values and the process has to repeat till there are no more 9 present in the entire worksheet in Column G. Once I get this difference value , then I have to select Final Sum value for that block that present in Column K, Once I get that , I will perform this operation = Difference value / Sum value . The value that we will get after this, will get multiplied by values present in Column J for that specific block and the new set of values after this operation is my ideal result.
This is an example how my worksheet looks like, so there are cells in Column J which contains any value that can be any integer or decimal value or may be even 0 and there may be cells in column G and J which may contain no data.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]G[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]M[/TD]
[TD]Difference[/TD]
[TD]Divide[/TD]
[TD]Final Result[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[TD]100 (200-100)[/TD]
[TD]14 (100/7.0)[/TD]
[TD][/TD]
[/TR]
[TR]
[T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[/TD]
[TD]9[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]9[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Column G to M is given and Column Difference, Divide and Final result has to be computed by macro for whole worksheet. The macro will run till there is some value present in Column A.
Below is the code that I am trying , till now, this is just able to look for the first value in Column M but not able to locate the second value.
Code:
Sub RegisterDifference()
Dim FirstRegisterRead As Integer
Dim SecondRegisterRead As Integer
Dim RegisterReadDifference As Integer
Range("G2").Select
FirstRegisterRead = 0
Do While ActiveCell.Offset(0, -6).Value <> ""
If ActiveCell.Value = "9" Then
ActiveCell.Offset(0, 6).Select
Do While ActiveCell.Offset(0, 0).Value = ""
If ActiveCell.Offset(0, 0).Value = "" Then
ActiveCell.Offset(-1, 0).Select
End If
If ActiveCell.Offset(0, 0).Value <> "" Then
FirstRegisterRead = ActiveCell.Offset(0, 0).Value
ActiveCell.Offset(0, 3).Value = FirstRegisterRead
End If
Loop
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub