Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Applying autofill macro that covers multiple sheets/columns

I've already searched on autofill macros and found bits and pieces that help,
but my lack of understanding of the actual VBA is hampering my success with
this particular macro.

The macro in question is set up to copy and paste data to various
spreadsheets and then add formulas to each sheet depending on what I'm
looking for. I've managed to do this with the macro, but when it comes to
autofilling the formula columns on seperate sheets it's working, but only to
a point.

Using the following formula on sheet A, the macro works. It autofills to
the point in column A on Sheet A in the right column.

Sheets("Sheet A").Select

Range("O2").Select
ActiveCell.Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

However, when the macro moves onto sheet 2, it is still using the reference
from sheet A (i.e. on sheet A formula is dragged down to row 371 which is
correct, but on sheet B it's still pulling to row 371 which is too long).

Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

There's a third sheet that has the same behaviour as sheet B. I'm guessing
it's something to do with how I'm defining the 'Lastrow' but any help on the
matter would be much appreciated!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Applying autofill macro that covers multiple sheets/columns

A typo
Destination:=.Range("O2:O" & LastRow)
should be
Destination:=.Range("O2:O" & LastRow2)
as below


Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow2)
End With


If this post helps click Yes
---------------
Jacob Skaria


"bawpie" wrote:

I've already searched on autofill macros and found bits and pieces that help,
but my lack of understanding of the actual VBA is hampering my success with
this particular macro.

The macro in question is set up to copy and paste data to various
spreadsheets and then add formulas to each sheet depending on what I'm
looking for. I've managed to do this with the macro, but when it comes to
autofilling the formula columns on seperate sheets it's working, but only to
a point.

Using the following formula on sheet A, the macro works. It autofills to
the point in column A on Sheet A in the right column.

Sheets("Sheet A").Select

Range("O2").Select
ActiveCell.Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

However, when the macro moves onto sheet 2, it is still using the reference
from sheet A (i.e. on sheet A formula is dragged down to row 371 which is
correct, but on sheet B it's still pulling to row 371 which is too long).

Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

There's a third sheet that has the same behaviour as sheet B. I'm guessing
it's something to do with how I'm defining the 'Lastrow' but any help on the
matter would be much appreciated!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Applying autofill macro that covers multiple sheets/columns

You dont need to select or activate the sheet to do this...Use the worksheet
object...as below..which will be more faster..especially when you have work
with more number of sheets

Sub Macro1()
Dim ws As Worksheet, LastRow As Long

Set ws = Sheets("Sheet A")
LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row

ws.Range("O2").Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"
ws.Range("O2").AutoFill Destination:=ws.Range("O2:O" & LastRow)
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"bawpie" wrote:

I've already searched on autofill macros and found bits and pieces that help,
but my lack of understanding of the actual VBA is hampering my success with
this particular macro.

The macro in question is set up to copy and paste data to various
spreadsheets and then add formulas to each sheet depending on what I'm
looking for. I've managed to do this with the macro, but when it comes to
autofilling the formula columns on seperate sheets it's working, but only to
a point.

Using the following formula on sheet A, the macro works. It autofills to
the point in column A on Sheet A in the right column.

Sheets("Sheet A").Select

Range("O2").Select
ActiveCell.Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

However, when the macro moves onto sheet 2, it is still using the reference
from sheet A (i.e. on sheet A formula is dragged down to row 371 which is
correct, but on sheet B it's still pulling to row 371 which is too long).

Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

There's a third sheet that has the same behaviour as sheet B. I'm guessing
it's something to do with how I'm defining the 'Lastrow' but any help on the
matter would be much appreciated!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Applying autofill macro that covers multiple sheets/columns

Hi

Try to use Sheets("Sheet B").Activate not .Select,
or even better do not activate sheets at all, but use sheet references:

Sub aaa()
Dim LastRow As Long
Dim LastRow2 As Long
Dim shA As Worksheet
Dim shB As Worksheet

Set shA = Sheets("Sheet A")
shA.Range("O2").Formula = _
"=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

With shA
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With
Application.CutCopyMode = False

Set shB = Sheets("Sheet B")
shB.Range("O2").Formula = _
"=VLOOKUP(A2,Initials!A:H,8,FALSE)"
With shB
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With
End Sub

Regards,
Per

"bawpie" skrev i meddelelsen
...
I've already searched on autofill macros and found bits and pieces that
help,
but my lack of understanding of the actual VBA is hampering my success
with
this particular macro.

The macro in question is set up to copy and paste data to various
spreadsheets and then add formulas to each sheet depending on what I'm
looking for. I've managed to do this with the macro, but when it comes to
autofilling the formula columns on seperate sheets it's working, but only
to
a point.

Using the following formula on sheet A, the macro works. It autofills to
the point in column A on Sheet A in the right column.

Sheets("Sheet A").Select

Range("O2").Select
ActiveCell.Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

However, when the macro moves onto sheet 2, it is still using the
reference
from sheet A (i.e. on sheet A formula is dragged down to row 371 which is
correct, but on sheet B it's still pulling to row 371 which is too long).

Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

There's a third sheet that has the same behaviour as sheet B. I'm
guessing
it's something to do with how I'm defining the 'Lastrow' but any help on
the
matter would be much appreciated!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Applying autofill macro that covers multiple sheets/columns

Yes, I am that stupid. Thank you very much!

"Jacob Skaria" wrote:

A typo
Destination:=.Range("O2:O" & LastRow)
should be
Destination:=.Range("O2:O" & LastRow2)
as below


Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow2)
End With


If this post helps click Yes
---------------
Jacob Skaria


"bawpie" wrote:

I've already searched on autofill macros and found bits and pieces that help,
but my lack of understanding of the actual VBA is hampering my success with
this particular macro.

The macro in question is set up to copy and paste data to various
spreadsheets and then add formulas to each sheet depending on what I'm
looking for. I've managed to do this with the macro, but when it comes to
autofilling the formula columns on seperate sheets it's working, but only to
a point.

Using the following formula on sheet A, the macro works. It autofills to
the point in column A on Sheet A in the right column.

Sheets("Sheet A").Select

Range("O2").Select
ActiveCell.Formula = "=NETWORKDAYS(M2,H2,Lookup!$A$2:$A$82)-1"

Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

However, when the macro moves onto sheet 2, it is still using the reference
from sheet A (i.e. on sheet A formula is dragged down to row 371 which is
correct, but on sheet B it's still pulling to row 371 which is too long).

Sheets("Sheet B").Select

Range("O2").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=VLOOKUP(A2,Initials!A:H,8,FALSE)"

Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("O2").AutoFill _
Destination:=.Range("O2:O" & LastRow)
End With

There's a third sheet that has the same behaviour as sheet B. I'm guessing
it's something to do with how I'm defining the 'Lastrow' but any help on the
matter would be much 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
Applying the same macro to multiple excel files [email protected] Excel Programming 1 December 13th 06 09:23 AM
Applying Protection To Multiple Sheets Mhz New Users to Excel 4 July 6th 06 01:22 PM
macro to compile columns on multiple sheets simonsmith Excel Discussion (Misc queries) 2 May 9th 06 04:06 PM
Applying Macro to only certain sheets Darin Kramer Excel Programming 4 September 13th 05 05:46 PM
Autofill applying untwanted formats to protected sheet Daren Excel Programming 0 July 1st 05 12:29 AM


All times are GMT +1. The time now is 06:39 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"