H
henryonyeagbako
I have the following procedure MoveGrandTotalLabel code below which
finds a specified cell value on an excel spreadsheet and then formats
the found cell and inserts text next to the found cell. The sub is
passed excel objects from have that have been intiated in the main
subroutine as shown below. The problem is that when i step through the
code as soon i get to the CellAddress = Cells.Find statement the code
simply exits the sub routine and proceeds with the next statement in
the main sub. When i copy and paste this exact same code into the main
sub it works with no problems at all. The cell value to be searched is
clearly on the excel spreadsheet. The procedure MoveGrandTotalLabel is
in a different module to the PublishDataInExcel procedure. Any
suggestions on why this is happening and possible solutions welcome
Public Sub PublishDataInExcel(dbrs As DAO.Recordset, dbfld As
DAO.Field, SheetName As String, firstdate As Date, lastdate As Date)
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
Dim xlrng As Excel.Range
MoveGrandTotalLabel xlApp, xlWb, xlWs, xlrng, firstdate, lastdate
Sub MoveGrandTotalLabel(App As Excel.Application, Wb As
Excel.Workbook, Ws As Excel.Worksheet, rng As Excel.Range, firstdate
As Date, lastdate As Date)
Dim CellAddress As String
Dim Findstring As String
Findstring = "Grand Total"
rng.Value = Ws.name
Set Ws = Wb.ActiveSheet
rng.Value = Wb.name
CellAddress = Cells.Find(What:="Grand Total",
After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set rng = Ws.Range(CellAddress) '.Address
If Not rng Is Nothing Then
FirstAddress = rng ' .Address 'ActiveCell.Address
Do
rng.Cut
rng.End(xlToLeft).Select
Ws.Paste
Set rng = Ws.Columns("A:A")
rng.EntireColumn.AutoFit
Set rng = ActiveCell.FindNext(rng)
Loop While Not rng.Value Is Nothing And ActiveCell.Address <>
FirstAddress
End If
Set rng = rng.Offset(0, 1).Range("A1").Select
rng.Value = dbrs.RecordCount & " assets Aquired in the selected
period " & firstdate & " - " & lastdate
End Sub
finds a specified cell value on an excel spreadsheet and then formats
the found cell and inserts text next to the found cell. The sub is
passed excel objects from have that have been intiated in the main
subroutine as shown below. The problem is that when i step through the
code as soon i get to the CellAddress = Cells.Find statement the code
simply exits the sub routine and proceeds with the next statement in
the main sub. When i copy and paste this exact same code into the main
sub it works with no problems at all. The cell value to be searched is
clearly on the excel spreadsheet. The procedure MoveGrandTotalLabel is
in a different module to the PublishDataInExcel procedure. Any
suggestions on why this is happening and possible solutions welcome
Public Sub PublishDataInExcel(dbrs As DAO.Recordset, dbfld As
DAO.Field, SheetName As String, firstdate As Date, lastdate As Date)
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
Dim xlrng As Excel.Range
MoveGrandTotalLabel xlApp, xlWb, xlWs, xlrng, firstdate, lastdate
Sub MoveGrandTotalLabel(App As Excel.Application, Wb As
Excel.Workbook, Ws As Excel.Worksheet, rng As Excel.Range, firstdate
As Date, lastdate As Date)
Dim CellAddress As String
Dim Findstring As String
Findstring = "Grand Total"
rng.Value = Ws.name
Set Ws = Wb.ActiveSheet
rng.Value = Wb.name
CellAddress = Cells.Find(What:="Grand Total",
After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set rng = Ws.Range(CellAddress) '.Address
If Not rng Is Nothing Then
FirstAddress = rng ' .Address 'ActiveCell.Address
Do
rng.Cut
rng.End(xlToLeft).Select
Ws.Paste
Set rng = Ws.Columns("A:A")
rng.EntireColumn.AutoFit
Set rng = ActiveCell.FindNext(rng)
Loop While Not rng.Value Is Nothing And ActiveCell.Address <>
FirstAddress
End If
Set rng = rng.Offset(0, 1).Range("A1").Select
rng.Value = dbrs.RecordCount & " assets Aquired in the selected
period " & firstdate & " - " & lastdate
End Sub