VBA-工作表更改事件上未更新工作表单元格值

时间:2019-03-06 10:26:32

标签: excel button copy range

我正在尝试从另一个工作表中引用与E行中的名称相关的单元格...我正在使用插入功能,工作表复制功能以及所有其他功能。但是-我在wsMASTER上的锁定单元格上具有“间接”公式,并在“ shtNAME”上引用了单元格-我想要的是wsMASTER上的单元格使用动态“ shtNAME”的工作表中的信息进行更新...希望这可以解释一下有人可以协助吗?

wsMASTER -用于添加具有给定InputBox值的新行和复印表的按钮。...

Private Sub NewServer_Click()

Dim shtName As String, ws As Worksheet
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wsGENERAL As Worksheet
Dim shNAMES As Range, nmANCHOR As Range
Dim eRow As Long, wasVISIBLE As Boolean
Dim shANCHOR As Range
    'Get name for new sheet
With ThisWorkbook
    Set wsMASTER = .Sheets("Main")
    Set wsTEMP = .Sheets("ServerTemplate")
    Set nmANCHOR = wsMASTER.Range("E10:E" & Rows.Count).End(xlUp).Offset(1)
'set ServerTemplate to visible
    wasVISIBLE = (wsTEMP.Visible = xlSheetVisible)
    If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible
'Turn off updates, alerts and events
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With
'prompt for user to enter new server name
    wsMASTER.Unprotect Password:="J786djh$"
    Do
        wsMASTER.Activate
        shtName = Application.InputBox("Please enter the hostname of the new server..." & vbCrLf & vbCrLf & _
                 "Click Cancel To Quit", "Define Hostname", Type:=2)
                 If shtName = "False" Then Exit Sub
        'Check for existing sheet name
        Set ws = Nothing
        On Error Resume Next
        'set variable to input name
            Set ws = Sheets(shtName)
        On Error GoTo 0
        If ws Is Nothing Then Exit Do
        'incorrect input alert
        MsgBox "Please try again, ensuring no spaces are used in the new server name.", vbExclamation, "Name Exists"
    Loop

'add new row with input value
        eRow = wsMASTER.Range("E" & Rows.Count).End(xlUp).Row + 1
        wsMASTER.Cells(eRow, "E").Value = shtName
'add worksheet and rename it
        wsTEMP.Copy After:=Worksheets(Sheets.Count)
        ActiveSheet.Name = shtName
'Create hyperlink
        Set shANCHOR = wsMASTER.Range("E" & Rows.Count).End(xlUp)
            wsMASTER.Hyperlinks.Add anchor:=shANCHOR, Address:="", SubAddress:="'" & shtName & "'!A1", TextToDisplay:=shtName
'delete any copies of the Server Template
        For Each wsGENERAL In ThisWorkbook.Worksheets
        If wsGENERAL.Name = "ServerTemplate(1)" Then
            wsGENERAL.Delete
        End If
        Next wsGENERAL
'back to Main Sheet
wsMASTER.Activate
're-enable screen updating
    'Turn off updates, alerts and events
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
    End With
wsMASTER.Protect Password:="J786djh$"
End With
'set ServerTemplate to hidden
        If wasVISIBLE Then wsTEMP.Visible = xlSheetHidden Else: If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden

End Sub

wsMASTER-引用复制的wsTEMP工作表上的单元格的公式。 E67是公式所在的行。

=IF(E67="","",IF(INDIRECT(E67 & "!B93" )="","",IF(INDIRECT(E67 & "!B93" )="Yes","Yes","No")))

shtNAME(动态)WorkSheetChange函数...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim wsMASTER As Worksheet, wsTEMP As Worksheet
With ThisWorkbook
Set wsMASTER = .Sheets("Main")
Set wsTEMP = .Sheets("ServerTemplate")
Target = wsTEMP
wsMASTER.Unprotect Password:="J786djh$"
With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With
    wsTEMP.Unprotect Password:="J786djh$"
    ActiveSheet.Calculate
    wsMASTER.Calculate
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
    End With
    wsTEMP.Protect Password:="J786djh$"
wsMASTER.Protect Password:="J786djh$"
End Sub

0 个答案:

没有答案