Learning by yourself trying, trial and error , and mistakes is probably the best way in the long run. But also getting help can help sometimes to reduce the time it takes. All our time is limited so helping each other, sharing experience and knowledge is good for us all.

Originally Posted by
Anshu
... It may take time for me, may be many days, but I think I can fix it, at least, to the working degree....
Even if you don’t manage it, then usually, when you ask for help again , the fact that you tried may make it easier to understand when you get more help and have the final solution.
I think probably one of the best start points is to forget VBA and think carefully of exactly what you want to do, and try to think of all the possible scenarios/ situations that might occur. You might miss a few things, so there may still then be the need later to change things. But the more you try to think of all the different scenarios will reduce how many times you need to change / modify coding in order to get it perfect. Also thinking carefully about all you want to do before writing the coding will increase the chances that you can modify the coding slightly to make it perfect, rather than it be possibly better to completely re write a new macro
After you having re thought all the issues, you might decide that either
_ (i) A complete new macro is better. You may want to either try that yourself. Or ask again somewhere else. You should never rely on any one source of help. At least not when first you start learning.
Or
_(ii) you can modify the existing macro
If, having thought about it you choose _(ii) , then my initial thoughts are then a few ideas for you
If rows may have different number of column entries, then you need a different method to calculate the last column Lc
If you have no cells outside of the data table range to the right, then you can use what is probably the second most commonly used way to calculate last column Lc
Code:
Let Lc = Me.Range("B4:" & CL(Me.Columns.Count) & Lr & "").Find(What:="*", After:=Me.Range("B4"), LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, Searchdirection:=xlPrevious).Column ' I am trying to find anything ( "*" is "wildcard" for anything ) in the range that is all row cells from column B in our data table range , I start at the first cell , I look for a value , I look at all text in the cell , I go through the columns , I go backwards which means that I effectively start again at the end of the range since I started at the first cell - the .Find will always go through the entire range and this trick makes sure I go through all cells starting at the last one - if i went forward I might hit an empty cell in the sequence - I could also have started at the last cell in the worksheet, assuming I never use the last cell in the worksheet
That code line will not work if you have anything to the right of your table data range. For example, in you supplied file you have some explaining text in column V . ( For example, "Student is decreasing in Maths" ) So that code line will return you 22, which is column V. If you remove all that explaining text to the right of your data table range , then that code line returns you 7 for column G
If you had the jagged data range below, then that code line returns you 9 for column I ( provided that you have nothing in any cells to the right of that table data range. B4:I6
_____ Workbook: Dynamic Table.xls ( Using Excel 2007 32 bit )
Row\Col |
B |
C |
D |
E |
F |
G |
H |
I |
4 |
ENGLISH |
50 |
45 |
30 |
20 |
15 |
|
|
5 |
HINDI |
45 |
50 |
40 |
35 |
65 |
5 |
7 |
6 |
MATHS |
70 |
55 |
40 |
25 |
10 |
|
|
Worksheet: Sheet1 (4)
( By the way… You might want to change the code line that I gave you previously to calculate the last row, Lr , of your data range
This alternative will allow you to have text or anything below the data range, in column B , provided that there is at least one empty cell after the last entry in column B in your range
Let Lr = Me.Range("B4").End(xlDown).Row
That code line will give you the correct value , 6 , in you supplied file.
The original code line that I gave you,
Let Lr = Me.Range("B" & Me.Rows.Count & "").End(xlUp).Row
will give you an incorrect value of 18 in your sample file because you had text in row 18 outside your data range, in column B ( A BIG THANK YOU for the help!! ) )
The next step would probably be to arrange that nothing is ever done in any row having less than 3 columns of information , since then you can never have 3 consecutive decreases so there is not point in considering those rows.
The rest I can’t advise on, as I don’t know how you want to treat a situation where blank cells within a sequence occur.
In any case if you need more help, then feel free to ask here again, and / or anywhere else.
Alan
Bookmarks