Home > Software engineering >  Help. Would you like to automatically update record history, the code can only be run in sheet1
Help. Would you like to automatically update record history, the code can only be run in sheet1

Time:11-20

The great god to see trouble:

Sheet1 input original values as follows:

Product prices
Calcium chloride, 74, 110
Calcium chloride, 77, 130

Will not regularly update price, hope can be in sheet2 sheet3 history record 2 kinds of products in the price update records ()

Got a code on the net, only run in the sheet1, and can only write one product, trouble should help take a look at how to modify the great god,

Dim xVal As String
Dim iVal As Integer
Private Sub Worksheet_Change (ByVal Target As Range)
The Static xCount As Integer
IVal=Application. WorksheetFunction. Count (Range (F: "F"), 1)
XCount=iVal
Application. EnableEvents=False
If Target. Address=Range (" B2 "). The Address Then
Range (" E3 "). Offset (xCount, 0). The Value=https://bbs.csdn.net/topics/Range (" B2 "). The Value
Range (" F3 "). Offset (xCount, 0). The Value=https://bbs.csdn.net/topics/Now
XCount=xCount + 1
The Else
If xVal & lt;> Range (" B2 "). The Value Then
Range (" E3 "). Offset (xCount, 0). The Value=https://bbs.csdn.net/topics/Range (" B2 "). The Value
Range (" F3 "). Offset (xCount, 0). The Value=https://bbs.csdn.net/topics/Now
XCount=xCount + 1
End the If
End the If
Application. EnableEvents=True
End Sub
Private Sub Worksheet_SelectionChange (ByVal Target As Range)
XVal=Range (" B2 "). The Value
End Sub


According to the code to run, you will be in the sheet1 e4, f4 display update record:
150 2019/6/6 23:58
110 2019/6/6 23:58

We hope that this can be set up at the back of the sheet, also tried in the back of the page reference data to calculate the sheet1, individual time can calculate, and computing the top sheet only, most of the time, every sheet to write the code, what all don't record,
O give directions!

CodePudding user response:

Write well, code is as follows:
 
Option Explicit

Private Sub Worksheet_Change (ByVal Target As Range)
Dim objSheet As Worksheet
The Select Case Target. Address
Case "$B $2"
The Set objSheet=Sheets (2)
Case "$$3 B
"The Set objSheet=Sheets (3)
End the Select

Dim lngRow As Long
LngRow=1

While objSheet. Cells (lngRow, 1) & lt;> "
"LngRow=lngRow + 1
Wend

If lngRow & gt; 1 Then
If CDbl (objSheet Cells (lngRow - 1, 1))=CDbl (Target) Then
The Exit Sub
End the If
End the If

ObjSheet. Cells (lngRow, 1)=Target. The Value
ObjSheet. Cells (lngRow, 2)=Now
End Sub

Run the example:



Download address:
Link: https://pan.baidu.com/s/1ijqrW-4Nbbr_eZei-ISpPA
The extracted code: wmc4

CodePudding user response:

Perfect solution, thank you for your great god
  •  Tags:  
  • VBA
  • Related