简体   繁体   English

Excel VBA“更新”功能

[英]Excel VBA 'Updating' Function

I'm having trouble getting this code to execute.我无法执行此代码。 The purpose of the code is to 'Update' an existing row in a database spreadsheet, based on data input from a userform in Excel.该代码的目的是根据从 Excel 中的用户表单输入的数据“更新”数据库电子表格中的现有行。 Because there are so many rows in the database spreadsheet, I wrote the code to first copy data to an empty spreadsheet and then move it to the database spreadsheet..however, when I run this code I keep getting the error message that's set to show at the end of the code.因为数据库电子表格中有这么多行,所以我编写了代码,首先将数据复制到一个空电子表格,然后将其移动到数据库电子表格中……但是,当我运行此代码时,我不断收到设置为显示的错误消息在代码的末尾。 Do you have any insight into where my errors are?你对我的错误在哪里有任何见解吗?

Private Sub UpdateButton_Click()
Application.ScreenUpdating = False 'new
Dim copySheet As Worksheet 'new
Dim pasteSheet As Worksheet 'new
Dim f As Range, v

v = Trim(Me.TextBox1.Value)

Set copySheet = Worksheets("COPY AREA") 'NEW
Set pasteSheet = Worksheets("Database Entry Sheet") 'new

copySheet.Activate 'new

If Len(v) = 0 Then
     Msgbox "Run number cannot be blank", vbExclamation, "No"
     Exit Sub
End If

 Set f = ActiveSheet.Range("A:A").Find(What:=v, _
                                      lookat:=xlWhole)
 If Not f Is Nothing Then
     If Msgbox("Are you sure you want to update the record?", _
                  vbYesNo + vbQuestion, "UpdateRecord") = vbYes Then
 
 'Transfer information
        Cells(4, 1).Value = TextBox1.Value
        Cells(4, 2).Value = textbox2.Value
        Cells(4, 3).Value = textbox3.Value
        Cells(4, 4).Value = textbox4.Value
        Cells(4, 5).Value = textbox5.Value
        Cells(4, 6).Value = textbox6.Value
        Cells(4, 7).Value = textbox7.Value
        Cells(4, 8).Value = textbox8.Value
        Cells(4, 9).Value = textbox9.Value
        Cells(4, 11).Value = textbox11.Value
        Cells(4, 12).Value = textbox12.Value
        Cells(4, 13).Value = textbox13.Value
        Cells(4, 14).Value = textbox14.Value
        Cells(4, 17).Value = textbox17.Value
        Cells(4, 18).Value = textbox18.Value
        Cells(4, 19).Value = textbox19.Value
        Cells(4, 20).Value = textbox20.Value
        Cells(4, 21).Value = textbox21.Value
        Cells(4, 25).Value = textbox25.Value
        Cells(4, 26).Value = textbox26.Value
        Cells(4, 27).Value = textbox27.Value
        Cells(4, 28).Value = textbox28.Value
        Cells(4, 29).Value = textbox29.Value
        Cells(4, 33).Value = textbox33.Value
        Cells(4, 34).Value = textbox34.Value
        Cells(4, 35).Value = textbox35.Value
        Cells(4, 36).Value = textbox36.Value
        Cells(4, 37).Value = textbox37.Value
        Cells(4, 41).Value = textbox41.Value
        Cells(4, 42).Value = textbox42.Value
        Cells(4, 43).Value = textbox43.Value
        Cells(4, 44).Value = textbox44.Value
        Cells(4, 45).Value = textbox45.Value
    
        copySheet.Range("A4:BC4").Copy
            pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
          
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        
        pasteSheet.Activate
            
          With f.EntireRow
              .Cells(1) = v  
              .Cells(2).value = textbox2.Value
              .Cells(3).Value = textbox3.Value
              .Cells(4).Value = textbox4.Value
              .Cells(5).Value = textbox5.Value
              .Cells(6).Value = textbox6.Value
              .Cells(7).Value = textbox7.Value
              .Cells(8).Value = textbox8.Value
              .Cells(9).Value = textbox9.Value
              .Cells(11).Value = textbox11.Value
              .Cells(12).Value = textbox12.Value
              .Cells(13).Value = textbox13.Value
              .Cells(14).Value = textbox14.Value
              .Cells(17).Value = textbox17.Value
              .Cells(18).Value = textbox18.Value
              .Cells(19).Value = textbox19.Value
              .Cells(20).Value = textbox20.Value
              .Cells(21).Value = textbox21.Value
              .Cells(25).Value = textbox25.Value
              .Cells(26).Value = textbox26.Value
              .Cells(27).Value = textbox27.Value
              .Cells(28).Value = textbox28.Value
              .Cells(29).Value = textbox29.Value
              .Cells(33).Value = textbox33.Value
              .Cells(34).Value = textbox34.Value
              .Cells(35).Value = textbox35.Value
              .Cells(36).Value = textbox36.Value
              .Cells(37).Value = textbox37.Value
              .Cells(41).Value = textbox41.Value
              .Cells(42).Value = textbox42.Value
              .Cells(43).Value = textbox43.Value
              .Cells(44).Value = textbox44.Value
              .Cells(45).Value = textbox45.Value
          End With
      End If
 Else
     Msgbox "not found - '" & v & "'"
 End If

End Sub结束子

I think you don't need to copy the data first to copysheet and then paste the data in the database sheet..You just can take the data in database sheet.我认为您不需要先将数据复制到copysheet然后将数据粘贴到数据库表中..您只需将数据库表中的数据即可。 And you should use a for loop for taking the data( As you have 45 textbox)..And i don't understand that what is the purpose of the f.entirerow ....(this section)?并且您应该使用 for 循环来获取数据(因为您有 45 个文本框)。我不明白 f.entirerow ....(本节)的目的是什么? Sorry i want to comment in the post but i don't have enough reputation..抱歉,我想在帖子中发表评论,但我没有足够的声誉..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM