[VBA]Copy data from specify cell and input value to another sheet with time stamp

maiwarits

New Member
Joined
Jul 17, 2022
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hello Sir,

I have a simple stock that I have consult with our board before.

For now, I would like to create a time stamp, copy name in column C and input data to another sheet when user has click OK button.

Example:
If I input 10 in the box and click OK, It will store like in the result picture.

Input 10current result
1720405866571.png
1720405916118.png

Next, After I click OK then store the result finish, I would like to create time stamp in TimeStamp work sheet in every click OK.
Example: Result should be like this in TImeStamp worksheet.
1720406094477.png


Here is my code and file My file.
I have try to create the time stamp as you can see in the code. But it not working as my expected

VBA Code:
Sub addStock()

Dim balanceStock As Double
Dim Col As Integer
'Check imput Applies columns E & F only
If ActiveCell.Column < 5 Or ActiveCell.Column > 6 Then Exit Sub

'Find current mont
Col = 2 * Month(Now) + 6

balanceStock = InputBox("Amount?")

With ActiveCell
    .Value = .Value + balanceStock
    If balanceStock < 0 Then Col = Col + 1
    Cells(.Row, Col) = Cells(.Row, Col) + balanceStock
End With

'Copy and time stamp (not working now)
    Dim wsYearlyStock As Worksheet 'define source sheet
        Set wsYearlyStock = ThisWorkbook.Worksheets("YearlyStock")

    Dim wsTimeStamp As Worksheet 'define destination sheet
        Set wsTimeStamp = ThisWorkbook.Worksheets("TimeStamp")
    
    Dim DestRow As Long
    DestRow = 2 'start in row 5 in destination sheet
    
    Dim Rng As Range
    For Each Rng In Selection.Areas
        Rng.Resize(, 1).Copy Destination:=wsTimeStamp.Cells(DestRow, "C") 'copy A to B
        DestRow = DestRow + Rng.Rows.Count 'move DestRow to next free row
    Next Rng

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello @maiwarits. If I understand you correctly, here is an option:
VBA Code:
Option Explicit

Sub addStock()

    ' Check input Applies columns E & F only
    If ActiveCell.Column < 5 Or ActiveCell.Column > 6 Then Exit Sub

    ' Find current month
    Dim Col         As Long: Col = 2 * Month(Now) + 6
    Dim balanceStock As Double: balanceStock = InputBox("Amount?")

    With ActiveCell
        .Value = .Value + balanceStock
        If balanceStock < 0 Then Col = Col + 1
        Cells(.Row, Col) = Cells(.Row, Col) + balanceStock
    End With

    ' Copy and timestamp
    Dim wsYearlyStock As Worksheet: Set wsYearlyStock = ThisWorkbook.Worksheets("YearlyStock")   ' define source sheet
    Dim wsTimeStamp As Worksheet: Set wsTimeStamp = ThisWorkbook.Worksheets("TimeStamp")         ' define destination sheet
    Dim YearlyStockRow As Long: YearlyStockRow = ActiveCell.Row                                  ' get current row in YearlyStock
    Dim DestRow     As Long: DestRow = FindDestRow(wsYearlyStock, wsTimeStamp, YearlyStockRow)

    If DestRow > 0 Then

        ' Update existing row if the date matches
        If wsTimeStamp.Cells(DestRow, "A").Value = Date Then
            wsTimeStamp.Cells(DestRow, "C").Value = wsTimeStamp.Cells(DestRow, "C").Value + balanceStock
        Else

            ' Add new row
            AddNewTimeStampRow wsTimeStamp, YearlyStockRow, balanceStock
        End If

    Else

        ' Add new row if no matching row is found
        AddNewTimeStampRow wsTimeStamp, YearlyStockRow, balanceStock
    End If

End Sub

Function FindDestRow(wsYearlyStock As Worksheet, wsTimeStamp As Worksheet, YearlyStockRow As Long) As Long
    Dim i           As Long

    ' Search for the matching row in TimeStamp sheet
    For i = 2 To wsTimeStamp.Cells(wsTimeStamp.Rows.Count, "B").End(xlUp).Row

        If wsTimeStamp.Cells(i, "B").Value = wsYearlyStock.Cells(YearlyStockRow, "C").Value Then
            FindDestRow = i
            Exit Function
        End If

    Next i

    ' Return 0 if no matching row is found
    FindDestRow = 0
End Function

Sub AddNewTimeStampRow(wsTimeStamp As Worksheet, YearlyStockRow As Long, balanceStock As Double)
    Dim NewRow      As Long: NewRow = wsTimeStamp.Cells(wsTimeStamp.Rows.Count, "A").End(xlUp).Row + 1

    ' Add new row with today's date, YearlyStock reference, and balanceStock
    With wsTimeStamp
        .Cells(NewRow, "A").Value = Date
        .Cells(NewRow, "B").Value = ThisWorkbook.Worksheets("YearlyStock").Cells(YearlyStockRow, "C").Value
        .Cells(NewRow, "C").Value = balanceStock
    End With

End Sub
Perhaps I misunderstood something. Good luck.
 
Upvote 0
Hello @maiwarits. If I understand you correctly, here is an option:
VBA Code:
Option Explicit

Sub addStock()

    ' Check input Applies columns E & F only
    If ActiveCell.Column < 5 Or ActiveCell.Column > 6 Then Exit Sub

    ' Find current month
    Dim Col         As Long: Col = 2 * Month(Now) + 6
    Dim balanceStock As Double: balanceStock = InputBox("Amount?")

    With ActiveCell
        .Value = .Value + balanceStock
        If balanceStock < 0 Then Col = Col + 1
        Cells(.Row, Col) = Cells(.Row, Col) + balanceStock
    End With

    ' Copy and timestamp
    Dim wsYearlyStock As Worksheet: Set wsYearlyStock = ThisWorkbook.Worksheets("YearlyStock")   ' define source sheet
    Dim wsTimeStamp As Worksheet: Set wsTimeStamp = ThisWorkbook.Worksheets("TimeStamp")         ' define destination sheet
    Dim YearlyStockRow As Long: YearlyStockRow = ActiveCell.Row                                  ' get current row in YearlyStock
    Dim DestRow     As Long: DestRow = FindDestRow(wsYearlyStock, wsTimeStamp, YearlyStockRow)

    If DestRow > 0 Then

        ' Update existing row if the date matches
        If wsTimeStamp.Cells(DestRow, "A").Value = Date Then
            wsTimeStamp.Cells(DestRow, "C").Value = wsTimeStamp.Cells(DestRow, "C").Value + balanceStock
        Else

            ' Add new row
            AddNewTimeStampRow wsTimeStamp, YearlyStockRow, balanceStock
        End If

    Else

        ' Add new row if no matching row is found
        AddNewTimeStampRow wsTimeStamp, YearlyStockRow, balanceStock
    End If

End Sub

Function FindDestRow(wsYearlyStock As Worksheet, wsTimeStamp As Worksheet, YearlyStockRow As Long) As Long
    Dim i           As Long

    ' Search for the matching row in TimeStamp sheet
    For i = 2 To wsTimeStamp.Cells(wsTimeStamp.Rows.Count, "B").End(xlUp).Row

        If wsTimeStamp.Cells(i, "B").Value = wsYearlyStock.Cells(YearlyStockRow, "C").Value Then
            FindDestRow = i
            Exit Function
        End If

    Next i

    ' Return 0 if no matching row is found
    FindDestRow = 0
End Function

Sub AddNewTimeStampRow(wsTimeStamp As Worksheet, YearlyStockRow As Long, balanceStock As Double)
    Dim NewRow      As Long: NewRow = wsTimeStamp.Cells(wsTimeStamp.Rows.Count, "A").End(xlUp).Row + 1

    ' Add new row with today's date, YearlyStock reference, and balanceStock
    With wsTimeStamp
        .Cells(NewRow, "A").Value = Date
        .Cells(NewRow, "B").Value = ThisWorkbook.Worksheets("YearlyStock").Cells(YearlyStockRow, "C").Value
        .Cells(NewRow, "C").Value = balanceStock
    End With

End Sub
Perhaps I misunderstood something. Good luck.
Dear MikeVol,

Hello sir!
Sorry for my late reply.

I just clear my work and continue for this project again.


the value in timestamp sheet always combines for 1Gas name.
But actually, I would like to create time stamp in every event not combine total value for 1

Example:
Gas name1
I add 5 to stock, I would like to create 1 time stamp in new row.
Also, If I use 1, I would like to create 1 time stamp in new row. ( not combine in the same row)

I appreciate for your help in advance!

Thanks,
Warit L.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,150
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top