所以我创建了一个带有文本框和一个按钮的工作表。目标是将信息输入相应的文本框,并填充不同工作表上的数据库。我已设法创建有效的代码但有一个小问题。每次我点击按钮,它会使屏幕闪烁,同时复制数据。它确实有效,但我想知道是否有人可以看到一种方法来阻止屏幕闪烁。我认为从每个文本框复制数据时都会发生这种情况。一个文本框用于一个闪烁或类似的东西。试图写一个循环,但无法弄清楚如何让不同的文本框循环。
使用的代码如下:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Sheets("database")
ActiveWorkbook.Sheets("database").Activate
ws.Range("A1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox1.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("B1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox2.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("C1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox3.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("D1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox4.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("E1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox5.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("F1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox6.Value
ActiveWorkbook.Sheets("database").Activate
ws.Range("G1").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = TextBox7.Value
End Sub
答案 0 :(得分:0)
MRig的评论可以防止您提到的闪烁,但您可能只想考虑不使用.Select
和ActiveCell
语句。它们可能不可靠和缓慢。
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.WorkSheets("database")
Dim lastRow
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' gets last row in A
ws.Range("A" & lastRow + 1).Value = TextBox1.Value
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' gets last row in B
ws.Range("B" & lastRow + 1).Value = TextBox2.Value
' ... and so on until
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row ' gets last row in G
ws.Range("G" & lastRow + 1).Value = TextBox7.Value
End Sub
这将保存所有循环并选择您正在执行的操作,因此它会更快,并且无论您是否包含Application.ScreenUpdating
屏幕都不会闪烁。
答案 1 :(得分:0)
已编辑以添加ActiveX文本框的解决方案
根据您的实际文本框名称和范围来填充值,您可以这样做:
工作表解决方案" ActiveX"文本框(代码进入工作表"数据库"代码窗格)
Option Explicit
Private Sub CommandButton1_Click()
Dim iTB As Long
For iTB = 1 To 7 '<--| just change "7" to your actual numebre of textboxes
Cells(Rows.Count, Range("A1").Offset(, iTB - 1)).End(xlUp).Offset(1).Value = OLEObjects("TextBox" & iTB).Object.Value
Next iTB
End Sub
Userform文本框的解决方案(代码位于用户窗体代码窗格中)
Option Explicit
Private Sub CommandButton1_Click()
Dim iTB As Long
With ThisWorkbook.Worksheets("database")
For iTB = 1 To 7 '<--| just change "7" to your actual number of textboxes
.Cells(.Rows.Count, .Range("A1").Offset(, iTB - 1)).End(xlUp).Offset(1).Value = Me.Controls("TextBox" & iTB).Value
Next iTB
End With
End Sub