Log button press

Trixafett

New Member
Joined
Oct 9, 2023
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
I have a sheet called Inventory where a large number of items appear.
There is a quantity of stock for each item.
i did:
Range("G4").Value = Range("G4") + Range("N4") for each line on button press, this must be able to be done easier.

And i would also like to have a Log .txt file where evert time a push that button i add a line with button name, time, and username.

Can someone please help me.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
To append to a text file you can use
VBA Code:
Sub AppendFile()
Dim strFile_Path As String
Dim ApTxtLine As String

strFile_Path = "C:\somewhere\nameoffile.txt"
ApTxtLine = "Button X" & ", " & Application.UserName & ", " & Date & ", " & Time()

Open strFile_Path For Append As #1
Write #1, ApTxtLine
Close #1

End Sub
 
Upvote 0
To append to a text file you can use
VBA Code:
Sub AppendFile()
Dim strFile_Path As String
Dim ApTxtLine As String

strFile_Path = "C:\somewhere\nameoffile.txt"
ApTxtLine = "Button X" & ", " & Application.UserName & ", " & Date & ", " & Time()

Open strFile_Path For Append As #1
Write #1, ApTxtLine
Close #1

End Sub
Thank you for the response.

I fixed it myself

VBA Code:
Option Explicit

Sub btnStk_Click()
    Dim rData As Range, arrData
    Dim i As Long
    Dim sName As Long
    Dim sNote As String
    Set rData = Sheets("Inventory").Range("A3").CurrentRegion
    arrData = rData.Value
    For i = 2 To UBound(arrData)
        If Len(arrData(i, 14)) > 0 And VBA.IsNumeric(arrData(i, 14)) Then
            arrData(i, 7) = arrData(i, 7) + arrData(i, 14)
            If Val(arrData(i, 14)) > 0 Then
                sNote = "Stock Added"
            Else
                sNote = "Stock Removed"
            End If
            ' Clear column C
            Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 5).Value _
                = Array(Now(), Environ("UserName"), sNote, arrData(i, 3), arrData(i, 14))
            arrData(i, 14) = ""
        End If
    Next
    rData.Value = arrData
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,269
Members
449,093
Latest member
Vincent Khandagale

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