Color Parent Dropdown Cell Without Clearing Contents - excel

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

Related

Insert other values from drop-down list with macro, but several times

I make an Excel file that I use to import on a website. However, the values displayed mean nothing to the website, it needs IDs, but it's just not user-friendly. To help my users I said to myself I'll make a drop-down list that will change the value. For example, if you click on Switzerland, it will show 1 in the cell.
I have created one Macro based on the instruction on https://www.extendoffice.com/documents/excel/4130-excel-drop-down-list-show-different-value.html
'Updateby Extendoffice
Dim xRg As Range
selectedNa = Target.Value
If Target.Column = 3 Then
Set xRg = ActiveWorkbook.Names("Dropdown_cantons").RefersToRange
selectedNum = Application.VLookup(selectedNa, xRg, 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
End If
End If
End Sub
For the first column it's ok
For my second collone with the cities this time, I thought I would change the collone numbers and follow the instructions in the link above. Maybe I can't put two macros in one Excel?
I tried to do it as a module, but it doesn't work for the second collone either. Do you have an idea?
Could look something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub 'only handle single-cell changes
Select Case Target.Column
'depending on the column number, call `SetId` with specific arguments
Case 3: SetId Target, ActiveWorkbook.Names("Dropdown_cantons").RefersToRange, 2
Case 4: SetId Target, ActiveWorkbook.Names("Dropdown_countries").RefersToRange, 2
End Select
End Sub
'translate a data validation drop-down "text" selection into a matching "id" value
Sub SetId(c As Range, rngTable As Range, returnCol As Long)
Dim v, res
v = c.Value
If Len(v) > 0 Then
res = Application.VLookup(v, rngTable, returnCol, False) 'find the id
If Not IsError(res) Then 'got a match
On Error GoTo haveError 'ensure we exit with events back on
Application.EnableEvents = False 'disable events
c.Value = res 'switch the value
Application.EnableEvents = True 're-enable events
End If
End If
Exit Sub 'normal exit
haveError:
Application.EnableEvents = True 'just in case....
End Sub

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

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

Macro is not working automatically

I am using a macro to write a datestamp when a column is modified. The idea is that whenever the status changes it gives the running time for that particular status. I have four columns:
A b c d
clearing 24.04.2015 1 empty
**when stauts is changed**
A b c d
wait for start 24.04.2015 2 24.04.2015
formual for c is :
IF(RC[-2]="";"";IF(RC[-2]="clearing";1;2))
Macro;
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Value = "clearing"
Then
Cells(Target.Row, 2) = Date
Else
If Target.Column = 3 And Target.Value = 2
Then
Cells(Target.Row, 4) = Date
End If
End If
End Sub
The problem is when C column is, with the help of formula, changed to 2 the macro does not automatically give me the date, but when I insert that manually it's working.
When you put values into the worksheet that is triggering the Worksheet_Change event macro, you should always turn off events or the macro will try to run on top of itself.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns(1)) Is Nothing Then
On Error GoTo Fìn
Application.EnableEvents = False
Dim rng As Range
For Each rng In Intersect(Target, Columns(1))
If LCase(rng.Value) = "clearing" Then
Cells(rng.Row, 2) = Now
Cells(rng.Row, 2).NumberFormat = "dd.mm.yyyy"
'Cells(rng.Row, 3).FormulaR1C1 = "maybe put the formula in here"
ElseIf rng.Offset(0, 2).Value = 2 Then
Cells(rng.Row, 4) = Now
Cells(rng.Row, 4).NumberFormat = "dd.mm.yyyy"
End If
Next rng
End If
Fìn:
Application.EnableEvents = True
End Sub
It sounds like you already have that formula in column C but I left a place where you can put it in once column A gets the clearing value. Another option would be to simply write a 1 into column C and the next time write a 2 in column C. That way you wouldn't have to deal with the formula at all.

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

Resources