Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Applying the same macro to multiple excel files | Excel Programming | |||
Applying Protection To Multiple Sheets | New Users to Excel | |||
macro to compile columns on multiple sheets | Excel Discussion (Misc queries) | |||
Applying Macro to only certain sheets | Excel Programming | |||
Autofill applying untwanted formats to protected sheet | Excel Programming |