Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Macro with vlookup with variable sheet

Hi,

I am trying to write a loop macro that will insert a vlookup that references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan 08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan 08,
but when I run the macro in Sheet March 08 I want it to reference Sheet Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous sheet"!A:B,2,false)

Any help will be greatly appreciated

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Macro with vlookup with variable sheet

Hi
Try the following line:
Range("C2:C4").FormulaR1C1 = "=HLOOKUP(RC[-2]," & ActiveSheet.Previous.Name
& "!RC[-2]:R[2]C[-1],2,FALSE)"

rgds,
Lazzzx

"stuck4once" skrev i meddelelsen
...
Hi,

I am trying to write a loop macro that will insert a vlookup that
references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the
previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan 08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan
08,
but when I run the macro in Sheet March 08 I want it to reference Sheet
Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous
sheet"!A:B,2,false)

Any help will be greatly appreciated


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Macro with vlookup with variable sheet

Sorry, I pasted the wrong formula into my answer:
Range("C2:C10").FormulaR1C1 = "=VLOOKUP(RC[-2]," & ActiveSheet.Previous.Name
& "!C[-2]:C[-1],2,FALSE)"

Lazzzx


"Lazzzx" skrev i meddelelsen
...
Hi
Try the following line:
Range("C2:C4").FormulaR1C1 = "=HLOOKUP(RC[-2]," &
ActiveSheet.Previous.Name & "!RC[-2]:R[2]C[-1],2,FALSE)"

rgds,
Lazzzx

"stuck4once" skrev i meddelelsen
...
Hi,

I am trying to write a loop macro that will insert a vlookup that
references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the
previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan
08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan
08,
but when I run the macro in Sheet March 08 I want it to reference Sheet
Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous
sheet"!A:B,2,false)

Any help will be greatly appreciated



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Macro with vlookup with variable sheet


You need name all you worksheet names consistant ly. Either spell the
month names out or have them all 3 letters. The month of March is spelled
out while JAN is a abbreviation. I assumed all Month names where three
letters. A single quote is needed around a sheet name the has a space in it.

Make sure you testt for the month of January because the function below will
return DEC for the worksheet that has JAN on it

This code takes the month from the DATE function
LastMonth = "'" & Format(Month(Date - 1), "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonth &
"!A:B,2,FALSE)"

This function takes the month from the worksheet name
MonthNm = Left(ActiveSheet.Name, 3)
MonthNumber = Month((DateValue(MonthNm & " 1 08")))
LastMonthDate = DateSerial(8, MonthNumber - 1, 1)
LastMonthSheet = "'" & Format(LastMonthDate, "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonthSheet & _
"!A:B,2,FALSE)"

"stuck4once" wrote:

Hi,

I am trying to write a loop macro that will insert a vlookup that references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan 08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan 08,
but when I run the macro in Sheet March 08 I want it to reference Sheet Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous sheet"!A:B,2,false)

Any help will be greatly appreciated

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Macro with vlookup with variable sheet

Hi Joel,

Thanks for you help on this, it worked great, until I put it in to a macro
that I already have running.

Now it comes up with:

a compile error and highlights the word format in the lastMonthSheet line of
the macro.

Any idea why?



"Joel" wrote:


You need name all you worksheet names consistant ly. Either spell the
month names out or have them all 3 letters. The month of March is spelled
out while JAN is a abbreviation. I assumed all Month names where three
letters. A single quote is needed around a sheet name the has a space in it.

Make sure you testt for the month of January because the function below will
return DEC for the worksheet that has JAN on it

This code takes the month from the DATE function
LastMonth = "'" & Format(Month(Date - 1), "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonth &
"!A:B,2,FALSE)"

This function takes the month from the worksheet name
MonthNm = Left(ActiveSheet.Name, 3)
MonthNumber = Month((DateValue(MonthNm & " 1 08")))
LastMonthDate = DateSerial(8, MonthNumber - 1, 1)
LastMonthSheet = "'" & Format(LastMonthDate, "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonthSheet & _
"!A:B,2,FALSE)"

"stuck4once" wrote:

Hi,

I am trying to write a loop macro that will insert a vlookup that references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan 08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan 08,
but when I run the macro in Sheet March 08 I want it to reference Sheet Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous sheet"!A:B,2,false)

Any help will be greatly appreciated



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Macro with vlookup with variable sheet

Joel,

thanks for this, it all works fine, except when I add it to an already
existing macro.

It keep coming up with a compile error and highlighting the word format in
the LastMonthSheet line of the macro.

Any ideas why??

"Joel" wrote:


You need name all you worksheet names consistant ly. Either spell the
month names out or have them all 3 letters. The month of March is spelled
out while JAN is a abbreviation. I assumed all Month names where three
letters. A single quote is needed around a sheet name the has a space in it.

Make sure you testt for the month of January because the function below will
return DEC for the worksheet that has JAN on it

This code takes the month from the DATE function
LastMonth = "'" & Format(Month(Date - 1), "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonth &
"!A:B,2,FALSE)"

This function takes the month from the worksheet name
MonthNm = Left(ActiveSheet.Name, 3)
MonthNumber = Month((DateValue(MonthNm & " 1 08")))
LastMonthDate = DateSerial(8, MonthNumber - 1, 1)
LastMonthSheet = "'" & Format(LastMonthDate, "mmm") & " 08" & "'"
ActiveSheet.Range("C10").Formula = "=VLOOKUP(A1," & LastMonthSheet & _
"!A:B,2,FALSE)"

"stuck4once" wrote:

Hi,

I am trying to write a loop macro that will insert a vlookup that references
the previous worksheet/tab in a work book.

The worksheet/tab needs to be variable as the Macro will be run in a new
worksheet/tab each month and the vlookup will need to reference the previous
sheet only.

e.g

Col A. Col B. Col C.
ref Notes Previous Notes
1 Ok to Process
2 In query
3 Send back

So, if the above is set out in sheets 1, 2 and 3 in reference to 1=Jan 08,
2=Feb 08 and 3=March 08.

When I run the macro in Feb 08 I want the vlookup to reference sheet jan 08,
but when I run the macro in Sheet March 08 I want it to reference Sheet Feb
08 instead of Jan 08

in column c the macro will insert "vlookup(a1,"previous sheet"!A:B,2,false)

Any help will be greatly appreciated

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XL2002 - VLOOKUP with variable Sheet Name Trevor Williams Excel Worksheet Functions 3 July 29th 08 05:33 PM
Formula in macro with variable sheet names ewan7279 Excel Programming 4 August 21st 07 11:19 AM
Running a variable macro when any value is entered into a variable cell [email protected] Excel Programming 3 December 14th 05 05:15 PM
variable in a link where the variable is the name of the sheet darrelly Excel Worksheet Functions 1 October 7th 05 08:24 AM
Macro Creating Variable and using variable in a SQL statement Jimmy Excel Programming 4 October 25th 04 02:36 AM


All times are GMT +1. The time now is 03:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"