[英]Transfer data to new sheet
I have a macro to create a new worksheet, name that sheet based on the value of cell in another sheet (ie sheet1.Range("F3").value
). 我有一个宏来创建一个新的工作表,根据另一个工作表中单元格的值命名该工作表(即
sheet1.Range("F3").value
)。
After that I need to go to another sheet in the same workbook where my data is located, select certain data and transfer it to the new sheet. 之后,我需要转到同一工作簿中数据所在的另一个工作表,选择某些数据并将其传输到新工作表。
This is the code I use to create the new sheet and name it. 这是我用来创建新工作表并命名的代码。
sub createNewSheet()
sheet_name_to_creat = sheet1.range("F3").value
for rep = 1 to (worksheets.count)
if lCase(Sheets(rep).name = Lcase(Sheet_name_to_create) Then
MagBox "This Sheet already exists"
exit sub
end if
next
Sheets.add after:=sheets(sheets.count
Sheets(ActiveSheet.Name).name=Sheet_name_to_create
end sub
You can declare worksheet variables and put your data sheet and your newly added sheet into them. 您可以声明工作表变量,然后将数据表和新添加的表放入其中。 Then it's easy to manipulate data on either one.
这样就很容易在任一数据上进行操作。
Sub createNewSheet()
Dim wsNew As Worksheet
Dim wsData As Worksheet
'Rename this sheet to the name of the sheet where your data is located
Set wsData = ActiveWorkbook.Sheets("MyDataSheet")
sheet_name_to_create = Sheet1.Range("F3").Value
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep)).Name = LCase(Sheet_name_to_create) Then
MagBox "This Sheet already exists"
Exit Sub
End If
Next
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(ActiveSheet.Name).Name = Sheet_name_to_create
Set wsNew = ActiveWorkbook.ActiveSheet
'Now grab the data from your data worksheet
Dim myData As String
myData = wsData.Range("A1").Value
'Then put it in your newly added sheet
wsNew.Range("A1").Value = myData
wsData = Nothing
wsNew = Nothing
End Sub
I would make a few edits to the above statement to make it more effective. 我将对以上声明进行一些修改以使其更有效。 I know the changes are minor but this should work.
我知道更改很小,但这应该可行。
Sub createNewSheet()
Dim wsNew As Worksheet
Dim wsData As Worksheet
'Rename this sheet to the name of the sheet where your data is located
Set wsData = ActiveWorkbook.Sheets("MyDataSheet")
sheet_name_to_create = Sheet1.Range("F3").Value
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep).Name) = LCase(Sheet_name_to_create) Then
MagBox "This Sheet already exists"
Exit Sub
End If
Next
Set wsNew = Sheets.Add after:=Sheets(Sheets.Count)
wsNew.Name = Sheet_name_to_create
wsData.Range("A1:Z80").copy 'Change range here to whatever range you need to move to the new sheet
wsNew.Range("A1").pastespecial xlPasteValuesAndNumberFormats
wsNew = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.