How can I stop editing cell if it is not done with in set time? - vba

In my office we tally bags with a barcode scanner, but some times the user edits the Excel cell, giving the bag number manually, so I want to stop manually writing in excel cell.
That cell must update only by scanner.
I've tried the code below, and it returns the keystroke count but not the time.
Private Sub Worksheet_Change(ByVal Target As Range)
'If Target.Address = Range("A1:A100") Then
'Enter Code or Call any Function if any process has to be performed
'When someone Edits the cell A1
If Range(ActiveCell, ActiveCell.Offset(numRows, numCols)).Offset.Value = "" Then
Call Demo
Else: End If
End Sub
Sub Demo()
'Specify a range (change to suit)
MsgBox CountKeystrokes(Sheets("Sheet1").Range("A:A"))
If Range(ActiveCell, ActiveCell.Offset(numRows, numCols)).Offset.Value <> "" Then
Range(ActiveCell, ActiveCell.Offset(numRows, numCols)).Select
Selection.ClearContents
Else
End If
End Sub
Function CountKeystrokes(rng As Range) As Long
Dim rCell As Range
Dim iCtr As Long
For Each rCell In rng
iCtr = iCtr + Len(rCell.Formula)
Next
CountKeystrokes = iCtr
End Function

Related

VBA: How to automatically trigger macro without a button

I have made a macro that auto fill the formula on sheet1 whenever the row number of sheet2 is changed.
Is it possible to trigger it automatically without a button when i have any update on sheet2?
Sub Autofill()
Dim sg As Sheets
Dim Row As Long
Dim fillRow As Integer
Application.EnableEvents = False
Row = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
fillRow = Row - 1
Sheets("Sheet1").Select
Range("A1:E1").Select
Selection.Autofill Destination:=Range("A1:E" & fillRow), Type:=xlFillDefault
Application.EnableEvents = True
End Sub
You could try to create a sub like following:
Paste the following code. And change:
1) "D4" with your cells you want to "monitor"
2) Paste your macro in the line "Do things"
The problem is, your code is run everytime the focus is changed to another cell.
But you could also use Worksheet_BeforeDoubleclick if this is enough. Then every time you clicke twice the code will run
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
'Do things
End If
End If
End Sub
Right-click on the sheet tab at the bottom of the scree, click 'view
code' then insert this following code in there.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Row As Long
Dim fillRow As Integer
This next line will exit the code if column A is not what is being
changed on the sheet. Delete it if you want the code to be triggered
by any change on any cell of the sheet.
if InRange(Target,Worksheets("Sheet2").range("A:A") = false then exit sub
Row = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
fillRow = Row - 1
Sheets("Sheet1").Select
Range("A1:E1").Select
Selection.Autofill Destination:=Range("A1:E" & fillRow), Type:=xlFillDefault
End Sub
Function InRange(Range1 As Range, Range2 As Range) As Boolean
InRange = Not (Application.Intersect(Range1, Range2) Is Nothing)
End Function
use a Worksheet.SelectionChange-Event.
in the Worksheet-VBA for sheet2 add:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call Autofill()
End Sub
(This will be triggered if cell is changed, even if user does not leave the row, so check parameter Target.)

implement code in unprotected and protected sheets VBA Excel

when I am trying to execute this code it only executes the part after Exit Sub , only the foreach loop is executed, when the sheet is unprotected. I think it is caused by the Exit Sub. My problem is that I want to execute to different codes one when the sheet is protected (for each cell in Range("B6:B112..)) and the other(starting at Dim rng as Range..) when the sheet is unprotected. I tried If..Then..Else but that does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("test").ProtectContents Then Exit Sub
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
Dim rng As Range
Dim eingabeNr As Double, letzteZeile As Long, eingabeDatum As String, eingabeNrString As String
Set rng = Range("D:BC")
THX.
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("test").ProtectContents = True Then 'added = true for readability.
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
Else
Dim rng As Range
Dim eingabeNr As Double, letzteZeile As Long, eingabeDatum As String,_
eingabeNrString As String
Set rng = Range("D:BC")
Do some stuff here
End if

Display cell content in a text box in excel using VBA

I have a range of cells with data. I want a text box to show the cell content when I click on any cell in the text box. Is this possible? Thanks
You could just use something like this:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Rows
For i = 1 To lRow
If Cells(i, 1).Count = 1 Then
If Cells(i, 1) = "" Then
Else
If Not Intersect(Target, Cells(i, 1)) Is Nothing Then
MsgBox (i)
End If
End If
End If
Next i
End Sub
This will show the value in a message box, not a text box. Not sure why you need a text box.
i refers to the row and change the 1 in lRow = Cells(Rows.Count, 1).End(xlUp).Rows to the correct column number you are working in
Add this to the worksheet (see the black arrow):
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target.Value
End Sub
In general, if you want to check for a specific range, you can define the range in the event:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngPreselected As Range
Set rngPreselected = Range("A1:B10")
If Not Intersect(Target, rngPreselected) Is Nothing Then
MsgBox Target.Value
End If
End Sub
In this case, A1:B10 is the defined range.
That's called Event. See more about events here: http://www.cpearson.com/excel/events.aspx

excel vba loop - repeat a command till somewhere

Hello I would like to have a function that, every time I click in a certain cell (E6) it will lock cells on the left (A6:C6) or (E7) will lock (A7:C7).
Example of my sheet
I do not really know VBA I just need this one function.
I need to have this for as many cells as i want - for example every E2:E1000 will lock the row on the left from A2:C2, A3:C3 and so on...
Sub ProtectTheSheet()
Dim chCell As Range
Dim chRng As Range
ActiveSheet.Unprotect Password:="password"
Set chRng = ActiveSheet.Range("A6:C6")
'Check cell value in body and lock cells with content
For Each chCell In chRng.Cells
chCell.Locked = (chCell.Value <> "")
Next chCell
ActiveSheet.Protect Password:="password"
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E6")) Is Nothing Then
Call ProtectTheSheet
End If
End If
End Sub
Thank you guys very much in advance.
I think, by reading you question, that the next code will solve your problem:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 And Selection.Column = 5 Then
ThisWorkbook.ActiveSheet.Unprotect Password:="password"
For col = 1 To 3
ThisWorkbook.ActiveSheet.Cells(Selection.Row, col).Locked = True
Next col
ThisWorkbook.ActiveSheet.Protect Password:="password"
End If
End Sub
Please note that, in this case, you don't need ProtectTheSheet subroutine.

SHEETOFFSET to copy color

I am using the SHEETOFFSET VBA code
Function SHEETOFFSET(offset, Ref)
' Returns cell contents at Ref, in sheet offset
Application.Volatile
With Application.Caller.Parent
SHEETOFFSET = .Parent.Sheets(.Index + offset) _
.Range(Ref.Address).Value
End With
End Function
And then the following code within within my new sheet
=sheetoffset(-1, B2)
to copy the value of cell B2 in the previous sheet to my new sheet.
However, I also need to copy the color of that particular cell. Is there any code that I can enter in the original VBA code above to do this? Or is there another way of achieving this?
Many thanks for your help
Tim
Logic:
Define a Public variable to hold the color of the cell
In Worksheet_Change check if the above variable has any value. If yes then change the color of the target cell.
Once the above is done, reset the variable to 0
Code in Module:
Public cellColor As Double
Function SHEETOFFSET(offset, Ref)
With Application.Caller.Parent
SHEETOFFSET = .Parent.Sheets(.Index + offset) _
.Range(Ref.Address).Value
'~~> Store the color in a variable
cellColor = .Parent.Sheets(.Index + offset) _
.Range(Ref.Address).Interior.ColorIndex
End With
End Function
Code in Sheet Code Area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
On Error GoTo Whoa
Application.EnableEvents = False
For Each aCell In Target.Cells
If cellColor <> 0 Then aCell.Interior.ColorIndex = cellColor
Next
Letscontinue:
cellColor = 0
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
ScreenShot:
My Personal Thoughts:
I am not in favor of the SHEETOFFSET function in the first place because the formula is actually referring a cell in the current sheet. Any changes, for example, deletion of that cell will error out your formula
It is better to link the cells directly
FOLLOWUP (From Comments)
You can run this code in the end to refresh all formulas.
Sub Sample()
Dim ws As Worksheet
Dim rng As Range, aCell As Range
For Each ws In ThisWorkbook.Sheets
Set rng = Nothing
On Error Resume Next
Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not rng Is Nothing Then
For Each aCell In rng
aCell.Formula = aCell.Formula
Next
End If
Next
End Sub