Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Help with macros

Forgive me if this posting is in the wrong area of this forum. I would like
to create a macro that inserts a formula into each cell in column I where the
column A is populated for that row.

The formula I'd like to use is: =LOOKUP(B2,'06-2005'!B:B,'06-2005'!J:J)

I want the formula to reflect the previous worksheet (month-1) rather than a
fixed name ('06-2005') putting in a fixed sheet name.

Is this possible? If so, how?

Thanks in advance,
Marina Garrison
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Help with macros

Here is some code

Dim sDate As String
sDate = Format(DateSerial(Year(Date), Month(Date), 0), "mm-yyyy")
Range("I1").Formula = "=LOOKUP(B2," & sDate & "!B:B,'" & sDate &
"'!J:J)"
Range("I1").AutoFill Range("I1").Resize(Cells(Rows.Count,
"A").End(xlUp).Row)


--
HTH

Bob Phillips

"Marina" wrote in message
...
Forgive me if this posting is in the wrong area of this forum. I would

like
to create a macro that inserts a formula into each cell in column I where

the
column A is populated for that row.

The formula I'd like to use is: =LOOKUP(B2,'06-2005'!B:B,'06-2005'!J:J)

I want the formula to reflect the previous worksheet (month-1) rather than

a
fixed name ('06-2005') putting in a fixed sheet name.

Is this possible? If so, how?

Thanks in advance,
Marina Garrison



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Help with macros

Marina

If I understand you correctly then I think the formula you are looking for
is a VLOOKUP which will return the value in column J from the previous sheet
where the value in Column B is found.

You can use a macro like this:

Sub InsForm()
Dim UsedRows As Long
Dim PrevSheet As String
Dim ColA As Range
Dim Cell As Range

If ActiveSheet.Index 1 Then
PrevSheet = Sheets(ActiveSheet.Index - 1).Name
UsedRows = Cells(Rows.Count, 1).End(xlUp).Row
Set ColA = Range(Cells(2, 1), Cells(UsedRows, 1))
For Each Cell In ColA
If Cell.Value < Empty Then
Cell.Offset(0, 8).FormulaR1C1 = _
"=VLOOKUP(RC[-7],'" & PrevSheet & "'!C2:C10,9,0)"
End If
Next Cell
End If
End Sub

which will (as requested) only insert the formula into rows where column A
is populated. Or you can use the code below which will insert a formula into
every row which tests to see that column A is populated before performing the
vlookup. This might be a better option if you are likely to go back and
populate cells in column A which may initially be blank. This version also
tests to see that the lookup value in column B actually exists on the
previous sheet i.e. it will not return #N/A errors.

Sub InsForm2()
Dim UsedRows As Long
Dim PrevSheet As String

If ActiveSheet.Index 1 Then
PrevSheet = Sheets(ActiveSheet.Index - 1).Name
UsedRows = Cells(Rows.Count, 1).End(xlUp).Row
Range(Cells(2, 9), Cells(UsedRows, 9)).FormulaR1C1 = _
"=IF(RC[-8]<"""",IF(ISNA(VLOOKUP(RC[-7],'" _
& PrevSheet & "'!C2:C10,9,0)),"""",VLOOKUP(RC[-7],'" _
& PrevSheet & "'!C2:C10,9,0)),"""")"
End If
End Sub

Hope this helps
Rowan

"Marina" wrote:

Forgive me if this posting is in the wrong area of this forum. I would like
to create a macro that inserts a formula into each cell in column I where the
column A is populated for that row.

The formula I'd like to use is: =LOOKUP(B2,'06-2005'!B:B,'06-2005'!J:J)

I want the formula to reflect the previous worksheet (month-1) rather than a
fixed name ('06-2005') putting in a fixed sheet name.

Is this possible? If so, how?

Thanks in advance,
Marina Garrison

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
weird saving of a document with macros resulting with macros being transfered to the copy alfonso gonzales Excel Programming 0 December 12th 04 09:19 PM
convert lotus 123w macros to excel macros rpiescik Excel Programming 1 September 18th 04 01:35 PM
Macro Size Limit / open macros with macros? andycharger[_7_] Excel Programming 6 February 13th 04 02:00 PM
Macros not appearing in the Tools Macro Macros list hglamy[_2_] Excel Programming 5 October 24th 03 09:10 AM
Suppress the Disable Macros / Enable Macros Dialog Shoji Karai Excel Programming 5 September 24th 03 03:10 AM


All times are GMT +1. The time now is 09:18 AM.

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"