[英]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.