Ich habe diese Frage aus der ersten Buchung bearbeitet, seit ich erkannte, dass kein Makro die Worksheet_change-Funktion aktivieren wird.execute Worksheet_Change, wenn Zelle von einem Makro geändert wurde
Ich benutze ein UserForm, um ein Makro zum Bearbeiten einer Zelle zu erstellen. Dann möchte ich, dass das Arbeitsblatt den Wert aus einer Zelle übernimmt und Werte in anderen Zellen erstellt. Dies funktioniert manuell, aber nicht über Makro!
Von der Userform:
Sub WriteOperatingFunds(dates, description, money)
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Worksheets("OperatingFunds")
'find first empty row in database
irow = ws2.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws2.Cells(irow, 1).value = dates
ws2.Cells(irow, 2).value = description
ws2.Cells(irow, 3).value = money
End Sub
Und aus dem Arbeitsblatt:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim change As String
Dim chngRow As Long
Dim IncRow As Long
Dim ExpRow As Long
Dim TotRow As Long
Dim Income As Long
Dim Expense As Long
Dim Total As Long
Set ws = ThisWorkbook.Worksheets("OperatingFunds")
TotRow = ws.Cells(ws.Rows.Count, 5).End(xlUp).Row 'finds bottom of 'Total' Column
'looks for target in range
If Not Application.Intersect(Target, ws.Range("C3", ws.Cells(TotRow + 1, 4))) Is Nothing Then
change = Target.Address(False, False)
chngRow = ws.Range(change).Row
'Get the last rows of range columns
IncRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
ExpRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
Application.EnableEvents = False 'to prevent endless loop & does not record changes by macro
'if Total column is empty
If ws.Cells(chngRow, 5) = "" Then
Income = Application.WorksheetFunction.Sum(ws.Range("C3", ws.Cells(TotRow + 1, 3)))
Expense = Application.WorksheetFunction.Sum(ws.Range("D3", ws.Cells(TotRow + 1, 4)))
Total = Income + Expense
ws.Cells(chngRow, 5) = Total
'if total column is not empty (i.e. needs to be rewritten)
ElseIf ws.Cells(chngRow, 5) <> "" Then
Income = Application.WorksheetFunction.Sum(ws.Range("C3", ws.Cells(chngRow, 3)))
Expense = Application.WorksheetFunction.Sum(ws.Range("D3", ws.Cells(chngRow, 4)))
Total = Income + Expense
ws.Cells(chngRow, 5) = Total
End If
Else
MsgBox "Else is thrown."
Exit Sub
End If
Application.EnableEvents = True 'so that future changes can be read
End Sub
Ich bin mir nicht sicher, ob ich Ihre Erklärung verstehe. Auf welchem Arbeitsblatt befindet sich der 'Worksheet_Change'-Code? – Comintern
Ich habe nicht den vollständigen Code gesehen, aber ich denke, Sie brauchen 'Private Sub Workbook_SheetChange (ByVal Sh als Objekt, ByVal Ziel als Bereich)' anstelle von 'Private Sub Worksheet_Change (ByVal Ziel als Bereich)' zu beiden Seiten gerecht zu werden. Dieser Code wird in 'ThisWorkbook' liegen –
Das Arbeitsblatt, wo" Worksheet_Change "Code in .worksheets (" OperationFund ") ist, aber ich habe vor kurzem einen Button zu diesem Blatt gemacht und nicht einmal ein Makro von diesem Blatt löst die Änderung aus. – JoeShiro