View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
GEdwards GEdwards is offline
external usenet poster
 
Posts: 40
Default Use of Cell Values Instead of Hard Coding

I tried that but I get

Run-time error '9':

Subscript out of range

"ker_01" wrote:

You are very close;

When you pass WBook as a parameter, Excel will use the string that it
represents. When you pass "WBook", then Excel simply takes that string, and
does not know that you are trying to refer to a variable.

Set MstrInvList = Workbooks(WBook)

and
Set AllInvSht = Worksheets(WSheet)


HTH,
Keith

"GEdwards" wrote:

I am using Excel 2003 and want to make my code as generic as possible.

I am very junior with this but I know proper coding is ...

Dim MstrInvList As Workbook
Dim AllInvSht As Worksheet

Set MstrInvList = Workbooks("Invoices & Work Estimates.xls")
MstrInvList.Activate
Set AllInvSht = Worksheets("Invoices & Work Estimates")
AllInvSht.Activate

But I would like something like this...
where cell S2 contains "Invoices & Work Estimates"
and cell R2 contains "Invoices & Work Estimates.xls"

The SET lines below fail. Is there a way around this? Can cell values used
in place of hard coding the workbook, worksheet names, etc?

Dim MstrInvList As Workbook
Dim AllInvSht As Worksheet

WBook = Range("S2")
WSheet = Range("R2")

Set MstrInvList = Workbooks("WBook")
MstrInvList.Activate
Set AllInvSht = Worksheets("WSheet")
AllInvSht.Activate

Others that I need to consider are working with these others...
Set DestWB = Workbooks.Open("E:\Directory1\TempWorkSheet.xls")
Set SourceRange = ThisWorkbook.Sheets("Fun With Excel").Range("K8:W8")
Set DestSh = DestWB.Worksheets("Invoices & Work Estimates")


Any and all help would be appreciated. Thanks in advance!