Динамически изменяемый код события изменения листа - PullRequest
0 голосов
/ 01 июля 2019

Я сделал исходную книгу, которую импортирую из нее в качестве шаблона.В шаблоне у меня есть некоторый код в событии изменения листа.Этот код использует имя листа.Когда я импортирую шаблон, я тоже меняю его имя.Я хочу обновить шаблон с новым именем листа.Это должно произойти автоматически с кодом.

Это код события;

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r1, r2, r3, trh, myMultipleRange As Range
Set r1 = ThisWorkbook.Sheets("DATA").Range("C:C")
Set r2 = ThisWorkbook.Sheets("DATA").Range("E:E")
Set r3 = ThisWorkbook.Sheets("DATA").Range("G:G,H:H")
Set trh = ThisWorkbook.Sheets("DATA").Range("V:V")
Set myMultipleRange = Union(r1, r2, r3)

If Not Intersect(Target, myMultipleRange) Is Nothing Then
lastrow = ThisWorkbook.Sheets("DATA").Cells(Rows.count, 2).End(xlUp).Row
For xsay = 2 To lastrow
    Cells(xsay, "F") = Cells(xsay, "C") * Cells(xsay, "E")
    Cells(xsay, "I") = Cells(xsay, "F") * Cells(xsay, "G")
    Cells(xsay, "J") = Cells(xsay, "F") - Cells(xsay, "I")
    Cells(xsay, "K") = Cells(xsay, "J") * Cells(xsay, "H")
    Cells(xsay, "L") = Cells(xsay, "J") + Cells(xsay, "K")

    If Cells(xsay, "AA") = "STG" Then
        Range("E" & xsay & ":F" & xsay).NumberFormat = "[$£-809]#,##0.00"
        Range("I" & xsay & ":M" & xsay).NumberFormat = "[$£-809]#,##0.00"
    ElseIf Cells(xsay, "AA") = "DOLAR" Then
        Range("E" & xsay & ":F" & xsay).NumberFormat = "[$$-409]#,##0.00"
        Range("I" & xsay & ":M" & xsay).NumberFormat = "[$$-409]#,##0.00"
    ElseIf Cells(xsay, "AA") = "EURO" Then
        Range("E" & xsay & ":F" & xsay).NumberFormat = "[$€-2] #,##0.00"
        Range("I" & xsay & ":M" & xsay).NumberFormat = "[$€-2] #,##0.00"
    End If

    If Cells(xsay, "Z") <> 0 Then
        Var = xsay + (Cells(xsay, "Z") - 1)
        Cells(xsay, "M").Formula = "=SUM(L" & xsay & ":L" & Var & ")"
    End If

    Next xsay
End If

End Sub

Это код импорта;

Sub yeni_sayfa_ekle()

Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet

'Turns off screenupdating and events:
Application.ScreenUpdating = False
Application.EnableEvents = False

'Sets the variables:
Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open(WB.Path & "\source.xlsm")  'Modify to match

'Copies each sheet of the SourceWB to the end of original wb:
For Each WS In SourceWB.Worksheets
If WS.Name = "DATA" Then
WS.Copy after:=WB.Sheets(WB.Sheets.Count)
ism = usrSiparis.cmbFirm.Value
WB.Sheets("DATA").Name = ism
WB.Sheets(ism).Visible = xlSheetHidden
End If
Next WS

SourceWB.Close savechanges:=False
Set WS = Nothing
Set SourceWB = Nothing

WB.Activate
ASheet.Select
Set ASheet = Nothing
Set WB = Nothing

Application.EnableEvents = True

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...