hide next column upon getting total in previous column - excel

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

Related

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

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

VBA code to check if the result of formula is negative and if negative pop up a msgbox stating the error

I have following code, however this code is work in progress:
My intentions are to use table name instead of cell name (f7 and h7). Formula used in the sheet is
Pending Quantity (column f) = Total quantity(Column Issue till date (column e) -((issue from FSD Harduaganj (column g)) + (issue from SWC Aligarh (column H)))
and VBA code is to check if the entered quantity in column g or h makes the pending quantity negative, then clear that entered quantity.
Here is the current code, the code is referring to f7 and h7 however I intend to use it on whole column like... if I enter data in next row it should check if the pending quantity is negative if negative it should remove/clear the last entry from column g or h of that row.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim entrycell As Range
Set Target = Me.Range("f7")
Set entrycell = Me.Range("H7")
If Target.Value < 0 Then
MsgBox ("This Value must be a number of zero or greater."), , "Invalid Entry":entrycell.ClearContents
End If
End Sub
Based on your description, try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Or Target.Column = 8 Then
If Cells(Target.Row, "F") < 0 Then
MsgBox ("This Value must be a number of zero or greater."), , "Invalid Entry"
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
End If
End If
End Sub

How to ensure correct character length is being input

I need to ensure that the user types in an integer with length (blank) in any cell of a certain column. If the user inputs a number that is not length (blank), the Excel freezes the user at that cell and prompts to re-enter until integer length (blank) has been inputted or cancel is hit.
I currently have most of the things I request working. However, my issue is that Excel doesn't recognize length errors until I move away from the cell and come back to it.
For example (using 3 as desired length):
If i am currently on Cell B12 and type in 15646, which is not length 3, I can still click enter and it will move to Cell B13, which I want to prevent. But if I move up to B12 again from B13, the length error is seen and Excel prompts me to input integer with correct length until its fixed.
For now, the length error is only being recognized when I come back to cell. I need it to recognize as soon as I hit enter and prevent from moving on to next cell.
Sub InputNum()
row = ActiveCell.row
col = ActiveCell.Column
If col = 2 And ActiveCell.Value <> "" Then
Dim lotTextLen As Integer
lotTextLen = Len(ActiveCell.Value)
'checks to ensure the number put in is 3 characters long
'requests an input number to be put in
If lotTextLen <> 3 Then
lotData = InputBox("Invalid Entry Length. Scan in Lot #")
If Len(lotData) <> 3 Then
'error message
Result = MsgBox("Invalid Lot # Inputed. Must be 3 Characters. Try Again?", vbOKCancel)
'if cancel is clicked, input number is made blank and sub is exited
If Result <> vbOK Then
ActiveCell.Value = ""
'if ok is clicked to try again, recurses to beginning of code again
Else
InputNum
End If
Else
ActiveCell.Value = lotData
End If
End If
End If
End Sub
InputNum is being called in the Sheet1
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range("B:C")) Is Nothing Then
InputNum
End If
End Sub
In the sheet object place the following
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Columns(2)) Is Nothing Then
Application.EnableEvents = False
InputNum Target
Application.EnableEvents = True
End If
End Sub
Then use this in a standard module
Public Sub InputNum(Target As Range)
Dim IoTData As String
Dim Result As String
Dim isCancel As Boolean
Do While Len(Target.Value2) <> 3
IoTData = InputBox("Invalid Entry Length. Scan in Lot #")
If Len(IoTData) = 3 Then
Target.Value2 = IoTData
Else
If IoTData <> vbNullString Then
' error message
Result = MsgBox("Invalid Lot # Inputed. Must be 3 Characters. Try Again?", vbOKCancel)
If Result <> vbOK Then isCancel = True
Else
isCancel = True
End If
End If
If isCancel Then
Target.Value2 = vbNullString
Exit Do
End If
Loop
End Sub
By placing your code in a loop it will keep pestering the user for the right length until either they enter the right format or they press cancel in which instance the cell will be cleared of it's input.
You can also add And IsNumeric(IoTData) to your If statement to test that a number has been entered.
Replace
If Len(IoTData) = 3 Then
With
If Len(IoTData) = 3 And IsNumeric(IoTData) Then
Option Explicit
Dim add As String
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:C")) Is Nothing And Target.Count = 1 Then
If Len(Target.Value) <> 3 Then
MsgBox "Invalid entry in cell with address " & add
Application.EnableEvents = False
Target.Activate
'Enter more code
Application.EnableEvents = True
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B:C")) Is Nothing And Target.Count = 1 Then
add = Target.Address
End If
End Sub

Updating value of a non-Target cell in Excel VBA

I found the attached when looking for how to due an event change to correct user data based on the values in two columns. I'm not a programmer, so I may have butchered the code as I combined two different solutions together.
Right now, it's working exactly as I want it to. Changing the offset cell value forces Excel to replace the target value with what I've specified. What I'm looking to achieve (and am not sure is possible), is to reverse the code. Basically, I want to change the offset cell, if the values are entered in the opposite order. The code will change the cell value to "Beta" if a user enters "Bravo" in column A, and then "Gamma" in column C.
What I'm trying to achieve is that if the user enters "Bravo" in column A second, that Excel still sees the combination of these cells and still replaces the value with "Beta". I know this is additional code, but I couldn't find anything to support replacing cell when the target cell isn't the value being updated.
Thanks in advance!
Dim oldCellAddress As String
Dim oldCellValue As String
Private Sub Worksheet_Change(ByVal Target As Range)
oldCellValue = "Bravo"
If Target = "Bravo" And Target.Offset(0, -2) = "Gamma" Then
Target.Value = "Beta"
Application.EnableEvents = True
End If
End Sub
This may meet your needs:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim colnum As Long, v As Variant
colnum = Target.Column
v = Target.Value
If colnum = 1 Then
If v = "Bravo" And Target.Offset(0, 2) = "Gamma" Then
Application.EnableEvents = False
Target.Value = "Beta"
Application.EnableEvents = True
End If
Exit Sub
End If
If colnum = 3 And v = "Gamma" And Target.Offset(0, -2) = "Bravo" Then
Application.EnableEvents = False
Target.Offset(0, -2).Value = "Beta"
Application.EnableEvents = True
End If
End Sub
For example if the user puts Bravo in cell A1 and C1 already contained Gamma, the code puts Beta in A1 (the code corrects the A1 entry).If the user puts Gamma in cell C1 and cell A1 already contained Bravo, the code corrects A1.
There are two possible scenarios like below...
Scenario 1:
If ANY CELL on the sheet is changed, the following code will check the content of column A and C in the corresponding row and change the content of the Target Cell.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim r As Long
r = Target.Row
On Error GoTo Skip:
Application.EnableEvents = False
If Cells(r, "A") = "Bravo" And Cells(r, "C") = "Gamma" Then
Target.Value = "Beta"
End If
Skip:
Application.EnableEvents = True
End Sub
Scenario 1:
If a cell in column D is changed, the change event will be triggered and check the content in column A and C in the corresponding row and change the Target Cell in Column D.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim r As Long
On Error GoTo Skip:
'The below line ensures that the sheet change event will be triggered when a cell in colunm D is changed
'Change it as per your requirement.
If Not Intersect(Target, Range("D:D")) Is Nothing Then
Application.EnableEvents = False
r = Target.Row
If Cells(r, "A") = "Bravo" And Cells(r, "C") = "Gamma" Then
Target.Value = "Beta"
End If
End If
Skip:
Application.EnableEvents = True
End Sub

How to hide/unhide rows based on cell value, multiple times in 1 sheet

When the value in B16 is "0", I need to hide some other rows, and also un-hide them when the value is "1".
But, here's my issue:
Also, in the same sheet:
If the value in H4 is "0", I need to hide some rows. When the value is "1", I need to un-hide these rows.
The first part is working:
Private Sub Worksheet_Change(ByVal Target As Range)
With Sheets("Checklist")
If Range("b16").Value = 0 Then rows("17:21").EntireRow.Hidden = True
If Range("b16").Value >= 1 Then rows("17:21").EntireRow.Hidden = False
End With
End Sub
And here's where I understand I suck at VB
In other words, the code below is clearly not working. Can you please provide a working alternative?
Private Sub Worksheet_Change(ByVal Target As Range)
With Sheets("Checklist")
Select Case Taget.Address
Case "$b$16"
If IsNumeric(Target.Value) And Target.Value = 0 Then
rows("17:21").EntireRow.Hidden = True
End If
Case "$b$16"
If IsNumeric(Target.Value) And Target.Value = 0 Then
rows("17:21").EntireRow.Hidden = False
End If
Case "$h$4"
If IsNumeric(Target.Value) And Target.Value = 1 Then
rows("23:26").EntireRow.Hidden = True
End If
Case "$h$4"
If IsNumeric(Target.Value) And Target.Value = 1 Then
rows("23:26").EntireRow.Hidden = False
End If
End Select
End Sub
Thanks.

Resources