代码之家  ›  专栏  ›  技术社区  ›  Philipp Rosengart

工作表_代码生成工作表的更改方法

  •  1
  • Philipp Rosengart  · 技术社区  · 9 年前

    我正在通过代码创建工作表并添加一些值。 现在我想检查C列中的值是否已经改变,并想改变D列的值。 我找到了子工作表_Change来执行此操作。但是这个方法不适用于我创建的工作表,它适用于我来自的工作表。 有人能帮我吗?

    我正在使用ws将工作表设置为活动。激活,但它不能像我希望的那样工作。

    Sub Test()
    
    Dim monat As Integer
    Dim jahr As Integer
    Dim tag As Integer
    Dim anzahlTage As Integer
    Dim ws As Worksheet
    Dim kalenderTag As Date
    
    On Error GoTo Fehler
    
    jahr = Worksheets("Kalender erstellen").Cells(2, 2).Value
    monat = Worksheets("Kalender erstellen").Cells(2, 1).Value
    
    anzahlTage = DateSerial(jahr, monat + 1, 1) _
               - DateSerial(jahr, monat, 1)
    
    Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = MonthName(monat) + " " + CStr(jahr)
    
    ws.Cells(1, 1) = "Datum"
    ws.Cells(1, 2) = "Wochentag"
    ws.Cells(1, 3) = "Beginn"
    ws.Cells(1, 4) = "Ende"
    ws.Cells(1, 5) = "Stunden"
    ws.Cells(1, 6) = "Über-/Unterstunden"
    ws.Cells(1, 8) = "Stunden gesamt"
    ws.Cells(1, 9) = "Urlaub gesamt"
    
    ws.range("A1", "I33").HorizontalAlignment = xlCenter
    ws.range("A1", "I1").Font.FontStyle = "Bold"
    ws.Columns("B").ColumnWidth = 20
    ws.Columns("F").ColumnWidth = 20
    ws.Columns("H").ColumnWidth = 25
    ws.Columns("I").ColumnWidth = 25
    ws.range("A2", "I2").MergeCells = True
    
    ws.Activate
    
    For tag = 1 To anzahlTage
    
    kalenderTag = DateSerial(jahr, monat, tag)
    ws.Cells(tag + 2, 1) = kalenderTag
    ws.Cells(tag + 2, 2) = Format$(kalenderTag, "dddd")
    
    Next tag
    
    'Dim rng As range
    'Set rng = ActiveSheet.range("A1", "F1")
    'With rng.Borders
    '.LineStyle = xlContinous
    '.Color = vbBlack
    '.Weight = xlThin
    'End With
    
    'MsgBox (anzahlTage)
    
    Exit Sub
    Fehler:
    
    MsgBox "FehlerNr.: " & Err.Number & vbNewLine & vbNewLine _
        & "Beschreibung: " & Err.Description _
        , vbCritical, "Fehler"
    
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As range)
    
    If Not Application.Intersect(Target, range("C3", "C33")) Is Nothing Then
    MsgBox ("TEST")
    
    End If
    
    End Sub
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Gary's Student    9 年前

    要使代码在新创建的工作表中工作,必须将其插入新工作表的工作表代码中。


    更容易 .Copy 已安装宏的现有工作表 .Add 新的工作表。

    你得到了额外的好处 .Copied 工作表可以有预格式化的列、行、标题等。

        2
  •  0
  •   Pierre    9 年前

        3
  •  0
  •   Darren Bartrup-Cook    9 年前

    另一种方法,尽管我仍然会说使用@Gary的学生答案,但使用 Workbook_SheetChange 事件

    将此代码添加到任何普通模块:

    Option Explicit
    Public SheetCodeName As String
    
    Sub CreateSheet()
        Dim wrkSht As Worksheet
    
        'Add the worksheet and remember the codename for it.
        Set wrkSht = ThisWorkbook.Worksheets.Add
        SheetCodeName = wrkSht.CodeName
    End Sub
    

    ThisWorkBook 模块:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Sh.CodeName = SheetCodeName Then
            If Not Application.Intersect(Target, Range("C3", "C33")) Is Nothing Then
                MsgBox ("TEST")
            End If
        End If
    End Sub
    

    正如我所说,Garys的答案更好,因为如果您添加了大量工作表(模板工作表会很快变得复杂)。