[英]Copy and paste into a new sheet that is created in the same VBA code
I can't find the answer to this question anywhere in here or at else on the internet! 我在这里或互联网上的任何地方都找不到这个问题的答案! So please help me:
所以请帮助我:
1) I wan't to create a new sheet that I name, 2) thereafter I wan't to copy specific cells into this newly created sheet. 1)我将不创建一个我命名的新工作表,2)之后,我将不将特定的单元格复制到此新创建的工作表中。
I've seen thousands of videos explaining the code to create a new sheet and name it after what I write in a certain cell, so far so good. 我看过成千上万的视频,解释了创建新工作表并以我在特定单元格中编写的内容命名的代码,到目前为止一切顺利。 I've also seen thousands where you copy values into an already existing sheet, nice.
我也看到过成千上万个将值复制到现有工作表中的地方,很好。 But I can't find anyone describing how to create a new sheet, named as Idefine in a specific cell, and then copy values into it;
但是我找不到任何人描述如何在特定的单元格中创建一个名为Idefine的新工作表,然后将值复制到其中。 and when I'm changing the name in the specific cell, I want to copy to that the new sheet.
当我在特定单元格中更改名称时,我想复制到新工作表中。
Here's the code I did for the first part (working fine) and the second part (working not so fine): 这是我为第一部分(工作正常)和第二部分(工作不太正常)编写的代码:
sheet_name_to_create = Blad1.Range("e33").Value
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep).Name) = LCase(sheet_name_to_create) Then
MsgBox "This sheet already exists!)"
Exit Sub
End If
Next
' börja kopieringen
Sheets.Add After:=ActiveSheet
Sheets(ActiveSheet.Name).Name = sheet_name_to_create
'Second part trying to copy and paste
'Now I want to copy values in "Blad1" to the newly made sheet ("sheet_name_to_create"). I know these codes are wrong, but it's what I got:
Range("A1:AM73").Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
ActiveWindow.Zoom = 25
' Or
WorksWorksheets("Blad1").Range ("A1:AF80").Copy Worksheets("sheet_name_to_create").Range ("A1:AF80")
Please help 请帮忙
When you create the new sheet, reference it - like so: 创建新工作表时,请参考它-如下所示:
Set sourceSheet = ActiveSheet
Set destSheet = Sheets.Add(After:=sourceSheet)
You can now reference either the source worksheet or destination using sourceSheet
and destSheet
. 现在,您可以使用
sourceSheet
和destSheet
引用源工作表或目标。
Then you can apply a name to your destSheet
: 然后,您可以将名称应用于
destSheet
:
destSheet.Name = sheet_name_to_create
And then you can copy/paste from source to destination sheet: 然后您可以从源代码复制/粘贴到目标表:
sourceSheet.Range("A1:AM73").Copy destSheet
So your code becomes: 因此,您的代码变为:
sheet_name_to_create = Blad1.Range("e33").Value
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep).Name) = LCase(sheet_name_to_create) Then
MsgBox "This sheet already exists!)"
Exit Sub
End If
Next
Set sourceSheet = ActiveSheet
Set destSheet = Sheets.Add(After:=sourceSheet)
destSheet.Name = sheet_name_to_create
sourceSheet.Range("A1:AM73").Copy destSheet
ActiveWindow.Zoom = 25
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.