Attempting to scan a barcode and split the entry into 2 cells - excel

I haven't found anything specific, so please forgive me if this has been addressed.
I have a spreadsheet set up for the user to scan a barcode (Code128). I need the spreadsheet to automatically place the first 13 digits in the next empty cell in column A, and the remaining characters (10 characters) in the adjacent cell in column B.
I've seen the user of left and right in formulas but haven't determined how to do this in VBA.

Please, copy the next code in the sheet code module where the scanning is done:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.cells.count > 1 Then Exit Sub
If Target.Column = 1 Then
If Len(Target.Value) > 10 Then
Application.EnableEvents = False
Target.Offset(1).Value = left(Target.Value, 10) 'place first 10 digits in the next cell
Target.Offset(1, 1).Value = Right(Target.Value, Len(Target.Value) - 10) 'and the rest on the next row, column B:B
Target.Offset(2).Select
Application.EnableEvents = True 'select next cell to scan in
End If
End If
End Sub
Before start scanning, if the barcode content is numeric, you should format columns "A:A" and "B:B" as text, but using TextToColumns (one column at a time). Pressing Next, Next, check Text and press Finish.
Edited:
The next version keep the first 10 digits in the cell where the scan has been done, and rest of the digits in column B:B, Target row:
Private Sub Worksheet_Change_(ByVal Target As Range)
If Target.cells.count > 1 Then Exit Sub
If Target.Column = 1 Then
If Len(Target.Value) > 10 Then
Application.EnableEvents = False
Target.Offset(1).Value = left(Target.Value, 10)
Target.Offset(1, 1).Value = Right(Target.Value, Len(Target.Value) - 10)
Target.Offset(2).Select
Application.EnableEvents = True
End If
End If
End Sub

Related

How to move row to another sheet?

VBA is a total black magic to me so please be gentle and assume you're talking to an idiot :)
I've created a spreadsheet, with multiple tabs.
Based on a drop down selection, I am trying to move rows to corresponding sheets.
I've achieved that ( and is working great ) by using the below VBA code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Check to see only one cell updated
If Target.CountLarge > 1 Then Exit Sub
' Check to see if entry is made in column C after row 7 and is set to "Yes"
If Target.Column = 3 And Target.Row > 7 And (Target.Value = "LTS" Or Target.Value = "On Hold") Then
Application.EnableEvents = False
' Copy columns A to AU to complete sheet in next available row
Range(Cells(Target.Row, "A"), Cells(Target.Row, "AU")).Copy Sheets("On Hold and LTS").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
' Delete current row after copied
Rows(Target.Row).Delete
Application.EnableEvents = True
End If
' Check to see if entry is made in column C after row 7 and is set to "Yes"
If Target.Column = 3 And Target.Row > 7 And (Target.Value = "Leaver") Then
Application.EnableEvents = False
' Copy columns B to I to complete sheet in next available row
Range(Cells(Target.Row, "A"), Cells(Target.Row, "AU")).Copy Sheets("Leavers").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
' Delete current row after copied
Rows(Target.Row).Delete
Application.EnableEvents = True
End If
End Sub
Although the above code works as intended, every time I receive the "run time error 424 - object required message". Clicking end resolves it and the rule works as intended.
( The second part of the code [ Leaver ] does not produce the error, and also works as intended )
Could you please help me stopping this error from coming up please?
Thank you!
If the cell contains "LTS" (or "On Hold") then the first If..End If block runs and among other things deletes the row containing Target. You then move to your second If..End If block and test Target - which no longer exists - hence your error.
Try using ElseIf in there instead:
Private Sub Worksheet_Change(ByVal Target As Range)
' Check to see only one cell updated
If Target.CountLarge > 1 Then Exit Sub
' Check to see if entry is made in column C after row 7 and is set to "Yes"
If Target.Column = 3 And Target.Row > 7 And (Target.Value = "LTS" Or Target.Value = "On Hold") Then
Application.EnableEvents = False
' Copy columns A to AU to complete sheet in next available row
Range(Cells(Target.Row, "A"), Cells(Target.Row, "AU")).Copy Sheets("On Hold and LTS").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
' Delete current row after copied
Rows(Target.Row).Delete
Application.EnableEvents = True
' Check to see if entry is made in column C after row 7 and is set to "Yes"
ElseIf Target.Column = 3 And Target.Row > 7 And (Target.Value = "Leaver") Then
Application.EnableEvents = False
' Copy columns B to I to complete sheet in next available row
Range(Cells(Target.Row, "A"), Cells(Target.Row, "AU")).Copy Sheets("Leavers").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
' Delete current row after copied
Rows(Target.Row).Delete
Application.EnableEvents = True
End If
End Sub

Color Parent Dropdown Cell Without Clearing Contents

Have had some great help from this forum on editing my codes. I need to update it just slightly, but can't seem to figure out the right solution. Essentially, my code allows for cell clearing and color in adjacent data validation dropdowns. I have the primary dropdown in column F, secondary dropdown in column G, and final dropdown in column H. My code works as such: if the primary dropdown value in column F changes, clear and color adjacent cells in column G and H. If only secondary dropdown value in column G changes, clear and color adjacent cells in column H.
What I need is if there is a change in primary dropdown in column F, not to clear contents, but only color the cell, while still clearing and coloring adjacent cells in column G and H, or clearing and coloring adjacent cells in column H if change in secondary dropdown in column G.
I tried adjusting the (For i = Target.Column + 1 To 8) to (For i = Target.Column + 0 to 8) and it does work with coloring the primary dropdown, but then it clears it. And I can't make a selection because it is clearing it consistently. Thus, I cannot make any adjacent dropdown selections.
Here is the code I have so far:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.CountLarge <> 1 Then Exit Sub 'screen out multi-cell changes
If Target.Column > 7 Then Exit Sub 'col 1/2
If Not CellHasValidation(Target) Then Exit Sub '...with validation
On Error GoTo haveError 'ensure events are not left off
Application.EnableEvents = False
'loop to max column to be cleared
For i = Target.Column + 1 To 8
With Target.EntireRow.Cells(i)
.Interior.ColorIndex = 44
.Value = ""
End With
Next i
Application.EnableEvents = True
haveError:
MsgBox Err.Description
Application.EnableEvents = True
End Sub
'check if a cell has validation
Function CellHasValidation(cell As Range) As Boolean
Dim vt
On Error Resume Next 'ignore if error (no validation)
vt = cell.Validation.Type
On Error GoTo 0 'stop ignoring errors
CellHasValidation = Not IsEmpty(vt)
End Function
So after a bit of troubleshooting, I was able to embed if statements into the code in order to test for the target column before running the entire row change.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
If Target.CountLarge <> 1 Then Exit Sub 'screen out multi-cell changes
If Target.Column > 7 Then Exit Sub 'col 1/2
If Not CellHasValidation(Target) Then Exit Sub '...with validation
On Error GoTo haveError 'ensure events are not left off
Application.EnableEvents = False
'loop to max column to be cleared
If Target.Column = 6 Then
ActiveCell.Interior.ColorIndex = 44
End If
If Target.Column = 7 Then
ActiveCell.Interior.ColorIndex = 44
End If
For i = Target.Column + 1 To 8
With Target.EntireRow.Cells(i)
.Interior.ColorIndex = 44
.Value = ""
End With
Next i
Application.EnableEvents = True
Exit Sub
haveError:
MsgBox Err.Description
Application.EnableEvents = True
End Sub
'check if a cell has validation
Function CellHasValidation(cell As Range) As Boolean
Dim vt
On Error Resume Next 'ignore if error (no validation)
vt = cell.Validation.Type
On Error GoTo 0 'stop ignoring errors
CellHasValidation = Not IsEmpty(vt)
End Function

Excel macro to add date for any change in data in a row

I am looking to parse an entire row in a particular excel sheet for any change in data in that row. If there is any change in data in that row then i want to add the date in which that particular cell of that row. I want to pass the row as an input. I tried the following code but it doesnt work.
Private Function User_func1(ByVal i As Long)
Dim j As Long
For j = 1 To j = 100
If Cells(i, j).Value > 1 Then
Cells(i, 2) = Now()
End If
Next j
End Function
You can use the Worksheet_Change event in the sheet you want to scan.
Option Explicit
Const RowtoTest = 2
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Row = RowtoTest Then
Target.Value = Date
End If
Application.EnableEvents = True
End Sub
Option 2: Get the row to test from a certain cell, lets say Cell "A1" (value is set to 2, means look for changes in cells in row 2).
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
' compare the row number to the number inputted as the row to test in cell A1
If Target.Row = Range("A1").Value Then
Target.Value = Date
End If
Application.EnableEvents = True
End Sub

hide next column upon getting total in previous column

I have been assigned work on Excel & Macro, and I am not much aware of those.
Task is to Hide Next Column automatically(withouht being refreshed or press F2) if Previous Column Total is 0. and incase if its Total is >=0 then unhide itautomatically(withouht being refreshed or press F2).
Assume I have Set of Columns & Rows(say C11 to C20) where I have to enter values (say 0 or >=0), and once i reach last cell(say C20) which has Formula of Sum for particular column(C11 to C20), If total is 0 then next column (say D) should get Hide without pressing any key if total is >=0 then column D should be as it is.
Please help me out.
Here is the code to Hide Column.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$31" Then
If Target <= 0 Then
Range("D31").EntireColumn.Hidden = True
Else
Range("D31").EntireColumn.Hidden = False
End If
End If
If Target.Address = "$D$31" Then
If Target <= 0 Then
Range("E31").EntireColumn.Hidden = True
Else
Range("E31").EntireColumn.Hidden = False
End If
End If
If Target.Address = "$E$31" Then
If Target <= 0 Then
Range("F31").EntireColumn.Hidden = True
Else
Range("F31").EntireColumn.Hidden = False
End If
End If
End Sub
Here is the solution i found after googling for this long. I am using this only for 1 column right now.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
For Each rCell In Range("C11:C31")
If Range("C31").Value = "0" Then
Range("D31").EntireColumn.Hidden = True
Else
If Range("C31").Value <> "0" Then
Range("D31").EntireColumn.Hidden = False
End If
End If
Next rCell
End Sub

How to highlight a row if three conditions are met?

If the following conditions are met:
For any given row between row 10 and row 100 inclusively:
The cell in column A is not empty
The cell in column B is not empty
The cell in column O is empty
I would like to highlight a specific cell (let's say A1).
Example:
I populate A10 and E10 while leaving O10 empty, then cell A1 gets highlighted. If I then populate cell O10, the highlight in cell A1 disappears.
I can proceed to the next row. Any row at any time should generate these actions.
Thanks!
This will do the highlights based on the conditions you specified. When you run it, it'll stop at the first row you need to input something in column O. If you want it to keep running until row 101 and highlight all the rows, then remove then Exit Do command that's between the 2 End If statements.
Sub Highlight()
Dim TheRow As Integer
TheRow = 9
Application.ScreenUpdating = False 'This hides the visual process and speeds up
'the execution
Do
TheRow = TheRow + 1
If TheRow = 101 Then Exit Do
Cells(TheRow, 1).Select
Selection.Interior.Pattern = 0
Cells(TheRow, 2).Select
Selection.Interior.Pattern = 0
If Not Cells(TheRow, 1).Value = "" And Not Cells(TheRow, 2).Value = "" And Cells(TheRow, 15).Value = "" Then
If Cells(TheRow, 1).Value = "" Then
Cells(TheRow, 1).Select
Selection.Interior.Color = 656
End If
If Cells(TheRow, 2).Value = "" Then
Cells(TheRow, 2).Select
Selection.Interior.Color = 656
End If
Exit Do 'this is the line to remove if you want to highlight all cells
End If
Loop
Application.ScreenUpdating = True
End Sub
And then, create an event handler that triggers when a cell in column 15 changes. Put the following code in the module of the actual worksheet (in the VBA project explorer, double click on the sheet you want have this functionality for; don't put this in a different module!)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 15 Then
If Target.Row > 9 And Target.Row < 101 Then Call Highlight
End Sub
Let me know if this solution works and remember to click "accept solution" and to vote for it!
Happy coding.
You don't need VBA: just use conditional formatting on cell A10 with the following formula:
=AND(NOT(ISBLANK($A10)),NOT(ISBLANK($B10)),ISBLANK($O10))
OK - I misunderstood what you wanted. Here is a VBA UDF to do the checking.
Enter =Checker($A$10:$B$100,$O$10:$O$100) in cell A1, then use conditional formatting on cell A1 that is triggered when it becomes True.
Public Function Checker(theRangeAB As Range, theRangeO As Variant) As Boolean
Dim varAB As Variant
Dim varO As Variant
Dim j As Long
varAB = theRangeAB.Value2
varO = theRangeO.Value2
Checker = False
For j = 1 To UBound(varAB)
If Not IsEmpty(varAB(j, 1)) And Not IsEmpty(varAB(j, 2)) Then
If IsEmpty(varO(j, 1)) Then
Checker = True
Exit For
End If
End If
Next j
End Function

Resources