скопированный лист с пользовательской формой не будет вставлять данные в новую пользовательскую форму - PullRequest
0 голосов
/ 06 мая 2020

У меня проблема с пользовательской формой, скопированной на новый лист, чтобы вставить данные в новый лист. код пользовательской формы:

Sub userform_initialize()
Dim LastColumn As Long
Dim aCell As Range
' dont know why this code, but helps the named ranges
    With Worksheets("MasterData")
        For i = 1 To LastColumn
            With .Columns(i)
            LastRow = Lookup.Cells(Rows.Count, i).End(xlUp).Row
                With Range(Cells(1, i), Cells(LastRow, i))
                Range(Cells(1, i), Cells(LastRow, i)).Select
                Selection.CreateName Top:=True
                End With
            End With
        Next i
    End With
' defining the input to the combobox and lists from masterdata
    Me.ComboBoxName.RowSource = "Name"
    Me.ComboBoxTMS.RowSource = "TMS"
    Me.ComboBoxOtherT.RowSource = "Other_T"
    Me.ListBoxPlacement.RowSource = "Placement"



End Sub


Sub CommandSave_click()

' mandatory for picking name in userform
    If ComboBoxName.Value = "" Then
    MsgBox "You must select a name"
    Cancel = True
    Exit Sub
    End If

'mandatory for writing hours in userform
    If TextBoxHours.Value = "" Then
    MsgBox "You need to type in hours"
    Cancel = True
    Exit Sub
    End If

'mandatory of either TMS or Other Timeregistration in userform
    If ComboBoxOtherT.Value = "" Then
    If ComboBoxTMS.Value = "" Then
    MsgBox "You must select TMS or Other Timeregistration"
    Cancel = True
    Exit Sub
    End If
    End If

'manadatory for picking one in the listbox with placement for userform
    If Not IsAnythingSelected(ListBoxPlacement) Then
        MsgBox "Please select placement of workday"
        Exit Sub
    End If

' calling the macro that inserts the data in the correct coloumns and rows.
    Call BSave

' Clear Values
    ComboBoxName.Value = ""
    ComboBoxTMS.Value = ""
    ComboBoxOtherT.Value = ""
    TextBoxHours.Value = ""
    TextBoxComments.Value = ""
    ListBoxPlacement.Value = ""

End Sub

Private Sub BSave()
    Dim LastRow As Long, ws As Worksheet

    Set ws = Sheets("McLys_Timeregistration") ' declaring which worksheet we are working on

    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

    ws.Range("B" & LastRow).Value = ComboBoxName.Text 'Adds into Col A & Last Blank Row
    ws.Range("C" & LastRow).Value = DTPicker1.Value 'Adds into Col B & Last Blank Row
    ws.Range("D" & LastRow).Value = TextBoxHours.Value 'Adds into Col C & Last Blank Row
    ws.Range("E" & LastRow).Value = ComboBoxTMS.Text 'Adds into Col D & Last Blank Row
    ws.Range("F" & LastRow).Value = ComboBoxOtherT.Text 'Adds into Col E & Last Blank Row
    ws.Range("G" & LastRow).Value = TextBoxComments.Text 'Adds into Col F & Last Blank Row
    ws.Range("H" & LastRow).Value = ListBoxPlacement.Text 'Adds into Col G & Last Blank Row

End Sub

' function for making sure that at least one of the choices are chosen in the listbox
Function IsAnythingSelected(lBox As Control) As Boolean
    Dim i As Long
    Dim selected As Boolean
    selected = False
    For i = 1 To lBox.ListCount
        If lBox.selected(i) Then
            selected = True
            Exit For
        End If
    Next i
    IsAnythingSelected = selected
End Function

Sub CommandCancel_click()

    Unload Me ' making the userform closing

End Sub

Private Sub UserForm_Activate()
' every time the userform is activated it makes sure that its todays date there is in the calendar.
DTPicker1.Value = Date
End Sub

и то, что я вижу и пытался изменить только в коде ниже, упоминается имя листа

Private Sub BSave()
    Dim LastRow As Long, ws As Worksheet

    Set ws = Sheets("McLys_Timeregistration") ' declaring which worksheet we are working on

    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

    ws.Range("B" & LastRow).Value = ComboBoxName.Text 'Adds into Col A & Last Blank Row
    ws.Range("C" & LastRow).Value = DTPicker1.Value 'Adds into Col B & Last Blank Row
    ws.Range("D" & LastRow).Value = TextBoxHours.Value 'Adds into Col C & Last Blank Row
    ws.Range("E" & LastRow).Value = ComboBoxTMS.Text 'Adds into Col D & Last Blank Row
    ws.Range("F" & LastRow).Value = ComboBoxOtherT.Text 'Adds into Col E & Last Blank Row
    ws.Range("G" & LastRow).Value = TextBoxComments.Text 'Adds into Col F & Last Blank Row
    ws.Range("H" & LastRow).Value = ListBoxPlacement.Text 'Adds into Col G & Last Blank Row

End Sub

может ли кто-нибудь сказать мне, где проблема есть? если я изменил имя листа здесь, он все еще вставлял информацию в старый лист.

в листе упоминается только это для вызова пользовательской формы:

Sub shape_button()
UserForm1.Show
End Sub

Sub placeUser()
With UserForm1
  .StartUpPosition = 0
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With

End Sub

Заранее спасибо

...