Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a value to a text box in user form based on the activecell

Status
Not open for further replies.

2ks

Technical User
Jan 13, 2007
54
GB
Evening

I am trying to get a text box to display a value (date) based on any cell that has been double clicked on from an excel sheet.

The user form loads up but I cannot get the txtbox to display the date from the activecell.

Thanks
 




Well, exactly WHAT does your code look like? My crystal ball is clouded over.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Many thanks

This is the coding in the Workbook that enables the double click in a cell to load user form:

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

iSheet = ActiveSheet.Name
iColumn = ActiveCell.Column
irowno = ActiveCell.Row
icell = ActiveCell.Value

If iSheet = "Daily TEMPLATE" And iColumn = 2 And irowno > 8 And irowno < 841 Then
Cancel = True

frmFront.Show

End If

End Sub

Then the user form loads and this is my code:


Private Sub frmFront()

'Get row number
irow = ActiveCell.Row

'Where CELL_MIDATE is a global cons
txtDate.Value = Range(CELL_MIDATE & irow).Value

End Sub
 



Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    
    If Sh.name = "Daily TEMPLATE" And Sh.Column = 2 And Sh.Row > 8 And Sh.Row < 841 Then
    Cancel = True
       
   frmFront.Show
         
    End If
    
End Sub
Private Sub frmFront()

    'Get row number
    irow = ActiveCell.Row
    
    'Where CELL_MIDATE is a global cons
    txtDate.Value = Range([b]CELL_MIDATE[/b] & irow).Value
    
End Sub
What is in CELL_MIDATE???

What function does frmFront have? Seems ABSOLUTELY NONE! You could have run this code from the DoubleClick Event as easily!!!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
CELL_MIDATE is a global constant and is set to "B" to represent column B where the date will always be.

I tried frmFront_Initialize/Open/Activate to give it some purpose but usually had runobject problems especially with initialise.
 
This now brings the date into play as expected so many many thanks.

The date is reversed ie 2/1/2008 2nd Jan 08 is displayed as 1/2/08. I have experienced this before and do not really know why it occurs?
 




"The date is reversed ie 2/1/2008 2nd Jan 08 is displayed as 1/2/08. I have experienced this before and do not really know why it occurs? "

It's just a FORMAT display setting. faq68-5827.

"I tried frmFront_Initialize/Open/Activate to give it some purpose but usually had runobject problems especially with initialise."
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    
    If Sh.name = "Daily TEMPLATE" And Sh.Column = 2 And Sh.Row > 8 And Sh.Row < 841 Then
    Cancel = True
       
    with frmFront
      .txtDate.Value = Range(CELL_MIDATE & Sh.row).Value
      .Show
    end with
         
    End If
    
End Sub


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

2ks,

One of the things that can cause this is the "Locale (location)" setting under Format-Cells-Number. If this is set to "Enlish (United Kingdom)" vs "Enlish (United States)" you WILL get the UK date format, which is MM/DD/YY vs the DD/MM/YY format we're used to here in the United States.

Hope this helps.

[glasses]

----------------------------------------------------------------------------------
[small][ponder]"How many surrealists does it take to screw in a lightbulb?"[/small]
"Two -- one to hold the giraffe, the other to put the clocks in the bathtub." [lol]
 
To avoid US date/number settings, don't link cells with VBA controls or pass values. This always returns US formats used in VBA. Instead set text:
Code:
.txtDate.Text = Format(Range(CELL_MIDATE & Sh.row).Value,"AnyDateFormat")


combo
 
I'd use this:
.txtDate.Text = Range(CELL_MIDATE & Sh.row).Text

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top