Update/Edit Data - VBA Excel

烂漫一生 提交于 2020-03-24 14:17:01

问题


enter image description hereI have looked through the pages and cannot seem to find an answer. Any help is greatly appreciated. I have gotten the code to work for updating the data in the sheet from from VBA form however it just keeps writing over the top row and does not edit the specific rows data. I am trying to get it to edit the data that is showing and not overwrite the top lines data. any help is appreciated. The code I am using is:

Private Sub cmdupdate_Click()

 Dim rowselect As Single
 rowselect = rowselect + 2
 Rows(rowselect).Select

 Cells(rowselect, 1) = Me.txtname.Value
 Cells(rowselect, 2) = Me.txtposition.Value
 Cells(rowselect, 3) = Me.txtassigned.Value
 Cells(rowselect, 4) = Me.cmbsection.Value
 Cells(rowselect, 5) = Me.txtdate.Value
 Cells(rowselect, 7) = Me.txtjoint.Value
 Cells(rowselect, 8) = Me.txtDAS.Value
 Cells(rowselect, 9) = Me.txtDEROS.Value
 Cells(rowselect, 10) = Me.txtDOR.Value
 Cells(rowselect, 11) = Me.txtTAFMSD.Value
 Cells(rowselect, 12) = Me.txtDOS.Value
 Cells(rowselect, 13) = Me.txtPAC.Value
 Cells(rowselect, 14) = Me.ComboTSC.Value
 Cells(rowselect, 15) = Me.txtTSC.Value
 Cells(rowselect, 16) = Me.txtAEF.Value
 Cells(rowselect, 17) = Me.txtPCC.Value
 Cells(rowselect, 18) = Me.txtcourses.Value
 Cells(rowselect, 19) = Me.txtseven.Value
 Cells(rowselect, 20) = Me.txtcle.Value

 End Sub

回答1:


replace following part for rowselect..

Dim rowselect As Integer
If Range("A:A").Find(Me.txtname.Value) Is Nothing Then
    Msg = "The Text Name could not be found in Column A" & _
    vbLf & "Do you want to create a new record?"    ' Define message.
    Style = vbYesNo + vbCritical + vbDefaultButton1 ' Define buttons.
    Title = "CAUTION"   ' Define title.
    Response = MsgBox(Msg, Style, "CAUTION")  '(Msg, Style, Title)
    If Response = vbYes Then
    rowselect = Range("A" & Rows.Count).End(xlUp).Row + 1
    'This will give you number of the first blank row below you table in Column A 
    Else: Exit Sub
    End If
Else
rowselect = Range("A:A").Find(Me.txtname.Value).Row
End If

' and then all the following shall work correctly each time you cilck the button .. if the form has all those fields
Cells(rowselect, 1) = Me.txtname.Value
Cells(rowselect, 2) = Me.txtposition.Value
Cells(rowselect, 3) = Me.txtassigned.Value ' and so on


来源:https://stackoverflow.com/questions/60649098/update-edit-data-vba-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!