how to pass on collected value to next step ?

RompStar

Well-known Member
Joined
Mar 25, 2005
Messages
1,200
Ok, I have a script that pops up two windows and ask for two date values, one from the J column and one from the K column...

Then it confirms what the user entered and goes on to the next step, I am having trouble passing on the collected values to the next step, as I am still learning the syntax.

the two values that I collect are, here is the script as it stands now....

start script -----

Sub FindByDateRange()

' User will be asked to enter Start Date from J and End Date from K to
' figure out comppliance on a selected date range from pop-up windows.

Dim rngFirst As Excel.Range
Dim rngSecond As Excel.Range
Dim c As Range
Dim LastRow As Long
LastRow = Range("N11").End(xlDown).Row

' Ask for the starting Date Range from Column J, Scheduled Pickups

StartOverOne:
On Error Resume Next
Set rngFirst = Application.InputBox("Enter Starting Range from _
Scheduled Pickups, Column J", "Select Range", , , , , , 8)
On Error GoTo 0
If TypeName(rngFirst) <> "Range" Then
Exit Sub

' 10 represents the, 10th column or column J

ElseIf rngFirst.Column <> 10 Then
MsgBox "Wrong column"
GoTo StartOverOne
Else
Set rngFirst = rngFirst(1)
If Not IsDate(rngFirst.Value) Then
MsgBox "Select a date format"
GoTo StartOverOne
End If
End If

' Aks for the ending Date Range from Column K,
' Acctual Pickup Date ( -2 for compliance )

StartOverTwo:
On Error Resume Next
Set rngSecond = Application.InputBox("Enter Ending Date Range _
from Pickup, Column K", "Select Range", , , , , , 8)
On Error GoTo 0
If TypeName(rngSecond) <> "Range" Then
Exit Sub

' 11 represents the, 11th column or column K

ElseIf rngSecond.Column <> 11 Then
MsgBox "Wrong column"
GoTo StartOverTwo
Else
Set rngSecond = rngSecond(1)
If Not IsDate(rngSecond.Value) Then
MsgBox "Select a date"
GoTo StartOverTwo
End If
End If

MsgBox "You selected: " & rngFirst.Value & " through " _
& rngSecond.Value & " ", , "Select Range"

' Do something with the date values

' Pass on values from rngFirst and rngSecond to the rest of the script...

' ------------------------ LAST PHAZE --------------------------
' Pass on collected values...

On Error GoTo Finish

' Step #1
' Fill the selected range of cells in column N with this fomula
' then go to the final STEP #2

Range("n11:rngFirst", rngSecond.End(xlUp).Row).Formula = "=NETWORKDAYS(J11,K11)-1"

' Step #2
' After selected cell ranges are fill with NETWORKDAYS formula
' run through them again,


Set c = Range("N11:N" & LastRow)

For Each Item In c
If Val(Item) > 2 Or Val(Item) < 0 Then
Item.Font.Bold = True
Item.Font.COLOR = vbRed
Else
Item.Font.Bold = True
Item.Font.COLOR = vbBlack
End If
Next Item

Finish:
End Sub

end script -----

Anyways, I am having trouble passing on the two variables values correctly to the STEP #1 after ---- LAST PHAZE ----


Basically, columns J and K have date values, they are compared with the
formula in column N and then the results are highlighted with red is < 0, > 2, and anything that is 0, 1 and 2 stays black.

thanks guys.. your help is appreciated.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
see, also that AutoFilter I don't think is working right:

here is a cut of my sheet, I selected 12/28/2004 - 12/29/2004

and it's selecting ranges in column N that are not within this
range, what do you think could be wrong, something with the ?

Rng.AutoFilter Field:=1, Criteria1:=">=" & CLng(BeginDate)
Rng.AutoFilter Field:=1, Criteria1:="<=" & CLng(EndDate)
Set Rng = Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1, 1)
Set Rng = Rng.SpecialCells(xlCellTypeVisible)
Rng.Offset(0, 4).FormulaR1C1 = "=NETWORKDAYS(RC[-4],RC[-3])-1"
Rng.AutoFilter

??
MASTER_5_final.xls
JKLMN
1112/31/20041/10/2005Closeddk
1212/28/20041/4/2005Closeddk5
1312/29/20041/4/2005Closeddk4
1412/28/20041/4/2005Closeddk5
1512/29/200412/29/2004Closedkm0
1612/29/200412/31/2005Closeddk262
1712/30/20041/12/2005Closeddk
1812/28/20041/4/2005Closeddk5
1912/29/20041/4/2005Closeddk4
2012/28/200412/30/2004Closeddk2
2112/29/200412/31/2004Closeddk2
2212/30/20041/4/2005Closeddk
2312/30/20041/5/2004Closeddk
241/4/20041/5/2005Closeddk262
251/4/20041/5/2005Closeddk262
2612/30/20041/4/2005Closeddk
271/6/20041/7/2005Closeddk263
2812/30/200412/30/2004Closeddk
2912/31/20041/6/2005Closeddk
3012/31/20041/6/2005Closeddk
311/4/20041/13/2005Closeddk268
3212/31/20041/7/2005Closeddk
3312/31/20041/11/2005Closeddk
3412/30/20041/4/2005Closeddk
351/4/20041/4/2005Closeddk261
3612/30/20041/4/2005Closeddk
371/6/20041/4/2005Closeddk260
Master
 
Upvote 0
Oh sorry, I forgot to alter the code, which was filtering columns J and K, to just filter on column K with an AND.

So replace:

Code:
Rng.AutoFilter Field:=1, Criteria1:=">=" & CLng(BeginDate) 
Rng.AutoFilter Field:=1, Criteria1:="<=" & CLng(EndDate)

with:

Code:
Rng.AutoFilter Field:=1, Criteria1:=">=" & CLng(BeginDate),  Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)
 
Upvote 0
when u look at Row11, COlumn J you see 12/31/2004

I tried to make a search for dates

12/31/2004 - 12/31/2004 just for that 1 day..

and finds them correctly, but skips the first row that also has the date 12/31/2004 in J, what would I need to adjust so that it reads row 11 too ?

For now I simply added another row above 11, so that first row that's skipped is empty anyways...

Also I would like to understand this, because my ultimate goal is to learn this through-out.

If you have time and don't mind, can you explain what does what in ?

Rng.AutoFilter Field:=1, Criteria1:=">=" & CLng(BeginDate), Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)

Set Rng = Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1, 1)
Set Rng = Rng.SpecialCells(xlCellTypeVisible)
Rng.Offset(0, 4).FormulaR1C1 = "=NETWORKDAYS(RC[-4],RC[-3])-1"
Rng.AutoFilter

thank you.
 
Upvote 0
also another problem, which isn't a large problem right now, but I want my statistics to be right.. as you see I use that NETWORKDAYS formula, which skips weekends Sat and Sun to do it's calculations based on the working days Mon - Fri.

There are also some holidays, in 2005, we have here 6 more holidays left.

like for example in May coming up on May 30 is memorial day and that's a monday, I would want that skipped.

Any good way to tell networkdays to look at a list of holidays, maybe I can put them in the sheet or in the VB it's self and for any day that's a holiday it would discount that as -1 from the compliance, because no one is at work, so we can't count it.
 
Upvote 0
ok I listed my holidays: 8 of them for 2005, in cells c2:c9

one date on each cell of column C

normally it would be

=NETWORKDAYS(J12,K12)-1

now it's

=NETWORKSDAYS(J12,K12,C2:C9)-1

is this right ?

also how would that be written using your format ?

Rng.Offset(0, 4).FormulaR1C1 = "=NETWORKDAYS(RC[-4],RC[-3])-1"
 
Upvote 0
Record a macro while entering the formula and you will get the code.

For your missing date, I assumed a header was in row 11. If it's in row 10 change the starting row accordingly.
 
Upvote 0
do you think I did this right ?

Set c = Range("N11:N" & LastRow)
For Each Item In c
If Val(Item) > 2 Or Val(Item) < 0 Then
Item.Font.Bold = True
Item.Font.COLOR = vbRed
Else
Item.Font.Bold = True
Item.Font.COLOR = vbBlack
End If
If Val(Item) < -20000 Then
Item.Value = "N/A"
End If
Next Item

I want also any negative values that come up as part of the formular calculations to be replaced with the word N/A, it compiles without errors, but doesn't seem to work, did I do it wrong ?
 
Upvote 0

Forum statistics

Threads
1,215,245
Messages
6,123,842
Members
449,129
Latest member
krishnamadison

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