简体   繁体   English

Excel VBA如何将单元格的一部分复制并粘贴到新制作的工作表中

[英]Excel VBA How to copy and paste a section of cells into a newly made sheet

I'm making a budgeter sort of thing that helps people keep track of their money. 我正在做一个预算制定者,可以帮助人们跟踪自己的钱。 I currently have a bunch of code that checks the current month and attempts to make a new sheet with the name (MM/YYYY) unless that sheet has already been made, if it has been made then nothing will happen. 我目前有一堆代码来检查当前月份,并尝试制作一个名称为(MM / YYYY)的新工作表,除非该工作表已经完成,否则将不会发生任何事情。

Private Sub Worksheet_Change(ByVal Target As Range)
    nowMonth = Month(Now)
    nowYear = Year(Now)
    sheetNameStr = nowMonth & "," & nowYear

    sheetExists = False
    For Each Sheet In Worksheets
        If sheetNameStr = Sheet.Name Then
            sheetExists = True
        End If
    Next Sheet
    If sheetExists = False Then
        Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        ws.Name = sheetNameStr
        MsgBox ("New sheet named " & sheetNameStr & "was created")
    End If
    Sheets("Main").Activate

    Worksheets("Main").Range("A5:D300").Copy Worksheets("sheetNameStr").Range("A1")
End Sub

The problem I am having is trying to copy and paste the history of my purchases/income and pasting it into the new sheet. 我遇到的问题是试图复制并粘贴我的购买/收入历史记录并将其粘贴到新表中。 I always get the 我总是得到

Run-time error '9': Subscript out of range 运行时错误“ 9”:下标超出范围

error. 错误。

If anyone could help that'd be great thanks! 如果有人可以帮助,将非常感谢!

Your line saying 你的行说

Worksheets("Main").Range("A5:D300").Copy Worksheets("sheetNameStr").Range("A1")

is referring to a worksheet called "sheetNameStr" , but you really want to refer to a sheet with the name contained in the variable sheetNameStr , ie 是指一个名为"sheetNameStr"的工作表,但您确实要引用具有工作表的名称,该工作表包含在变量sheetNameStr ,即

Worksheets("Main").Range("A5:D300").Copy Worksheets(sheetNameStr).Range("A1")

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

相关问题 如何使用输入框复制单元格范围并将其粘贴到新创建的工作表中 - How to copy range of cells using inputbox and paste to newly created sheet 如何使用 VBA 复制 excel 中的一列单元格,直到出现空白并将其粘贴到新工作表中? - How can I copy a column of cells in excel, with VBA, until there is a blank and paste it into a new sheet? 如何在vba中复制和粘贴工作表? - How to copy and paste a sheet in vba? Excel VBA - 如何复制和转置粘贴另一张纸 - Excel VBA - How do copy and transpose paste another sheet 如何在VBA Excel中使用按钮复制行并将其粘贴到新工作表? - How to use a button in VBA Excel to copy a row, and paste to a new sheet? Excel:如何使用复制粘贴命令到选定的工作表区域(VBA) - Excel: How to use Copy Paste command to selected sheet area (VBA) 如何使用vba在excel中仅复制和粘贴过滤的单元格 - How to copy and paste only filtered cells in excel using vba Excel VBA-如何在空白单元格之后复制和粘贴数据组? - Excel VBA - How to copy and paste groups of data following blank cells? 如何通过Excel中的VBA复制/粘贴已过滤/可见单元格中的公式 - How to copy/paste formula in filtered/visible cells via VBA in Excel 如何复制单元格并将其粘贴到 Excel VBA 宏中的多个单元格 - How to copy a cell & paste it to multiple cells in Excel VBA Macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM