Hi,
. I’d started before the other 2 replied.. so I finished!! Here is my beginner’s attempt!. Surprisingly it seems to work.
. I changed your example data a bit to make it easier for me to check the results. Here is my modified example data: (Note also that I changed your Date heading in Summary to DATE. That is because for my program to work it is important that headings are spelt exactly the same in both sheets)
Book1 |
---|
|
---|
| A | B | C | D | E | F | G | H | I |
---|
2 | DATE | DRIVER NAME | REGISTRATION NUMBER | KM OUT(BRANCH) | KM IN(BRANCH) | KM AT FILL UP | LITRES OF FUEL | OIL AMOUNT | FLEET CARD AMOUNT |
---|
3 | 08.08.2014 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | R 1.00 |
---|
4 | 08.07.2014 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | R 2.00 |
---|
5 | 08.04.2014 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | R 3.00 |
---|
6 | 08.08.2014 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | R 4.00 |
---|
7 | 08.02.2014 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | R 5.00 |
---|
|
---|
. Then, when you select January you get this…..
Book1 |
---|
|
---|
| A | B | C | D | E | F | G | H | I |
---|
1 | | Januar | SELECT MONTH | | | | | | |
---|
2 | | | | | | | | | |
---|
3 | DATE | REGISTRATION NUMBER | DRIVER NAME | KM OUT(BRANCH) | KM IN(BRANCH) | KM AT FILL UP | LITRES OF FUEL | OIL AMOUNT | FLEET CARD AMOUNT |
---|
4 | 08.01.2014 | 16 | 8 | 24 | 32 | 40 | 48 | R 56.00 | R 8.00 |
---|
|
---|
. For February this……
Book1 |
---|
|
---|
| A | B | C | D | E | F | G | H | I |
---|
1 | | Februar | SELECT MONTH | | | | | | |
---|
2 | | | | | | | | | |
---|
3 | DATE | REGISTRATION NUMBER | DRIVER NAME | KM OUT(BRANCH) | KM IN(BRANCH) | KM AT FILL UP | LITRES OF FUEL | OIL AMOUNT | FLEET CARD AMOUNT |
---|
4 | 08.02.2014 | 10 | 5 | 15 | 20 | 25 | 30 | R 35.00 | R 5.00 |
---|
5 | | | | | | | | | |
---|
|
---|
….etc. etc. (Do not worry about any German Words- I use Excel German version, but I think it automatically converts it to English when you open it)
. I do not quite understand what is happening with those R’s. I am beginner and know nothing about those strange sort of sorting/ droping - down bits. Maybe you can spread some light on that one for me and explain wot is going on there?
. The size of everything (Rows / columns etc.) is limited to about 255 initially. But that is dead easy to change.
. You can change around the order of, or add new, Headings. The program automatically takes care of that. Just make sure the heading spellings are exactly the same in both sheets
. The Code has lots of unnecessary extra bits and steps and includes lots of annoying green graffiti (comments). But as a beginner I Have to do that to keep track of wot is going on. I will tidy and shorten it a bit and send a more shortened “sane” looking version in a Reply sometime. And anyway, I learn the most when a profi jumps in and gives a better code. So let’s both hope one of them does! On that same note, I would advise to take a good look at wot
Aladin Akyurek sai
d and his links etc.. He is a pro and (unlike me) has the experience and knows wot he is doing. For example his approach is probably a lot more “sane” and efficient. Maybe that goes also for the snb_ code, but I did not understand his and could not get it to work.. Probably lack of experience on my behalf.
. So I send the working file with macro in back to you here.
FileSnack | Easy file sharing
. See how you get on and get back if you need more help. I see you already saved your file as .xlsm, so I expect you have a basic idea of running macros (Of course this one starts automatically when you select a new month). You just have to enable macros when asked to when opening the file.
Here is the code: As I said do not be too shocked, I will sanify it a bit sometime!!
[face=Calibri]
Option Explicit ' Forces you to define variables-that helps memory space and as a by-product errors show up easier
'----First bit- Sets off main bit once a new date is entered.
Private Sub Worksheet_Change(
ByVal Target
As Range)
On Error GoTo TheEnd
' If anything goes wrong then end Sub without crashing
Dim DateBox
As Range
'By doing this you can call all the properties and methods of a Range Object by typing the dot after A1toF1Box
Set DateBox = Range("B1")
' Set that Range to a specific Range
'-------------------------------------
If Intersect(DateBox, Target)
Is Nothing Then ' If no intersection in the "Target Area"(that is where you typed or pasted in) and your area of interest(Here the "Box" A1 to F1)...
' - do nothing!!
Else ' The only other possibility is that where you typed or pasted in did intersected with your box. so then:
'######Main Bit-Does wot you want after changing Month---------------
Dim FuelCaptureDate
As Date, FuelCaptureMonth
As Byte 'Date in date Format, Month as 1.2.3...12
Dim FuelCaptureLastEntryRow
As Byte ' Working Out last entry in fuel capture
Let FuelCaptureLastEntryRow = Worksheets("FUEL CAPTURE").Cells.Find(What:="*", _
After:=Worksheets("FUEL CAPTURE").Cells(1, 1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'Thanks shg! for this code!
Dim FuelSummaryDate
As Date, FuelSummaryMonth
As Byte
Let FuelSummaryDate = Worksheets("FUEL SUMMARY - MONTHLY").Range("b1").Value
'Get selscted month-Note must do this as VBA sees it as a date!
Let FuelSummaryMonth = Month(FuelSummaryDate)
'Work out month number from date
Dim FuelSummaryLastEntryRow
As Byte ' Working Out last entry, if any, in fuel Summarry
Let FuelSummaryLastEntryRow = Worksheets("FUEL SUMMARY - MONTHLY").Cells.Find(What:="*", _
After:=Worksheets("FUEL SUMMARY - MONTHLY").Cells(1, 1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'Thanks shg! for this code!
On Err
GoTo TheEnd2
' Important here , if anything goes wrong go to end2....
Application.EnableEvents =
False 'Stop the worksheet change thing while we do the main bit
Worksheets("FUEL SUMMARY - MONTHLY").Range("A4:K" & FuelSummaryLastEntryRow).ClearContents
'Clear any enties allready in Summarry
Dim FuelCaptureRow
As Byte, FuelSummeryRow
As Byte, LastFuelCaptureColumn
As Byte, LastFuelSummaryColumn
As Byte 'Limiting everything to 255 for now: can easilly be changed.
Let FuelSummeryRow = 3
'Set initially row in Summary to 3
Let LastFuelCaptureColumn = Worksheets("FUEL CAPTURE").Cells(3, Columns.Count).End(xlToLeft).Column
'Work out actual last column in Fuel capture
Let LastFuelSummaryColumn = 10
'Instead of working out the last column I put in 10 as I see you have something in column AA
Dim FuelSummaryColumn
As Byte, FuelCaptureColumn
As Byte 'The column count numbers as you go along in next bit
For FuelCaptureRow = 3
To FuelCaptureLastEntryRow
' go thrpugh each row in fuel capture starting at row 3
Let FuelCaptureDate = Worksheets("FUEL CAPTURE").Cells(FuelCaptureRow, 1).Value
'get capture date
Let FuelCaptureMonth = Month(FuelCaptureDate)
'worj out capture minth number 1,2,3.....12
If FuelCaptureMonth = FuelSummaryMonth
Then ' Look for match in month, then...
Let FuelSummeryRow = FuelSummeryRow + 1
' Start new Summarry row
For FuelSummaryColumn = 1
To LastFuelSummaryColumn
' going througth every Fuel Column (Heading)....
For FuelCaptureColumn = 1
To LastFuelCaptureColumn
'....go through every fuel capture column (heading)
If Worksheets("FUEL SUMMARY - MONTHLY").Cells(3, FuelSummaryColumn) = Worksheets("FUEL CAPTURE").Cells(2, FuelCaptureColumn).Value
Then ' If heading match, then
Worksheets("FUEL SUMMARY - MONTHLY").Cells(FuelSummeryRow, FuelSummaryColumn).Value = Worksheets("FUEL CAPTURE").Cells(FuelCaptureRow, FuelCaptureColumn).Value
' The most important line!!- copy in the appropriate value in Summary
Else
' No heading match so do nothing
End If
Next FuelCaptureColumn
' go on to next captue heading
Next FuelSummaryColumn
' go on to next summary heading
Else
'No Month match so do nothing.
End If
Next FuelCaptureRow
' go oin to next capture Row and start the whole thing again
TheEnd2:
'If anything did go wrong make sure the worksheet change thing is turned back on (to default)
Application.EnableEvents =
True 'Turn workksheet change thing back on
End If
'#############End of main Bit---------------------------
'--------------------------
Exit Sub ' End Sub If everything went OK.. Otherwise....
TheEnd: MsgBox "Oh? It didn't work, sorry about that. Alan"
' Appologies from me if it did not work!
End Sub 'Worksheet_Change(ByVal Target As Range)[/face]
Alan_E