如何将列宽和条件格式复制到活动工作表?

问题描述 投票:0回答:1

我有两张纸

  1. 项目(我将在其中存储项目名称)和
  2. 模板(将使用“模板”表创建新项目)

我有两个问题:

  1. 如何复制活动工作表上的格式,包括条件格式和列宽?
    PasteSpecial 复制所有颜色设计,但不复制列宽/条件格式

  2. 当我运行代码时,它会创建一个名为“项目名称”的新工作表,但不确定它来自哪里。

Sub Copy()
Sheets("Template").Range("A1:O100").Copy
ActiveSheet.PasteSpecial
End Sub

我想生成一个项目名称,确保它不存在(不重复),打开一个新工作表并从“模板”中复制模板。

完整代码:

RunAll()
CreateProjectName
CreateNewTab
CopyPaste
End Sub


Sub CreateProjectName()
Dim AddData As Range
Dim AddName As String
Set AddData = Cells(Rows.Count, 4).End(xlUp).Offset(1, 0)
AddName = InputBox("Enter Project Name do not input manually", "Project Monitor")
If AddName = "" Then Exit Sub
AddData.Value = AddName
AddData.Offset(0, 1).Value = Now
End Sub


Function SheetCheck(sheet_name As String) As Boolean
Dim ws As Worksheet
SheetCheck = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Name = sheet_name Then
        SheetCheck = True
    End If
Next
End Function


Sub CreateNewTab()
Dim sheets_count As Integer
Dim sheet_name As String
Dim i As Integer
sheet_count = Range("D3:D1000").Rows.Count
For i = 1 To sheet_count
    sheet_name = Sheets("Projects").Range("D3:D1000").Cells(i, 1).Value
    If SheetCheck(sheet_name) = False And sheet_name <> "" Then
        Worksheets.Add(After:=Sheets("Projects")).Name = sheet_name
    End If
Next i
End Sub


Sub CopyPaste()
Sheets("Template").Range("A1:o100").Copy
ActiveSheet.PasteSpecial
End Sub
excel vba copy-paste vba7
1个回答
1
投票
Option Explicit

Sub AddProject()
   
    Dim ws As Worksheet, NewName As String
    NewName = InputBox("Enter Project Name do not input manually", "Project Monitor")
   
    ' checks
    If NewName = "" Then
        MsgBox "No name entered", vbCritical
        Exit Sub
    Else
        ' check sheet not existing
        For Each ws In ThisWorkbook.Sheets
            If UCase(ws.Name) = UCase(NewName) Then
                MsgBox "Existing Sheet '" & ws.Name & "'", vbCritical, "Sheet " & ws.Index
                Exit Sub
            End If
        Next
    End If
   
    ' check not existing in list
    Dim wb As Workbook, n As Long, lastrow As Long, v
   
    Set wb = ThisWorkbook
    With wb.Sheets("Projects")
        lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row
        v = Application.Match(NewName, .Range("D1:D" & lastrow), 0)
        ' not existing add to list
        If IsError(v) Then
            .Cells(lastrow + 1, "D") = NewName
            .Cells(lastrow + 1, "E") = Now
        Else
            MsgBox "Existing Name '" & NewName & "'", vbCritical, "Row " & v
            Exit Sub
        End If
    End With
   
    ' create sheet
    n = wb.Sheets.Count
    wb.Sheets("Template").Copy after:=wb.Sheets(n)
    wb.Sheets(n + 1).Name = NewName
    MsgBox NewName & " added as Sheet " & n + 1, vbInformation
   
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.