简体   繁体   中英

How to apply Autofill function for a dynamic range in VBA?

I'm in the process of creating a macro that should return 3 results:

  • adds 3 columns to the right after column C
  • implements characters per line counting formula which returns number of characters per line from column C in newely created column D
  • implements total characters count formula in column E (counts sum of characters count returned from column D)

Future Result : As current code gives formula results in cells D2 and E2 I want it to run Autofill function so that no matter the number of rows in the spreadsheet it gives results for all of them (just like double-clicking in the bottom right corner of a cell with formula).

This is how code looks like now:

Sub AutoFillTest

Columns("D:F").Insert Shift:=xlToRight, _
    CopyOrigin:=xlFormatFromLeftOrAbove '

     Range("D2").Select
     ActiveCell.Formula = "=Len(Left(C2, IfError(Find(Chr(10), C2, 1), 99))) & IfError("" ,"" & 
     Len(Mid(C2, Find(Chr(10), A2, 1) + 1, 99)), """")"
     #Here goes Autofill part which I'm looking for#

     Range("D2,E2").Select
     Range("E2").Activate
     ActiveCell.Formula = "Len("C2")"
     #Here goes Autofill part which I'm looking for#
End Sub

You can use autofill like this :

Set SourceRange = Worksheets("Sheet1").Range("A1:A2")
Set fillRange = Worksheets("Sheet1").Range("A1:A20")

SourceRange.AutoFill Destination:=fillRange

Then what you would need to is find the last row for the columns you need.

UPDATE

With your need for dynamic last row, something simple like this might work:

Add a function:

Public Function GetLastRow(str_TabName As String, lng_Column As Long, lng_Row As Long) As Long
 
Sheets(str_TabName).Select
'Range(str_Column & lng_Row).Select
Cells(lng_Row, lng_Column).Select
Selection.End(xlUp).Select
 
GetLastRow = ActiveCell.Row
 
End Function

Then amend the code like this:

Sub AutoFillTest
    
'' Get last row
Dim lng_LastRow as Long
'' Pass argument as required sheet, the column as a number, and the last row to start from
lng_LastRow = GetLastRow("Sheet1", 4, 100000)

    Columns("D:F").Insert Shift:=xlToRight, _
        CopyOrigin:=xlFormatFromLeftOrAbove '
    
         Range("D2").Select
         ActiveCell.Formula = "=Len(Left(C2, IfError(Find(Chr(10), C2, 1), 99))) & IfError("" ,"" & 
         Len(Mid(C2, Find(Chr(10), A2, 1) + 1, 99)), """")"
         #Here goes Autofill part which I'm looking for#
Set SourceRange = Worksheets("Sheet1").Range("D2:D2")
Set fillRange = Worksheets("Sheet1").Range("D3:D" & lng_LastRow )
SourceRange.AutoFill Destination:=fillRange
    
         Range("D2,E2").Select
         Range("E2").Activate
         ActiveCell.Formula = "Len("C2")"
         #Here goes Autofill part which I'm looking for#

'' apply from the example above and see if it works for you End Sub

Note I haven't checked your formulae

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM