我可以编写1行VBA代码来进行100行复制并自动填充A的最后一行直到B的最后一行

问题描述 投票:0回答:1

我有证书文档,当我在 B 列中输入修复代码时,它将从数据库表中查找该修复代码的详细信息。目前我在单元格的 ea 中有 Vlookup 公式,但我希望写入证书时的数据保持原样,如果数据库发生变化,那么证书会发生我不希望的变化,所以我在下面编写了宏,几乎正是我想要的,只是有几个问题。

在 Sh2 上,我有一个辅助行,它从另一个文档中获取工具校准数据,包括工具序列号、工具描述和校准到期日期。我有大约 100 个数据单元格需要进入 T 行到 Row ~DT 有没有一种方法可以让我用 1 行代码来完成此操作,而不是每次将 ea 范围更改 1 100 行。

完成所有 vlookups 后,我希望“A”列从“A”的最后一行自动填充到“B”的最后一行。我不能总是从 A2:A 开始,因为有时在奇怪的情况下,我们需要一行重做证书,并将其称为相同的报告,末尾带有 B,它会覆盖该报告并使所有接下来的数字都错误.

提前致谢,托德

Sub Autofill()
Dim Sh1 As Worksheet
Dim Sh2 As Worksheet
Dim MyVariable As Variant
Dim rng As Range
Dim Last_Row As Single
Dim Last_Row2 As Single

Set Sh1 = Sheets("Certificates") '<-The sheet that you test B for TRUE
Set Sh2 = Sheets("Codes") '<- The sheet for the vlookup range
Last_Row = Sh1.Range("B" & Rows.Count).End(xlUp).Row
Last_Row2 = Sh1.Range("A" & Rows.Count).End(xlUp).Row

For Each rng In Sh1.Range("B2:B" & Last_Row)
    If rng > 0 And Cells(rng.Row, "E").Value = 0 Then

            Cells(rng.Row, "E").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 4, 0)
            Cells(rng.Row, "F").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 5, 0)
            Cells(rng.Row, "J").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 9, 0)
            Cells(rng.Row, "K").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 10, 0)
            Cells(rng.Row, "L").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 11, 0)
            Cells(rng.Row, "M").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 12, 0)
            Cells(rng.Row, "N").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 13, 0)
            Cells(rng.Row, "O").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 14, 0)
            Cells(rng.Row, "P").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 15, 0)
            Cells(rng.Row, "Q").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 16, 0)
            Cells(rng.Row, "R").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 17, 0)
            Cells(rng.Row, "S").Value = Application.VLookup(rng, Sh2.Range("$A$2:$R$15000"), 18, 0)
            Cells(rng.Row, "T").Value = Sh2.Range("G19") 'Essentially I need to repeat this for every cell
            Cells(rng.Row, "U").Value = Sh2.Range("H19") 'all the way to - Cells(rng.Row, "DT").Value = Sh2.Range("DG19")
            Cells(rng.Row, "V").Value = Sh2.Range("I19") 'is there anyway I can do this with 1 line of code
            Cells(rng.Row, "W").Value = Sh2.Range("J19") 'instead of like 100 seperate lines?
            Cells(rng.Row, "X").Value = Sh2.Range("K19") 'Not as critical but can it be done with the Vlookup section above
            Cells(rng.Row, "Y").Value = Sh2.Range("L19") 'to clean up the code as Row J-S is all in order
            Cells(rng.Row, "Z").Value = Sh2.Range("M19")
    End If
Next rng
    Sh1.Range("A" & Last_Row2).Select
    Selection.Autofill Destination:=Sh1.Range("("A" & Last_Row2):("A" & Last_Row)")
         'Currently this last line doesnt work
         'I need this to be able to autofill "A" from whatever the last Row in "A" is to the last row in "B"
End Sub
excel vba
1个回答
0
投票

你不需要太多括号。只是简单地尝试-

Sh1.Range("A" & Last_Row2).Select
Selection.Autofill Destination:=Sh1.Range("A" & Last_Row2 & ":A" & Last_Row)
© www.soinside.com 2019 - 2024. All rights reserved.