2017-09-10 54 views
0

一些幫助,請 - 努力保持簡單更新ActiveRow Excel VBA中

我有一個用戶窗體這在組合框更新文本框從選擇。我希望能夠改變這些填充盒,然後用Update命令,改變工作表中的值,然後清除表單

Form Image

Sheet1 Image

我已經能夠創建添加數據的用戶窗體,但我努力讓VBA從控制框中選擇活動單元格和活動行...... ????

一旦我能得到VBA正確AvtiveCell,我可以使用偏移和變化/根據需要添加


Private Sub ComboBox1_Change() 
    With Me 
     .TextBox1.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 2) 
     .TextBox2.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 3) 
     .TextBox3.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 4) 
     .TextBox4.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 5) 
     .TextBox5.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 6) 
     .TextBox6.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 7) 
     .TextBox7.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 8) 
     .TextBox8.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 9) 
     .TextBox9.Value = Sheet1.Cells(.ComboBox1.ListIndex + 2, 10) 
    End With 
End Sub 

Private Sub CommandButton2_Click() 
    Unload Me 
End Sub 

Private Sub EditAddButton_Click() 
    EditAdd 
End Sub 

Private Sub UserForm_Initialize() 
    TextBox1.SetFocus 
End Sub 
+0

您可以粘貼您當前的用戶表單代碼嗎?我們需要看看你已經有多遠了。 –

+0

嗨克里斯,繼承人的代碼sofar –

回答

0

好了,你有autopopulation完成。現在您需要將代碼添加到您的EditAdd點擊子。大概你想用表單中的單元格值替換用戶表單中的單元格值。

你到目前爲止嘗試過什麼?我只是試着扭轉作業!例如,在該子,做這樣的事情:

Private Sub EditAddButton_Click() 
    Dim ComplaintNum as Integer 
    ComplaintNum = ComboBox1.ListIndex + 2 
    With Sheet1 
     .cells(ComplaintNum, 2) = TextBox1.Value 
     ' etc down the list 
    End With 
End Sub 

我沒有測試過這一點 - 但給它一個去玩弄它。您還需要處理添加新行,但我不會爲您編寫它:)

+0

謝謝克里斯,將嘗試它,這是我用來插入新行。任何改進?私人小組CommandButton2_Click() 昏暗WS作爲工作表:設置WS = Sheet 1中 ws.Activate 範圍( 「A1」)選擇 Selection.End(xlDown).Offset(1,0)。選擇 ActiveCell.Offset(。 0,1)= DateReceivedBox.Value ActiveCell.Offset(0,2)= ViaBox.Value –

+0

ActiveCell.Offset(0,3)= FromTextbox.Value ActiveCell.Offset(0,4)= Location1Box.Value ActiveCell。偏移(0,5)= Location2Box.Value ActiveCell.Offset(0,6)= RegNumberBox.Value ActiveCell.Offset(0,7)= VehicleMakeBox.Value ActiveCell.Offset(0,8)= VehicleColorBox.Value ActiveCell.Offset(0,9)= CommentsBox.Value ActiveCell.FormulaR1C1 = 「= + R [-1] C + 1」 範圍( 「A1」)。選擇 卸載我 NewComplaintForm.Show 結束子 –

+0

鑑於很難評價讀取代碼,我不會立即告訴這個插入代碼是否正常工作 - 但是如果它對你有效,那很好。雖然有一個改進:你幾乎不需要在代碼中「選擇」單元格。不要選擇然後使用ActiveCell。請直接參考單元格。 –