简体   繁体   English

遍历行,将某些单元格中的值复制到另一个工作簿

[英]Iterate through row, copy value from certain cells to another Workbook

i'm a total noob in VBA and been trying to create a script that copies value from following cells in database (eg. copy from cell A x to C11 in another document, then B x to C12, etc.) and after that saving the filled document with a custom filename.我是 VBA 中的总菜鸟,并一直在尝试创建一个脚本,该脚本从数据库中的以下单元格复制值(例如,从单元格 A x复制到另一个文档中的 C11,然后从 B x复制到 C12 等),然后保存具有自定义文件名的填充文档。

After reading through tutorials/other stackflows that's what I've came up with:在阅读了我想出的教程/其他堆栈流之后:

Function WypelnianieSMT()


Dim rng As Range
Dim row As Range


Set rng = Range("A2:J29")

    For i = 2 To rng.Rows.Count

        Workbooks("LISTA CZESCI-1.xlsm").Worksheets("Arkusz1").rng.Cells(RowIndex:=i, ColumnIndex:="H").Copy
        Workbooks("Szablon Specyfikacji Materiału Technicznego.xlsx").Worksheets("Formularz klasyfikacji").Range("C11").PasteSpecial Paste:=xlPasteValues

        Workbooks("LISTA CZESCI-1.xlsm").Worksheets("Arkusz1").rng.Cells(RowIndex:=i, ColumnIndex:="I").Copy
        Workbooks("Szablon Specyfikacji Materiału Technicznego.xlsx").Worksheets("Formularz klasyfikacji").Range("C12").PasteSpecial Paste:=xlPasteValues

        Workbooks("LISTA CZESCI-1.xlsm").Worksheets("Arkusz1").rng.Cells(RowIndex:=i + 1, ColumnIndex:="G").Copy
        Workbooks("Szablon Specyfikacji Materiału Technicznego.xlsx").Worksheets("Formularz klasyfikacji").Range("C13").PasteSpecial Paste:=xlPasteValues

        Workbooks("Szablon Specyfikacji Materiału Technicznego.xlsx").SaveAs Filename:="C:\***\Desktop\Makro" & Range("C2").Value & ".xlsx", FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False

    Next
End Function

sorry guys, I suppose that the script is totally messed, but couldn't work anything else out.对不起伙计们,我想脚本完全一团糟,但无法解决其他问题。

It's never a good idea to use faulty code to explain what you want.使用错误的代码来解释你想要什么从来都不是一个好主意。 Therefore it probably wasn't a good idea of mine to try and make head and toe of it.因此,尝试完全接受它可能不是我的好主意。 But you did.但你做到了。 And I did.我做到了。 And this is where we are now.这就是我们现在所处的位置。 I hope it's useful to you.我希望它对你有用。

Function WypelnianieSMT()
    ' 009

    ' note that 'Row' is a VBA object.
    ' The name shouldn't be used for a user-declared variable

    Dim WsList As Worksheet
    Dim WsSpecs As Worksheet
    Dim Fn As String                        ' file name
    Dim C As Long                           ' output column
    Dim R As Long                           ' input row

    ' both these workbooks must be open
    Set WsList = Workbooks("LISTA CZESCI-1.xlsm").Worksheets("Arkusz1")
    Set WsSpecs = Workbooks("Szablon Specyfikacji Materialu Technicznego.xlsx").Worksheets("Formularz klasyfikacji")

'    Dim Rng As Range
'    ' this is the joker in the deck: on which sheet is this range?
'    ' it's the ActiveSheet by default. But which is the ActiveSheet?
'    Set Rng = Range("A2:J29")

    C = 3                                   ' first column to use for output
    For R = 2 To 29
        With WsList
            .Cells(R, "H").Copy Destination:=WsSpecs.Cells(11, C)
            .Cells(R, "I").Copy Destination:=WsSpecs.Cells(12, C)
            .Cells(R, "G").Copy Destination:=WsSpecs.Cells(13, C)
        End With
        C = C + 1
    Next R

    ' you probably don't want to save the document on every loop
    ' therefore delay the sdaving until the looping is done
    ' Range("C2") is on the WsSpecs tab???
    Fn = "\Desktop\Makro" & WsSpecs.Range("C2").Value & ".xlsx"
    WsSpecs.SaveAs Filename:=Environ("Userprofile") & Fn, _
                   FileFormat:=xlOpenXMLStrictWorkbook, _
                   CreateBackup:=False
End Function

The code is totally untested because I don't have data.该代码完全未经测试,因为我没有数据。

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

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