0

I am working on a macro to copy certain information from one worksheet to another.

I have a question concerning the autofill I used in the code below. Is it possible to only autofill to a certain range of rows depending on the amount of rows of worksheetA containing data instead of the manually inserted 'row 5000'?

Code so far included below

Thanks in advance.

Michiel

P.S. Don't mind all the select commands, I'm not really into all the VBA-programming and the data copying isn't that massive so the run-time is limited.

Sub WorksheetB()
'
' WorksheetB Macro
'
' Sneltoets: Ctrl+Shift+H
'
Sheets("WorksheetB").Select
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "='WorksheetA'!RC"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "='WorksheetA'!RC"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=IF('WorksheetA'!RC[6]>0,'WorksheetA'!RC[6],"""")"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = _
        "=CONCATENATE('WorksheetA'!RC[6],"" "",'WorksheetA'!RC[5])"
    Range("E2").Select
    Columns("E:E").EntireColumn.AutoFit
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "=IF('WorksheetA'!RC[6]>0,'WorksheetA'!RC[6],"""")"
    Range("A1").Select
    Selection.AutoFill Destination:=Range("A1:A5000"), Type:=xlFillDefault
    Range("A1:A5000").Select
    Range("B1").Select
    Selection.AutoFill Destination:=Range("B1:B5000"), Type:=xlFillDefault
    Range("B1:B5000").Select
    Range("C1").Select
    Selection.AutoFill Destination:=Range("C1:C5000"), Type:=xlFillDefault
    Range("C1:C5000").Select
    Range("E1").Select
    Selection.AutoFill Destination:=Range("E1:E5000"), Type:=xlFillDefault
    Range("E1:E5000").Select
    Range("F1").Select
     Selection.AutoFill Destination:=Range("F1:F5000"), Type:=xlFillDefault
    Range("F1:F5000").Select
End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
Stoepsteen
  • 21
  • 6
  • Yes it's possible. Perhaps read [error in finding last used cell in vba](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) to find the last cell and its corresponding row, e.g. `lastRow`. Then instead of autofilling, you can write the formula to the entire range in one go, e.g. `Range("B1:B" & lastRow).FormulaR1C1 = ...`. – BigBen Mar 05 '19 at 15:18
  • Ok, thanks, will try to implement this! – Stoepsteen Mar 05 '19 at 16:13

0 Answers0