duplicate display of check Box VBA excel - excel

in the Range("J5:J1000, K5:K1000, L5:L1000 , M5:M1000, N5:N1000, O5:O1000") i applied a date Picker
i cant figure out why there is a duplicate display in cell O21 when i chose a field in the range.
it is stuck and i cant delete it.
the VBA code for the Date Picker is attached hereby:
Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Dim openrange As Range
Dim dates As Date
If (Not Intersect(Target, Range("J5:J1000, K5:K1000, L5:L1000 , M5:M1000, N5:N1000, O5:O1000")) Is Nothing) And (Target.Cells.Count = 1) Then
With Sheets("SHEET1").DTPicker1
.Top = Target.Top
.Left = Target.Offset(0, 1).Left
.Visible = True
If Application.WorksheetFunction.IsNA(Target) Then Target = ""
.LinkedCell = (Target.Address)
.Value = Format(DTPicker1.Value, "dd/mm/yyyy")
End With
End If
End Sub
the problem you can see that i am on L22 but the display is also on O21

Related

Making a safe entry with a searchable multiple list Excel VBA

I am making an entry data for that I implemented an internet code to make a comboBox with the capacity to search inside a range of cells. The code works well if in the data validating list the value of the reference is an absolute range (per ex. ='Posibles valores'!$P$4:$P$103). But I would like to not update the reference and instead just create news rows on a table. I have tried these ways:
Create a named range that has a function =INDIRECT("Tubos_Tipo[Tubos-Tipos]" in it. Making a reference to a table. But in this case the comboBox just shows the table reference. This way worked to me on data validation list but not without using this macro.
On the other hand trying to use a function another function =OFFSET('Posibles valores'!$P$4;0;0;COUNTA('Posibles valores'!$P$4:'Posibles valores'!$P$1000);1)) but It gives an error you cannot use reference operators (such as unions, intersections, and intervals), array constants, or the LAMBDA function for Validation of specific data.
This is the code of the searcheable comboBox with some comments:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xCombox As OLEObject
Dim xStr As String
Dim xWs As Worksheet
Dim xArr
Set xWs = Application.ActiveSheet
On Error Resume Next
Set xCombox = xWs.OLEObjects("TempCombo")
With xCombox
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
If Target.Validation.Type = 3 Then
Target.Validation.InCellDropdown = False
Cancel = True
xStr = Target.Validation.Formula1
xStr = Right(xStr, Len(xStr) - 1)
Set nameRangeFormula = Application.ActiveWorkbook.Names(xStr)
nameRangeFormula = Right(nameRangeFormula, Len(nameRangeFormula) - 1)
Dim rangeFormula As Range
'Application.ActiveSheet.Select
'Set rangeFormula = Range(nameRangeFormula).Select
Set rangeFormula = Names(nameRangeFormula).RefersToRange
If xStr = "" Then Exit Sub
With xCombox
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
'.ListFillRange = rangeFormula
.ListFillRange = xStr
If .ListFillRange = "" Then
xArr = Split(xStr, ",")
Me.TempCombo.List = xArr
End If
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
End If
Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 9
Application.ActiveCell.Offset(0, 1).Activate
Case 13
Application.ActiveCell.Offset(1, 0).Activate
End Select
End Sub
I don't know much from VBA more than what I have learnt this past two weeks. Firstly I would like to know if there is some way to execute a function and return the Range. In the case that it is not possible the only way in my mind to resolve thisto create a method with the table header referece as parameter create a Range.
The last two things, the macro functionality of searchable is used on each column with list validation that the code is in it. Is there any way to select exclusive the worksheet and columns they can be used. This code allows to give a wrong result. It just highlights with a green corner the wrong fields, any way to remove if It's wrong or make a pop up windows like the default data validation.
Thanks in advance
Edit: I achived to activate the comboBox for just the columns I wanted with this If Target.Column = 3 Or Target.Column = 4 Then before doing the first big if. For what I'm understanding each "page" has it's own code. Is this correct?

VBA - Color borders of a cell range white

I am trying to make a cell look borderless by coloring the edges white. Here is my code and the goddarn thing does not work. Thanks for correcting it.
Dim cel As Range
For Each cel In Range(Cells(4, 1), Cells(Worksheets("Deliverable-Epic-Story Progress").UsedRange.Rows.Count, 15))
With cel.Borders
If .Item(xlEdgeTop).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeTop).Color = vbWhite
End If
If .Item(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeBottom).Color = vbWhite
End If
End With
Next
** UPDATE **
Pictures attached if it helps.
I used this code from the link #Big Ben shared.
Private Sub TurnOffGridLines(target As Worksheet)
Dim view As WorksheetView
For Each view In target.Parent.Windows(1).SheetViews
If view.Sheet.Name = target.Name Then
view.DisplayGridlines = False
Exit Sub
End If
Next
End Sub
And I am calling that sub like this and it errors out. My worksheet name is "Deliverable-Epic-Story Progress"
TurnOffGridLines ("Deliverable-Epic-Story Progress")
First, it seems like you are re-inventing functionality. I'd just hide the grid.
Based on this question, I would add the following:
Private Sub TurnOffGridLines(target As Worksheet)
Dim view As WorksheetView
For Each view In target.Parent.Windows(1).SheetViews
If view.Sheet.Name = target.Name Then
view.DisplayGridlines = False
Exit Sub
End If
Next
End Sub
And pass it a Worksheet variable, not a String.
Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets("Deliverable-Epic-Story Progress")
TurnOffGridLines target:=ws
And it you just want to do this manually, View > Gridlines or Alt+W+V+G.
You don't have to turn them white, set the .LineStyle property of Borders to none.
.Borders(xlEdgeBottom).LineStyle = xlLineStyleNone
If you actually want the bottom white:
.Borders(xlEdgeBottom).ColorIndex = 2
or
.Borders(xlEdgeBottom).Color = RGB(255, 255, 255)

Excel-Changing pictures automatically using cell value in vba

I want to automatically insert a picture in cell AH32 depending on value in AB32.
I am able to insert the picture but not depending on the value in AB32. How do I fix this please?
Code:
Sub Picture()
Range("AH32").Select
Dim picname As String
If Range("AB32").Value < 85# Then
picname = "C:\Users\20149308\Desktop\sucess\images" & ".png" 'Link to the Picture
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
ElseIf Range("AB32").Value >= 85# Then
picname = "C:\Users\20149308\Desktop\sucess\succ" & ".jpg" 'Link to the Picture
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
End If
Range("AH32").Select
Application.ScreenUpdating = True
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo" 'Shows message box if picture not found
Exit Sub
End Sub
Here is one way of writing it in a more condensed form with some basic error checking.
Option Explicit
Sub Picture()
Application.ScreenUpdating = True
Dim testRange As Range
Dim picname As String
Set testRange = ActiveSheet.Range("AB32")
If IsEmpty(testRange) Then
MsgBox "No value in cell AB32"
Exit Sub
End If
Select Case True
Case Not IsNumeric(testRange.Value2)
MsgBox "Value in cell AB32 is not numeric"
Exit Sub
Case testRange.Value2 < 85#
picname = "C:\Users\20149308\Desktop\sucess\images" & ".png"
Case testRange.Value2 >= 85#
picname = "C:\Users\20149308\Desktop\sucess\succ" & ".jpg"
End Select
On Error GoTo ErrNoPhoto
ActiveSheet.Pictures.Insert(picname).Select
With Selection
.Left = Range("AH32").Left
.Top = Range("AH32").Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With
Application.ScreenUpdating = True
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo" 'Shows message box if picture not found
Exit Sub
End Sub
You can do this without any VBA using the Camera. You can find this by selecting File followed by Options and Customize Ribbon and adding the camera icon to your ribbon.
Create a blank worksheet and adjust the column width/row height so each of your pictures will sit within the boundaries of a cell (in my example I'm using B2 and B4).
Select one of these cells and click the camera icon to take a photo of it.
Switch to your reporting sheet and click on it to paste the photo you just took. You'll see a picture of the cell you originally clicked on within a picture frame that you can rotate and resize.
Paste your two pictures into the cells on the blank worksheet. The picture frame on the reporting sheet will now display whichever picture is in the cell you clicked on.
Create a named range using this formula (adjust sheet names to suit):
=IF(Sheet1!$AB$32<85,Sheet2!$B$2,Sheet2!$B$4) - absolute referencing is important here.
I called the range DisplayImage.
Select the picture frame and change the formula in the formula bar to =DisplayImage.
The image will now update based on the value in cell AB32.

Vba macros in Excel - Range.Validation.Type Causes 1004

I am trying to prevent pasting over a dropdown in Excel. The code I came up with is the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'turn off events so this routine is not continuously fired
Set BoroughTrainingTookPlaceIn = Range("BoroughTrainingTookPlaceIn")
Set StartTimeOfSession = Range("StartTimeOfSession")
Set TypeOfSession = Range("TypeOfSession")
Set BikeabilityLevelRatingBeforeTraining = Range("BikeabilityLevelRatingBeforeTraining")
Set BikeabilityLevelRatingAfterTraining = Range("BikeabilityLevelRatingAfterTraining")
If RangesIntersect(Target, BoroughTrainingTookPlaceIn) Then
PreventPaste (BoroughTrainingTookPlaceIn)
ElseIf RangesIntersect(Target, StartTimeOfSession) Then
PreventPaste (StartTimeOfSession)
ElseIf RangesIntersect(Target, TypeOfSession) Then
PreventPaste (TypeOfSession)
ElseIf RangesIntersect(Target, BikeabilityLevelRatingBeforeTraining) Then
PreventPaste (BikeabilityLevelRatingBeforeTraining)
ElseIf RangesIntersect(Target, BikeabilityLevelRatingAfterTraining) Then
PreventPaste (BikeabilityLevelRatingAfterTraining)
End If
Application.EnableEvents = True
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
Function RangesIntersect(ByVal Range1 As Range, ByVal Range2 As Range) As Variant
Dim intersectRange As Range
Set intersectRange = Application.Intersect(Range1, Range2)
If intersectRange Is Nothing Then
RangesIntersect = False
Else
RangesIntersect = True
End If
End Function
Private Function PreventPaste(ByVal Target As Range) As Variant
If Not HasValidation(Target) Then
Application.Undo
MsgBox "Invalid value. Please chose a value from the dropdown"
End If
End Function
The line x = r.Validation.Type is causing Error 1004 when I try to copy by dragging a cell in the dataValdiation Range. I am using named ranges.
I looked trough the already opened questions but didn't find answer there. All help is greatly appreciated.
EDIT:
I was trying to cut it and not to copy and paste it by dragging. Still for newbies like me might be useful to record the macro first and see what you are actually doing

How to disable button depending on blank cell

I have a problem. How do I enable/disable a button depending on whether a cell is empty or not.
In excel sheet named "Sheet1" I have 1 button called "CommandButton1" assigned to a macro named "If_empty".
What I need to do is if column "F2" is empty I want to disable the button but if column "F2" has any values in it for the button to be enabled.
How do I achieve this?
Help is greatly appreciated
As #Slai and Doug suggested, use the code below (add it to your Worksheet_Change event).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
Dim i, j As Integer
Set WatchRange = Range("F2")
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
If Target.Value = "" Then
CommandButton1.Enabled = False
Else
CommandButton1.Enabled = True
End If
End If
End Sub

Resources