View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Global Variables

First, if wsOld is part of the WbkOld workbook, you don't need code like:
Lusedrow = wbkOld.Sheets(wsOld.Name).Cells(Rows.Count, "B").End(xlUp).Row

you can refer to wsold directly
Lusedrow = wsOld.Cells(Rows.Count, "B").End(xlUp).Row

I think it makes the code easier to read (and write).

Second, I don't see anything that jumps out with a problem. But that doesn't
mean much.

The suggestion I would offer is to add a debug.print line right before the line
that causes the error. But print all the variables and their values so you know
what's going on.

So if this line is causing the trouble:

Set CopyTo = wbkNew.Sheets(wsNew.Name).Range("C" & NLusedrow & ":C" _
& NLusedrow + Lusedrow)
Or

Set CopyTo = wsNew.Range("C" & NLusedrow & ":C" & NLusedrow + Lusedrow)

Then add this before that line:

debug.print "nlusedrow: " & nlusedrow & vblf & "Lusedrow: " & lusedrow

Maybe something is not returning the value you think.

Karen53 wrote:

Hi,

For some reason the "Advisor" sections run fine. As soon as the ws name
changes to 'Annuity' I get a 'Method row of Object '_Global failed. wsOld
and wbkOld are set. Debug gives me the correct names. Is there something
about global variables I've missed?

Sub IDLocs(FromFileName, CopyFrom, CopyTo, DataType, FormatDateCell)

Dim Lusedrow As Long
Dim NLusedrow As Long

'Advisor
If wsOld.Name = "Advisor" And DataType = "ThisData" Then
Debug.Print "Starting Advisor ThisData " & DataType

'get the last used row
Lusedrow = wbkOld.Sheets(wsOld.Name).Cells(Rows.Count, "B"). _

End(xlUp).Row
Debug.Print "Advisor ThisData Lusedrow " & Lusedrow

Set CopyFrom = wbkOld.Sheets(wsOld.Name).Range("B2:B" & Lusedrow)
Set CopyTo = wbkNew.Sheets(wsNew.Name).Range("C4:C" & Lusedrow + 2)

Lusedrow = 0

End If

'Debug.Print "wsOld.Name & DataType ThisVolume " & wsOld.Name & " " _

& DataType
If wsOld.Name = "Advisor" And DataType = "ThisVolume" Then
Debug.Print "Starting Advisor ThisVolume " & DataType

Set CopyFrom = wbkOld.Sheets(wsOld.Name).Range("C1:D1")

'remove the lapsed month
wbkNew.Sheets(wsNew.Name).Range("K4:L4").Delete Shift:=xlUp

'add the new month data
Set CopyTo = wbkNew.Sheets(wsNew.Name).Range("K15:L15")
Set FormatDateCell = wbkNew.Sheets(wsNew.Name).Range("K15")

End If

Debug.Print "wbkOld.name " & wbkOld.Name
'Annuity
If wsOld.Name = "Annuity" And DataType = "ThisData" Then
Debug.Print "Starting Annuity ThisData " & wsOld.Name & " " & DataType

'get the last used row ***** Error is here *****
Lusedrow = wbkOld.Sheets(wsOld.Name).Cells(Rows.Count, "B"). _

End(xlUp).Row

Set CopyFrom = wbkOld.Sheets(wsOld.Name).Range("B2:B" & Lusedrow)

NLusedrow = wbkNew.Sheets(wsNew.Name).Cells(Rows.Count, "C"). _

End(xlUp).Row
NLusedrow = NLusedrow + 2 'move past last item and leave 1 space
'between
Advisor and Annuity
Set CopyTo = wbkNew.Sheets(wsNew.Name).Range("C" & NLusedrow _
& ":C" &
NLusedrow + Lusedrow)

Lusedrow = 0

End If

If wsOld.Name = "Annuity" And DataType = "ThisVolume" Then
Debug.Print "Starting Annuity & ThisVolume " & wsOld.Name & " " & _

DataType
Set CopyFrom = wbkOld.Sheets(wsOld.Name).Range("C1:D1")

'remove the lapsed month
wbkNew.Sheets(wsNew.Name).Range("H4:I4").Delete Shift:=xlUp

'add the new month data
Set CopyTo = wbkNew.Sheets(wsNew.Name).Range("H15:I15")
Set FormatDateCell = wbkNew.Sheets(wsNew.Name).Range("H15")

End If

End sub

--
Thanks for your help.
Karen53


--

Dave Peterson