2016-12-08 104 views
0

我的問題是與列「A」。在這一列中,今天的日期應該粘貼在第一個空白行中。但是我粘貼了兩次。第一次顯示UserForm1時,第二次單擊CommandButton1時。一切工作正常(從UserForm1數據粘貼到最後一行+ 1的問題是隻能用「A」欄 請找到下面的代碼:。值被粘貼兩列

Private Sub CommandButton1_Click() 


Call UserForm_Initialize 

Unload Me 
End Sub 


Private Sub UserForm_Initialize() 

Dim LastRow As Long, ws As Worksheet 

'UserForm1.Label9.Caption = ws.Range("A1").Value 

Set ws = Sheets("Log") 

With ComboBox1 
    .AddItem "Quality" 
    .AddItem "Time" 
    .AddItem "Money" 
End With 


LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'ostatni wiersz 


'entry date 
    ws.Range("A1").Copy 
     Sheets("Log").Range("A" & LastRow).PasteSpecial xlPasteValues 

'pasting data from form 
'ws.Range("A" & LastRow).Value = Label9.Caption 
ws.Range("B" & LastRow).Value = TextBox8.Text 
ws.Range("C" & LastRow).Value = TextBox1.Text 
ws.Range("D" & LastRow).Value = TextBox2.Text 
ws.Range("E" & LastRow).Value = TextBox3.Text 
ws.Range("F" & LastRow).Value = TextBox4.Text 
ws.Range("G" & LastRow).Value = TextBox5.Text 
ws.Range("H" & LastRow).Value = ComboBox1.Text 
ws.Range("I" & LastRow).Value = TextBox6.Text 
ws.Range("J" & LastRow).Value = TextBox7.Text 
End Sub 

請幫

回答

0

UserForm_Initialize()執行每一次你打開用戶表單,每次顯示這個用戶表單,然後當你點擊CommandButton1時,你正在執行UserForm_Initialize()的整個代碼,你只在A列中看到一個問題,因爲每個TextBox都是空的,所以你的程序填充剩餘的單元格,這裏的解決方案將分開初始化Userform的代碼部分和將值複製到單元格的部分。你可以這樣做:

Private Sub CommandButton1_Click() 
    CopyToCells 
    Unload Me 
End Sub 

Private Sub UserForm_Initialize() 
    With ComboBox1 
     .AddItem "Quality" 
     .AddItem "Time" 
     .AddItem "Money" 
    End With 
End Sub 

Sub CopyToCells() 
    Dim LastRow As Long, ws As Worksheet 
    Set ws = Sheets("Log") 

    LastRow = ws.Range("A" & Rows.Count).End(xlUp).row + 1 'ostatni wiersz 

    'entry date 
    ws.Range("A1").Copy 
    Sheets("Log").Range("A" & LastRow).PasteSpecial xlPasteValues 

    'pasting data from form 
    ws.Range("B" & LastRow).Value = TextBox8.Text 
    ws.Range("C" & LastRow).Value = TextBox1.Text 
    ws.Range("D" & LastRow).Value = TextBox2.Text 
    ws.Range("E" & LastRow).Value = TextBox3.Text 
    ws.Range("F" & LastRow).Value = TextBox4.Text 
    ws.Range("G" & LastRow).Value = TextBox5.Text 
    ws.Range("H" & LastRow).Value = ComboBox1.Text 
    ws.Range("I" & LastRow).Value = TextBox6.Text 
    ws.Range("J" & LastRow).Value = TextBox7.Text 
End Sub 
+0

非常感謝你的詳細信息。現在我明白它是如何與對方進行溝通的,以及爲什麼會這樣做。你很棒! – Igor

+0

沒問題,pozdrowienia z Poznania;) – Limak