【问题标题】:VBA Loop one sheet to the nextVBA循环一张到下一张
【发布时间】:2020-05-18 00:28:20
【问题描述】:

我有两张表,“数据 1”包含我的所有数据,“附表 1”是需要填写的表格。 我想要做的是遍历“数据 1”中的每一行,并将 E 列的值放入“计划 1”表中的 C 列,然后只保存“计划 1”表。然后重复该过程,直到完成所有行。这就是我目前所拥有的,我有点不知道下一步该做什么。

   Public Sub CopyRows()
        Dim SaveSheet As Worksheet
        Sheets("Data1").Select
        ' Find the last row of data
        FinalRow = Cells(Rows.Count, 1).End(xlUp).row
        ' Loop through each row
        For x = 2 To FinalRow

        'Code here. How can I go through all the rows in "Data 1" and place the value of column E into column C in "Schedule 1"

     'Save just the sheet
    Set SaveSheet = ActiveWorkbook.Worksheets(Data1)

     SaveSheet.SaveAs FilePath, xlXMLSpreadsheet
     SaveSheet.Name = SheetName 'I would like to put the name of values in Column A from "Data 1" sheet


        Next x
    End Sub

【问题讨论】:

  • 您无法保存工作表。只能保存工作簿,保存工作簿时会同时保存其中的所有工作表。
  • 如果在代码表顶部添加Option Explicit,VBE 的Intellisense 将显示SaveAs 方法不适用于变量SaveSheet被声明为工作表。对于每个对象,如果启用它,您只能使用 Intellisense 列出的其自己的方法和属性。

标签: excel vba for-loop


【解决方案1】:

不太清楚你想如何复制数据,但这应该给你一个开始:

Option Explicit

Public Sub CopyRows()

    Dim wsData As Worksheet, wsSchedule As Worksheet
    Dim FinalRow As Long, x As Long, wbNew As Workbook, nm

    Set wsData = ThisWorkbook.Sheets("Data1")
    Set wsSchedule = ThisWorkbook.Sheets("Schedule 1")

    FinalRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To FinalRow

        'copy over some data
        nm = wsData.Cells(x, "E").Value
        wsSchedule.Range("C1").Value = nm
        wsSchedule.Copy 'create a new workbook with the copied sheet
        Set wbNew = ActiveWorkbook 'the new workbook with the copied sheet
        'save the new workbook in an "exports" subfolder in the same
        '   folder as this workbook
        wbNew.SaveAs ThisWorkbook.Path & "\Exports\" & nm & ".xlsx"
        wbNew.Close False

    Next x

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多