简体   繁体   English

自动填充到 vba 中的命名范围

[英]Autofill to named range in vba

I'm trying to write a macro to autofill a range dynamically as you can see in the photo :我正在尝试编写一个宏来动态自动填充范围,如您在照片中看到的:

在此处输入图片说明

I've tried this code and it return an error :我试过这段代码,它返回一个错误:


Sub Auto()

Dim selection1 As Range
Dim selection2 As Range
h = 1
g = 1


This two loops I used to detect the the first empty cell in column AZ to refer to its range later这两个循环我用来检测列 AZ 中的第一个空单元格,以便稍后引用其范围

Do Until Cells(h + 1, 52).Value = ""
h = h + 1

Loop
Do Until Cells(g + 1, 1).Value = ""
g = g + 1

Loop


Set selection1 = Range("AZ" & h & ":" & "BD" & h)



Set selection2 = ActiveSheet.Range("AZ" & g & ":" & "BD" & g)

This where I tested the above idea and it worked fine and the range selected as it should as shown in the photo这是我测试上述想法的地方,它运行良好,并且选择的范围应如照片所示

Range("AZ" & h & ":" & "BD" & h).Select


'Autofill

This where I got the error:这是我得到错误的地方:

'Selection.AutoFill Destination:=Range("AZ" & g & ":" & "BD" & g), Type:=xlFillDefault







End Sub

Any Ideas?有任何想法吗?

This will autofill the data down.这将自动填充数据。
The method for finding the last row is the same as going to the last row and pressing Ctrl+Up .查找最后一行的方法与转到最后一行并按Ctrl+Up 相同

Sub Test()

    Dim LastRow1 As Long, LastRow2 As Long

    With ThisWorkbook.Worksheets("Sheet1")
        LastRow1 = .Cells(.Rows.Count, "AY").End(xlUp).Row 'Find last row in column AY.
        LastRow2 = .Cells(.Rows.Count, "AZ").End(xlUp).Row 'Find last row in column AZ.

        .Range(.Cells(LastRow2, "AZ"), .Cells(LastRow2, "BD")).AutoFill _
            Destination:=.Range(.Cells(LastRow2, "AZ"), .Cells(LastRow1, "BD"))
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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