简体   繁体   中英

How do I modify this vba to copy column width and conditional formatting to an active sheet?

I am new to VBA.

Thank you for your time. I have been Googling for 2 days and always get an error.

I have two sheets

  1. Projects ( where I will store project names) and
  2. Template (where new projects will be created using the "template" sheet)

I have 2 issues I am trying to solve:

  1. How do I copy the format on an active sheet including conditional formatting and column width. PasteSpecial already copies all the colour design but not the column width/conditional formatting

  2. When I run the code it creates a new sheet called Project Name,not sure where that is coming from.

This is the code I am using:

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

<<<<<<<<<<<<<<<<<<<<<<

I want to generate a project name, make sure it does not exist(no duplicate), open a new sheet and copy the template from "template".

The full codes is:

RunAll()
CreateProjectName
CreateNewTab
CopyPaste
End Sub
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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM