[英]copy sheet and cell value based on list and rename as per list with excel vba code
i am very new to vba, currently i am looking for code to copy cell values from list to multiple sheet in specific cell我对 vba 非常陌生,目前我正在寻找将单元格值从列表复制到特定单元格中的多个工作表的代码
what i am trying to do is as per bellow sheet "point" i have a list with values column B are the names and C & D are values我想做的是按照下面的表格“点”我有一个值列表 B 列是名称,C & D 是值
i need to copy sheet named "template" and rename as per the values in Column B and the values are un defined and be upto any length list sheet我需要复制名为“模板”的工作表并根据 B 列中的值重命名,并且这些值未定义并且可以达到任何长度的列表表
currently i am using bellow code to copy sheet and rename as per list目前我正在使用波纹管代码来复制工作表并根据列表重命名
Sub CopySheetRenameFromCell()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("template")
Set sh2 = Sheets("point")
For Each c In sh2.Range("B6", sh2.Cells(Rows.Count, 2).End(xlUp))
sh1.Copy After:=Sheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = c.Value:
On Error GoTo 0
Next
End Sub
but i have no idea how can i copy value from "point" Sheet Column C and D values 1 & 51 to sheet "a" Column C2 and F2 and so on但我不知道如何将值从“点”表列 C 和 D 值 1 和 51 复制到表“a”列 C2 和 F2 等等
a values in sheet "a" b values in sheet "b" and on a 工作表“a”中的值 b 工作表“b”中的值及以上
awaiting your help等待你的帮助
B6:BLastRow
contains a valid value for naming a worksheet.它假定范围B6:BLastRow
的每个单元格都包含用于命名工作表的有效值。 It also assumes that each of the worksheets to be created does not exist already.它还假定要创建的每个工作表都不存在。Option Explicit
Sub CopySheetRenameFromCell()
' Reference the workbook ('wb').
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Reference the source worksheet ('sws').
Dim sws As Worksheet: Set sws = wb.Worksheets("Point")
' Calculate the source last row ('slRow'),
' the row of the last non-empty cell in the column.
Dim slRow As Long
slRow = sws.Cells(sws.Rows.Count, "B").End(xlUp).Row
' Reference the template worksheets ('tws').
Dim tws As Worksheet: Set tws = wb.Worksheets("Template")
' Declare additional variables.
Dim dws As Worksheet ' Current Destination (Copied) Worksheet
Dim sr As Long ' Current Row in the Source Worksheet
' Loop through the rows of the source worksheet.
For sr = 6 To slRow
' Create a copy of the template worksheet after the last sheet
' in the workbook.
tws.Copy After:=wb.Sheets(wb.Sheets.Count)
' Reference this copy, the destination worksheet,
' which is the last (work)sheet in the workbook.
Set dws = wb.Sheets(wb.Sheets.Count)
' Rename the destination worksheet.
dws.Name = sws.Cells(sr, "B").Value
' Write the values from columns 'C' and 'D' in the row ('r')
' of the source worksheet to the cells `C2` and `F2` respectively
' in the destination worksheet.
dws.Range("C2").Value = sws.Cells(sr, "C").Value
dws.Range("F2").Value = sws.Cells(sr, "D").Value
Next sr
' Save the workbook.
'wb.Save
' Inform.
MsgBox "Point worksheets created.", vbInformation
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.