Hi,
I’ve currently making a workbook in which I have about eighty sheets, each relating to a specific customer, and two or three other sheets summary sheets which analyse the whole portfolio.
In one of these summary sheets I want to pose a few (kind of) questions that look like
1. The percentage of income generated by ________ that comes from _______ fees is _____%
2. …
3. A list of companies who’s exposure from ______(K5)__ is greater than___(M5)_____;
…….and that sort of thing, where into the cells that look like_____ I can type different criteria and so return different answers in the answer space.
Each worksheet is named after its company. The layout of all worksheets is the exact same. In the column D is the names of the sources of income and in column H is the amount of income generated by each source. Some, in fact most, cells in the H column are zero.
I want to write a macro that takes the words that I’ve typed into the cells K5 and M5 and searches each sheet in turn for these words, selects the number parallel to each word, compares the two numbers, and if figure A is bigger than B returns the name of the current worksheet into the column O in the summary worksheet where I want my list.
Could anyone tell me why the error message Next without For keeps appearing? I've checked my indenting very carefully and its all in line. Aswell as this, im painfully aware that there are many other aspects of my macro-writing that could be improved upon, apart from just my propensity to get "errored". If anyone has an actually functional/more elegant solution to my problem I'd welcome it gladly.
I’ve currently making a workbook in which I have about eighty sheets, each relating to a specific customer, and two or three other sheets summary sheets which analyse the whole portfolio.
In one of these summary sheets I want to pose a few (kind of) questions that look like
1. The percentage of income generated by ________ that comes from _______ fees is _____%
2. …
3. A list of companies who’s exposure from ______(K5)__ is greater than___(M5)_____;
…….and that sort of thing, where into the cells that look like_____ I can type different criteria and so return different answers in the answer space.
Each worksheet is named after its company. The layout of all worksheets is the exact same. In the column D is the names of the sources of income and in column H is the amount of income generated by each source. Some, in fact most, cells in the H column are zero.
I want to write a macro that takes the words that I’ve typed into the cells K5 and M5 and searches each sheet in turn for these words, selects the number parallel to each word, compares the two numbers, and if figure A is bigger than B returns the name of the current worksheet into the column O in the summary worksheet where I want my list.
Could anyone tell me why the error message Next without For keeps appearing? I've checked my indenting very carefully and its all in line. Aswell as this, im painfully aware that there are many other aspects of my macro-writing that could be improved upon, apart from just my propensity to get "errored". If anyone has an actually functional/more elegant solution to my problem I'd welcome it gladly.
Code:
Sub Compare()
Dim ThingA As String, ThingB As String
Dim NumberA As Integer, NumberB As Integer
A = Worksheets("PieChart").Range("K5").Value
B = Worksheets("PieChart").Range("M5").Value
Dim ColumnO As Range
ColumnO = Worksheets("PieChart").Column(15)
ColumnO.ClearContents
For Each Worksheet In Worksheets
Dim SearchHere As Range
Set SearchHere = Range("D52: D61")
For Each MyCell In SearchHere
If MyCell.Value = ThingA Then
MyCell.Offset(0, 4).Value = NumberA
End If
Next MyCell
For Each MyCell In SearchHere
If MyCell.Value = ThingB Then
MyCell.Offset(0, 4).Value = NumberB
End If
Next MyCell
Dim SheetName As String
Dim CellIWant As Range
CellIWant = Worksheets("PieChart").Column(15).End(xlUp).Offset(1, 0)
SheetName = ActiveSheet.Name
If NumberA > NumberB Then
CellIWant.Value = SheetName
Else
If NumberA < NumberB Then
CellIWant.Value = Nothing
End If
Next Worksheet
End Sub
Last edited by a moderator: