emptiness_void
New Member
- Joined
- Mar 29, 2021
- Messages
- 7
- Office Version
- 2019
- Platform
- Windows
Hello, hope everything is good!
I will try to be the most clear with the goal I'm tryin to achieve:
I have a Sheet1 where I would like to enter a given record with some information about Country; Transit Type; Incoterm; Forwarder; Currency info.
This would be almost what I needed (it shouldn't show blanks, and that happens because on the data validation source I wrote a range for the A column in Sheet2), a combo box that appears on the cell I want to enter the data with search option and a auto complete option!
The validation info. is on Sheet2
I'm only testing for the country column for now this following code where I found and edited on Internet:
The code seems good in general, because it would allow me also to replicate the "scenario" for transit type, and the others columns, just by adding data validation options to those cells! I aiming to a solution similar to this because it seems more automated and wouldn't need to write code for different columns in Sheet1, I think.
I'm a beginner in vba coding in excel, and I'm trying to learn the best I can but it has been hard, especially because I don't have anyone on my team to share this questions!
If you have any questions or need more information, please contact me!
Thank you if you have read till this far and I hope you can help me!
I will try to be the most clear with the goal I'm tryin to achieve:
I have a Sheet1 where I would like to enter a given record with some information about Country; Transit Type; Incoterm; Forwarder; Currency info.
This would be almost what I needed (it shouldn't show blanks, and that happens because on the data validation source I wrote a range for the A column in Sheet2), a combo box that appears on the cell I want to enter the data with search option and a auto complete option!
The validation info. is on Sheet2
I'm only testing for the country column for now this following code where I found and edited on Internet:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim r As Range
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
' value 3 to confirm there is the data validation
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
'--------- this is where I think the code doesn't work as I would want, doesn't recognize a dynamic range for example!
.ListFillRange = str
'-----------------
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If
exitHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
errHandler:
Resume exitHandler
End Sub
- The images shown are for a cell from Sheet1 with a data validation source with reference to Sheet2!A:A (although I don't want this solution because it gives me many blanks and the header), it's a Plan B if I can't find anything better :/
- I tried to use the "=OFFSET(Sheet2!A1:A57;1;0;COUNTA(Sheet2!$A:$A)-1;1)" as the source in data validation cells in Sheet1 but the code fails to recognize this as a range!
- This source formula (offset) in my point of view would be the "best" way to do it, because it takes in account new records added in the Sheet2 that someone can add, and it would consider them dynamically!
The code seems good in general, because it would allow me also to replicate the "scenario" for transit type, and the others columns, just by adding data validation options to those cells! I aiming to a solution similar to this because it seems more automated and wouldn't need to write code for different columns in Sheet1, I think.
I'm a beginner in vba coding in excel, and I'm trying to learn the best I can but it has been hard, especially because I don't have anyone on my team to share this questions!
If you have any questions or need more information, please contact me!
Thank you if you have read till this far and I hope you can help me!