Enter date and Number to find the result

Pinkster69

New Member
Joined
Jun 19, 2012
Messages
48
Hi Guys,
Just new to VBA, so I hope you can help me on a problem I am having!

I created a spreadsheet that I use for Hat Hire where I indicate on the intersection between the Date and the Hat SKU Number what Days the Hat is out on Hire with the number "1". I have Dates listed on the first row and I have the Hat SKU numbers on the first Column. I have attached a sample Spreadsheet to give you a better Idea of what I am talking about.

Firstly I am looking for help with code whereby if I enter a Date "Textbox1" eg: 02/06/2012 and a Hat SKU Number "Textbox 2" eg: SKU101 on a userform it will enter the number "1" in the intersecting cell as shown below.

Secondly if I enter a Date "Textbox1" and a Hat SKU Number "Textbox 2" on a different userform then if it finds the number "1" it will display "Hired" in "Textbox 3" and if it find the cell blank it displays "Available" in Textbox 3.

Hope you can help guys?




[TABLE="width: 50"]
<tbody>[TR]
[TD]Hat[/TD]
[TD]01/06/2012[/TD]
[TD]02/06/2012[/TD]
[TD]03/06/2012[/TD]
[TD]04/06/2012[/TD]
[/TR]
[TR]
[TD]SKU100[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SKU101[/TD]
[TD][/TD]
[TD="align: center"]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SKU102[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SKU103[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Here is my take on what you asked for (put it in the forms' module):

Code:
'--------------------------------------------------------
'--------------------------------------------------------
Const DataSheet = "Data"        ' Sheet with data
Const HatDates = "B17:E17"      ' Address of dates
Const HatSKUs = "A18:A21"       ' Address of SKUs
Const FLAGGEDTEXT = "Hired"     ' Text to display
Const FLAG = 1                  ' flag
'--------------------------------------------------------
'--------------------------------------------------------
Private Sub SetFlag()    ' Set flag in table
    On Local Error GoTo errors
    With Sheets(DataSheet)
        Set DateFound = .Range(HatDates).Find(what:=TextBox1.Value)
        Set SKUFound = .Range(HatSKUs).Find(what:=TextBox2.Value)
        .Cells(SKUFound.Row, DateFound.Column) = FLAG
    End With
    Exit Sub
errors:
    MsgBox "Error: " & Err.Description
End Sub
'--------------------------------------------------------
'--------------------------------------------------------
Private Sub GetFlag()      ' display "Hired" if flagged
    On Local Error GoTo errors
    With Sheets(DataSheet)
        Me.TextBox3 = ""
        Set DateFound = .Range(HatDates).Find(what:=TextBox1.Value)
        Set SKUFound = .Range(HatSKUs).Find(what:=TextBox2.Value)
        If .Cells(SKUFound.Row, DateFound.Column) = FLAG Then Me.TextBox3 = FLAGGEDTEXT
    End With
    Exit Sub
errors:
    MsgBox "Error: " & Err.Description
End Sub
'--------------------------------------------------------
'--------------------------------------------------------

However, rather than TextBoxes for data entry, I suggest ListBoxes/ComboBoxes which would limit the user to the data which is valid. Also, why not have just one form?
 
Upvote 0
Hi tLowry,

Thanks so much for that!
I created a userform with Textbox1,2,3, names the worksheet "Data" and inserted the code into the forms module but unfortunitly it didn't work?
I must be doing something wrong or forgotten something!

How would you suggest i initiate the code to work

Derek
 
Upvote 0
Did anything happen when it didn't work?

Did you set all the const to your values?

Code:
Const DataSheet = "Data"        ' Sheet with data
Const HatDates = "[B]B17:E17"  [/B]    ' Address of dates
Const HatSKUs = [B]"A18:A21"[/B]       ' Address of SKUs
Const FLAGGEDTEXT = "Hired"     ' Text to display
Const FLAG = 1                  ' flag
 
Upvote 0
To initiate the code, I put two CommandButtons on the form

One called "SetHiredFlag" with code:

Code:
Private Sub SetHiredFlag_Click()
    SetFlag
End Sub

The other called "HiredCheck" with code:

Code:
Private Sub HiredCheck_Click()
    GetFlag
End Sub
 
Upvote 0
Hi tLowry,

Did what you suggested, An error comes up when I press either Command Button

"Error: Object Variable or With Block Variable not set"


Rechecked all my values..

Sheet Name is "Data"
My HatDates are in Cells B17 to E17 - 01/06/12, 02/06/2012 etc
My HatSKUs are in Cells A18 to A21 - SKU100, SKU101 etc
Made sure Textbox1 relates to Date entries
Made sure Textbox2 relates to SKU entries
Made sure Textbox3 is blank for result when I press CommandButtons
CommandButtons are set as requested.

The Dates that I have in Cells B17 to E17 I formatted them to "Dates" on the Format Cells dialog box on the Excel sheet it self. Would this be correct or should I have left them to General Format? I switched it back to General Format and still came up with the Error!! No joy there.

Did you do a test on the code yourself with a userform and did it work for you?

regards

Derek
 
Upvote 0
Are the ranges with data in sheet "Data"?

Can you post all the code in the form?

I have two listboxes ""LBSKUs" and "LBDates" on the form

Here is all my code...


Code:
'--------------------------------------------------------
'--------------------------------------------------------
Const DataSheet = "Data"        ' Sheet with data
Const HatDates = "B17:E17"      ' Address of dates
Const HatSKUs = "A18:A21"       ' Address of SKUs
Const FLAGGEDTEXT = "Hired"     ' Text to display
Const FLAG = 1                  ' flag
'--------------------------------------------------------
'--------------------------------------------------------
Private Sub SetFlag()    ' Set flag in table
    On Local Error GoTo errors
    With Sheets(DataSheet)
        Set DateFound = .Range(HatDates).Find(what:=TextBox1.Value)
        Set SKUFound = .Range(HatSKUs).Find(what:=TextBox2.Value)
        .Cells(SKUFound.Row, DateFound.Column) = FLAG
    End With
    Exit Sub
errors:
    MsgBox "Error: " & Err.Description
End Sub
'--------------------------------------------------------
'--------------------------------------------------------
Private Sub GetFlag()      ' display "Hired" if flagged
    On Local Error GoTo errors
    With Sheets(DataSheet)
        Me.TextBox3 = ""
        Set DateFound = .Range(HatDates).Find(what:=TextBox1.Value)
        Set SKUFound = .Range(HatSKUs).Find(what:=TextBox2.Value)
        If .Cells(SKUFound.Row, DateFound.Column) = FLAG Then Me.TextBox3 = FLAGGEDTEXT
    End With
    Exit Sub
errors:
    MsgBox "Error: " & Err.Description
End Sub
'--------------------------------------------------------
'--------------------------------------------------------
Private Sub HiredCheck_Click()
    GetFlag
End Sub
Private Sub LBDates_Click()
    Me.TextBox1 = Me.LBDates.Value
End Sub

Private Sub LBSKUs_Click()
    Me.TextBox2 = Me.LBSKUs.Value
End Sub
Private Sub SetHiredFlag_Click()
    SetFlag
End Sub
Private Sub UserForm_Initialize()
    Me.LBDates.Clear
    Me.LBSKUs.Clear
    For Each cCell In Range("HatDates").Cells
        Me.LBDates.AddItem cCell
    Next cCell
    For Each cCell In Range("HatSKUs").Cells
        Me.LBSKUs.AddItem cCell
    Next cCell
End Sub
 
Upvote 0
That worked a charm, Thank you!
One last question if I may ask! Is it possible to check if the Hat is out for a 5 day period using the same Textbox or using multiple Textboxes representing the 5 Day Hire Period? If the code finds that at least 1 or more days from the initial Date entry has the Flag 1 in one of the cells the Textbox or Textboxes will indicate "Hired"


Regards

Derek
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top