range1.value = range2.Value非常缓慢的VBA
我正在编写像NextCell.Value = datejour.value一样的简单代码,dateJour是位于工作簿中的单元格中的日期。
当我循环(大约100次)时,它需要永远,因为AddData过程中的每个NextCell.Value = dateJour.value语句需要0.2秒。
.range(NextCell.Offset(0,1),NextCell.Offset(0,8))。值= wssaisie.range(
“由Filldata过程调用,这是循环发生的地方。
它检查了用户填充的数据是否已经存在在称为“Données”的数据表中。如果不是,它将数据添加到表(通过调用AddData),如果是,则修改了数据(通过调用更改)。它逐行进行/逐个检查,因为有时必须添加或修改数据。
非常感谢您的帮助,以改善我的代码!
Public Sub FillData()
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
Dim lastRow As Long, lastColumn As Long
lastRow = wsSaisie.Range("A:H").Find("*" _
, LookAt:=xlPart _
, LookIn:=xlFormulas _
, SearchOrder:=xlByRows _
, SearchDirection:=xlPrevious).Row
Dim rowKey As String
Dim foundRowNumber As Long
Dim cell As Range
For Each cell In wsSaisie.Range(wsSaisie.Range("I5"), wsSaisie.Range("I" & lastRow))
rowKey = cell.Value
foundRowNumber = DataAlreadyExists(rowKey)
If foundRowNumber = -1 Then
Call AddData(cell.Row)
Else
Call ChangeData(foundRowNumber, cell.Row)
End If
Next cell
End Sub
Public Sub AddData(rowNumber As Long)
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
Dim dateJour As Range
Set dateJour = wsSaisie.Range("B1")
Dim nextCell As Range
Set nextCell = wsData.Range("A1048576").End(xlUp).Offset(1, 0)
'StartTime = Timer
nextCell.Value = dateJour.Value
'Debug.Print Round(Timer - StartTime, 2)
wsData.Range(nextCell.Offset(0, 1), nextCell.Offset(0, 8)).Value = wsSaisie.Range("A" & rowNumber, "H" & rowNumber).Value
End Sub
Public Sub ChangeData(rowTo As Long, rowFrom As Long)
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
wsData.Range("G" & rowTo & ":" & "I" & rowTo).Value = wsSaisie.Range("F" & rowFrom & ":" & "H" & rowFrom).Value
End Sub
Public Function DataAlreadyExists(key As String) As Long
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
If Not IsError(Application.Match(key, wsData.Range("K:K"), 0)) Then
DataAlreadyExists = Application.Match(key, wsData.Range("K:K"), 0)
Else
DataAlreadyExists = -1
End If
End Function
I'am writing simple code like nextCell.Value = dateJour.Value, were dateJour is a date located in a cell in the workbook.
When I loop (about 100 times) it takes forever because each nextCell.Value = dateJour.Value statement in the AddData procedure takes 0.2 seconds.
Same for .Range(nextCell.Offset(0, 1), nextCell.Offset(0, 8)).Value = wsSaisie.Range("A" & rowNumber, "H" & rowNumber).Value
The AddData procedure is called by fillData procedure and this is where the loop occurs.
It checks if the filled data by the user already exists in the data sheet called "Données". If not it adds data to the sheet (by calling AddData), if yes it modifies the data (by calling ChangeData). It goes/checks line by line because sometimes data has to be added or modified.
Thanks a lot for your help to improve my code !
Public Sub FillData()
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
Dim lastRow As Long, lastColumn As Long
lastRow = wsSaisie.Range("A:H").Find("*" _
, LookAt:=xlPart _
, LookIn:=xlFormulas _
, SearchOrder:=xlByRows _
, SearchDirection:=xlPrevious).Row
Dim rowKey As String
Dim foundRowNumber As Long
Dim cell As Range
For Each cell In wsSaisie.Range(wsSaisie.Range("I5"), wsSaisie.Range("I" & lastRow))
rowKey = cell.Value
foundRowNumber = DataAlreadyExists(rowKey)
If foundRowNumber = -1 Then
Call AddData(cell.Row)
Else
Call ChangeData(foundRowNumber, cell.Row)
End If
Next cell
End Sub
Public Sub AddData(rowNumber As Long)
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
Dim dateJour As Range
Set dateJour = wsSaisie.Range("B1")
Dim nextCell As Range
Set nextCell = wsData.Range("A1048576").End(xlUp).Offset(1, 0)
'StartTime = Timer
nextCell.Value = dateJour.Value
'Debug.Print Round(Timer - StartTime, 2)
wsData.Range(nextCell.Offset(0, 1), nextCell.Offset(0, 8)).Value = wsSaisie.Range("A" & rowNumber, "H" & rowNumber).Value
End Sub
Public Sub ChangeData(rowTo As Long, rowFrom As Long)
Dim wsSaisie As Worksheet
Set wsSaisie = ThisWorkbook.Worksheets("Saisie")
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
wsData.Range("G" & rowTo & ":" & "I" & rowTo).Value = wsSaisie.Range("F" & rowFrom & ":" & "H" & rowFrom).Value
End Sub
Public Function DataAlreadyExists(key As String) As Long
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Données")
If Not IsError(Application.Match(key, wsData.Range("K:K"), 0)) Then
DataAlreadyExists = Application.Match(key, wsData.Range("K:K"), 0)
Else
DataAlreadyExists = -1
End If
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
value2
代替value
(。
>
ref DataAlReadyExists()函数,您评估匹配两次在存在数据时,例如,请考虑此
Use
Value2
instead ofValue
(ref.)i.e. in
AddData()
and
Also, in your
DataAlreadyExists()
function, you evaluate MATCH twice when data do exist, e.g. consider this