Hi,
Please help, I have just started programming.
I need to extract some data from one sheet and place in another. My Macro was working fine until I noticed the data ranges have some inconsistency where at times the data in column A is missing a consecutive number : ie(3040014) is missing below in the monhtly data:
Please help, I have just started programming.
I need to extract some data from one sheet and place in another. My Macro was working fine until I noticed the data ranges have some inconsistency where at times the data in column A is missing a consecutive number : ie(3040014) is missing below in the monhtly data:
Column A Rows B to M contain figures
3040012 amount
56654
555
22
3040013 Amount
56
3040015 Amount
456
4567
3040016 Amount
3040012 amount
56654
555
22
3040013 Amount
56
3040015 Amount
456
4567
3040016 Amount
My Macro (below) FINDS the selected cost centre number ie 3040012 location and then FINDS the CostCentre number +1 ie 3040013 which sets up the range to be copied. This worked fine until I noticed some months a cost centre would not appear eg 3040014 so I couldn’t FIND the end row of the range for the cost centre above the missing costcentre ie 3040013.
This is the part of the macro which selects range to be copied
'Registers cost centre to search as variable (from named range "costcentre")
Searchfor = Range("CostCentre")
'Registers next cost centre to finish as (+1 to the Search For variable)
SearchEnd = Searchfor + 1
Searchfor = Range("CostCentre")
'Registers next cost centre to finish as (+1 to the Search For variable)
SearchEnd = Searchfor + 1
‘If cost centre has no data for that month then it causes an error
On Error GoTo ErrHandler2:
On Error GoTo ErrHandler2:
'Finds finds the cost centre the user has selected (start of the range to be copied)
Cells.Find(what:=Searchfor, After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(what:=Searchfor, After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
‘This is the first row address of the range to be selected
CostCentreStart = ActiveCell.Address
CostCentreStart = ActiveCell.Address
(This section causes an error if the next consecutive cost centre is missing as it searches for the costcentre number +1)
‘This finds the end of the Range to be selected
Cells.Find(what:=SearchEnd, After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
‘This finds the end of the Range to be selected
Cells.Find(what:=SearchEnd, After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
‘This is the last row of the range to be copied
CostCentreFinish = ActiveCell.Offset(-1, 12).Address
CostCentreFinish = ActiveCell.Offset(-1, 12).Address
‘Copies the Range
Range(CostCentreStart & ":" & CostCentreFinish).EntireRow.Copy
Range(CostCentreStart & ":" & CostCentreFinish).EntireRow.Copy
Is it possible to write into the macro to look for the next consecutive cost centre number if it cannot find the SearchEnd?
Any help would be appreciated!!!
Thanks
Nutsy
Nutsy