Hi all,
Really just looking for an opinion on how to proceed witht he following.
I have raw data in the form of:
DataItemName|Date
These data item names give information about the location and details of a breach in a quality control measure. i.e. examples of the data would be:
BarrowNM.CV1|somedate
BarrowNM.H2S1|somedate
BarrowNM.WB1|somedate
DragonTer.WB1|somedate
DragonTer.H2S1|somdate
and so on and so forth. Basically, every time a dataitemname such as this appears in the data, it relates to a unique breach. What I would like to do is count the amount of times each of these breaches occurs and then populate some values in another sheet.
Now in my head I am thinking of something akin to the following
So I have tested this, and it works as anticipated. Counts the number of times a value appears, and echoes it into the appropriate cells in the appropriate sheet after the last row.
So I guess I should get to my question. As well as putting in the cell values, I would like to insert the name of the sheet the macro is ran on in the first column of the breaches sheet.
Something like
Sheets("Breaches").Cells(nRow, 1).Value = SheetName
But I am not sure of the appropriate code. Another point on this, all the sheet names are in the form of Dec10, Jan11, Feb11 etc. If the above macro inserts the sheet name in the first column, will excel automatically recognise it as being in a date format? This is important, as after I have compiled the data, I will then be writing a macro to plot the data in a series of charts with a timescale of a rolling 13 month period. (Not sure how I will define a rolling 13 month period in a macro, but that is a problem for another day)
Also, I have in effect 100 different dataitemnames, so as you can imagine, writing this macro in totalility, in the form I currently have it, will be both time consuming and will conatin a hundred if statements. Is there any more efficient ways you can think of doing this?
Thanks all for your time
Really just looking for an opinion on how to proceed witht he following.
I have raw data in the form of:
DataItemName|Date
These data item names give information about the location and details of a breach in a quality control measure. i.e. examples of the data would be:
BarrowNM.CV1|somedate
BarrowNM.H2S1|somedate
BarrowNM.WB1|somedate
DragonTer.WB1|somedate
DragonTer.H2S1|somdate
and so on and so forth. Basically, every time a dataitemname such as this appears in the data, it relates to a unique breach. What I would like to do is count the amount of times each of these breaches occurs and then populate some values in another sheet.
Now in my head I am thinking of something akin to the following
Code:
Sub test()
Dim lRow, nRow As Long
Dim BCV, BHS, BWB, DWB, DHS As Integer
BCV = 0
BHS = 0
BWB = 0
DWB = 0
DHS = 0
lRow = Range("A" & Rows.Count).End(xlUp).Row
nRow = Sheets("Breaches").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 1 To lRow
If Cells(i, 1).Value = "BarrowNM.CV1" Then
BCV = BCV + 1
End If
If Cells(i, 1).Value = "BarrowNM.H2S1" Then
BHS = BHS + 1
End If
If Cells(i, 1).Value = "BarrowNM.WB1" Then
BWB = BWB + 1
End If
If Cells(i, 1).Value = "DragonTer.WB1" Then
DWB = DWB + 1
End If
If Cells(i, 1).Value = "DragonTer.H2S1" Then
DHS = DHS + 1
End If
Next i
Sheets("Breaches").Cells(nRow, 2).Value = BCV
Sheets("Breaches").Cells(nRow, 3).Value = BHS
Sheets("Breaches").Cells(nRow, 4).Value = BWB
Sheets("Breaches").Cells(nRow, 5).Value = DWB
Sheets("Breaches").Cells(nRow, 6).Value = DHS
End Sub
So I have tested this, and it works as anticipated. Counts the number of times a value appears, and echoes it into the appropriate cells in the appropriate sheet after the last row.
So I guess I should get to my question. As well as putting in the cell values, I would like to insert the name of the sheet the macro is ran on in the first column of the breaches sheet.
Something like
Sheets("Breaches").Cells(nRow, 1).Value = SheetName
But I am not sure of the appropriate code. Another point on this, all the sheet names are in the form of Dec10, Jan11, Feb11 etc. If the above macro inserts the sheet name in the first column, will excel automatically recognise it as being in a date format? This is important, as after I have compiled the data, I will then be writing a macro to plot the data in a series of charts with a timescale of a rolling 13 month period. (Not sure how I will define a rolling 13 month period in a macro, but that is a problem for another day)
Also, I have in effect 100 different dataitemnames, so as you can imagine, writing this macro in totalility, in the form I currently have it, will be both time consuming and will conatin a hundred if statements. Is there any more efficient ways you can think of doing this?
Thanks all for your time
Last edited: