Look Up Z Table Value, Problem with second if 0

Bernieg

Board Regular
Joined
Jan 1, 2009
Messages
147
Office Version
  1. 365
Platform
  1. Windows
The code below works to look up Z table value if the number is i.e. 1.258745
However if the number is 1.5 i have errors "Paul" is used to look up the Z column
"Peter" is used to find the value

Thanks in advance

Bernie

Get Z Table Value.xlsm
ABCDEFGHIJKLMNOPQ
1
2z0.00000000.01000000.02000000.03000000.04000000.05000000.06000000.07000000.08000000.0900000USL1.52158966500
30.00000000.00000000.00400000.00800000.01200000.01600000.01990000.02390000.02790000.03190000.0359000USL, 2 Decimal 1.52000000000
40.10000000.03980000.04380000.04780000.05170000.05570000.05960000.06360000.06750000.07140000.0753000
50.20000000.07930000.08320000.08710000.09100000.09480000.09870000.10260000.10640000.11030000.1141000Paul ( Z Colum Ref )1.50000000000
60.30000000.11790000.12170000.12550000.12930000.13310000.13680000.14060000.14430000.14800000.1517000Peter ( Second Decimal )2.00000000000
70.40000000.15540000.15910000.16280000.16640000.17000000.17360000.17720000.18080000.18440000.1879000
80.50000000.19150000.19500000.19850000.20190000.20540000.20880000.21230000.21570000.21900000.2224000Z table Result0.43570000000
90.60000000.22570000.22910000.23240000.23570000.23890000.24220000.24540000.24860000.25170000.2549000
100.70000000.25800000.26110000.26420000.26730000.27040000.27340000.27640000.27940000.28230000.2852000Value can be changed
110.80000000.28810000.29100000.29390000.29670000.29950000.30230000.30510000.30780000.31060000.3133000
120.90000000.31590000.31860000.32120000.32380000.32640000.32890000.33150000.33400000.33650000.3389000
131.00000000.34130000.34380000.34610000.34850000.35080000.35310000.35540000.35770000.35990000.3621000
141.10000000.36430000.36650000.36860000.37080000.37290000.37490000.37700000.37900000.38100000.3830000
151.20000000.38490000.38690000.38880000.39070000.39250000.39440000.39620000.39800000.39970000.4015000
161.30000000.40320000.40490000.40660000.40820000.40990000.41150000.41310000.41470000.41620000.4177000
171.40000000.41920000.42070000.42220000.42360000.42510000.42650000.42790000.42920000.43060000.4319000
181.50000000.43320000.43450000.43570000.43700000.43820000.43940000.44060000.44180000.44290000.4441000
191.60000000.44520000.44630000.44740000.44840000.44950000.45050000.45150000.45250000.45350000.4545000
201.70000000.45540000.45640000.45730000.45820000.45910000.45990000.46080000.46160000.46250000.4633000
211.80000000.46410000.46490000.46560000.46640000.46710000.46780000.46860000.46930000.46990000.4706000
221.90000000.47130000.47190000.47260000.47320000.47380000.47440000.47500000.47560000.47610000.4767000
232.00000000.47720000.47780000.47830000.47880000.47930000.47980000.48030000.48080000.48120000.4817000
242.10000000.48210000.48260000.48300000.48340000.48380000.48420000.48460000.48500000.48540000.4857000
252.20000000.48610000.48640000.48680000.48710000.48750000.48780000.48810000.48840000.48870000.4890000
262.30000000.48930000.48960000.48980000.49010000.49040000.49060000.49090000.49110000.49130000.4916000
272.40000000.49180000.49200000.49220000.49250000.49270000.49290000.49310000.49320000.49340000.4936000
282.50000000.49380000.49400000.49410000.49430000.49450000.49460000.49480000.49490000.49510000.4952000
292.60000000.49530000.49550000.49560000.49570000.49590000.49600000.49610000.49620000.49630000.4964000
302.70000000.49650000.49660000.49670000.49680000.49690000.49700000.49710000.49720000.49730000.4974000
312.80000000.49740000.49750000.49760000.49770000.49770000.49780000.49790000.49790000.49800000.4981000
322.90000000.49810000.49820000.49820000.49830000.49840000.49840000.49850000.49850000.49860000.4986000
333.00000000.49870000.49870000.49870000.49880000.49880000.49890000.49890000.49890000.49900000.4990000
343.10000000.49900000.49910000.49910000.49910000.49920000.49920000.49920000.49920000.49930000.4993000
353.20000000.49930000.49930000.49940000.49940000.49940000.49940000.49940000.49950000.49950000.4995000
363.30000000.49950000.49950000.49950000.49960000.49960000.49960000.49960000.49960000.49960000.4997000
373.40000000.49970000.49970000.49970000.49970000.49970000.49970000.49970000.49970000.49970000.4998000
383.50000000.49980000.49980000.49980000.49980000.49980000.49980000.49980000.49980000.49980000.4998000
393.60000000.49980000.49980000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.4999000
403.70000000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.4999000
413.80000000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.49990000.4990000
42
Z Table


VBA Code:
Function LastCell(ws As Worksheet) As Range
  Dim LastRow, LastCol
' Error-handling is here in case there is not any
' data in the worksheet
  On Error Resume Next
  With ws
' Find the last real row
  LastRow = .Cells.Find(What:="*", _
  SearchDirection:=xlPrevious, _
  SearchOrder:=xlByRows).Row
' Find the last real column
  LastCol = .Cells.Find(What:="*", _
  SearchDirection:=xlPrevious, _
  SearchOrder:=xlByColumns).Column
  End With
' Finally, initialize a Range object variable for
' the last populated row.
  Set LastCell = ws.Cells(LastRow, LastCol)
  Cells(LastRow, LastCol).Select
 If IsEmpty(LastRow) Then
  MsgBox ("No data in worksheet")
  End
  End If
End Function
Sub GetZTableValue()

Worksheets("Z Table").Select
Range("O3:O8").Select
    Selection.ClearContents
    Range("a1").Select


USL = Worksheets("Z Table").Range("O2")
USL = Round(USL, 2)
Range("o3") = USL
'**********************************

'Get USL Z Table value
Dim Boby, Paul As Single
Paul = Mid(USL, 1, 3) ' i.e 1.5 ( Original No 1.52 )
Peter = Right(USL, 1) ' i.e 2 Second decimal

Range("o5") = Paul
Range("o6") = Peter

'Stop

rlc = LastCell(ActiveSheet).Address(False, False)
lr = ActiveCell.Row
lc = ActiveCell.Column

For r = 3 To lr '
If Not IsEmpty(Cells(r, 2)) And (Cells(r, 2)) = Paul Then
Dingo = Peter + 3
Boby = Cells(r, Dingo).Value
'bernie = (0.5 - Boby)
End If
Next r

'Stop

Range("o8") = Boby

'font turns white, don't know why
Range("O8").Select
With Selection.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0 'Black
End With
Range("a1").Select

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi @Bernieg .
Thanks for posting on MrExcel board.

I think it would work if you change this line:
VBA Code:
Peter = Right(USL, 1) ' i.e 2 Second decimal

For this
VBA Code:
Peter = Right(Format(USL, "0.00"), 1) ' i.e 2 Second decimal

--------------------------------​

But I would like to give you the following reduced code.
Change all your macro to the following:
(It's a weird way to name variables, but I kept the same name in the variables so you're familiar)
VBA Code:
Sub GetZTableValue()
  Dim Usl As Double, Paul As Double, Peter As Double
  Dim f As Range
 
  With Worksheets("Z Table")
    .Range("O3:O8").ClearContents
   
    Usl = Round(.Range("O2").Value, 2)
    Paul = Round(Usl, 1)
    Peter = Right(Format(Usl, "0.00"), 1)
   
    Set f = .Range("B:B").Find(Paul, , xlFormulas, xlWhole)
    If Not f Is Nothing Then
      .Range("O3").Value = Usl
      .Range("O5").Value = Paul
      .Range("O6").Value = Peter
      .Range("O8").Value = f.Offset(, Peter + 1)
    End If
  End With
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
Hi @Bernieg .
Thanks for posting on MrExcel board.

I think it would work if you change this line:
VBA Code:
Peter = Right(USL, 1) ' i.e 2 Second decimal

For this
VBA Code:
Peter = Right(Format(USL, "0.00"), 1) ' i.e 2 Second decimal

--------------------------------​

But I would like to give you the following reduced code.
Change all your macro to the following:
(It's a weird way to name variables, but I kept the same name in the variables so you're familiar)
VBA Code:
Sub GetZTableValue()
  Dim Usl As Double, Paul As Double, Peter As Double
  Dim f As Range
 
  With Worksheets("Z Table")
    .Range("O3:O8").ClearContents
  
    Usl = Round(.Range("O2").Value, 2)
    Paul = Round(Usl, 1)
    Peter = Right(Format(Usl, "0.00"), 1)
  
    Set f = .Range("B:B").Find(Paul, , xlFormulas, xlWhole)
    If Not f Is Nothing Then
      .Range("O3").Value = Usl
      .Range("O5").Value = Paul
      .Range("O6").Value = Peter
      .Range("O8").Value = f.Offset(, Peter + 1)
    End If
  End With
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Hi Dante
Thank you both solutions worked, your version is far more elegant though.
I don't program as much as I'd like to so will have to make a copy for referencing in the future.

Bernie
 
Upvote 0
Hi Dante

Just spotted an issue, if a number is 1.588 "USL" rounds it to 2 decimal places i.e. 1.59
However "Paul" also rounds it up to 1.6.
"Peter then extracts the last digit of "USL" = 9
New figure becomes 1.69 we gained 0.1

I have added my original code and that works, is there a way to format " Paul " as you formatted "Peter"

VBA Code:
Usl = Round(.Range("O2").Value, 2) ' = 1.59

'Paul = Round(Usl, 1) ' = 1.6
Paul = Mid(Usl, 1, 3) ' = 1.59

Peter = Right(Format(Usl, "0.00"), 1) ' = 9

Thanks for all your help
Bernie
 
Upvote 0
In my macro.

Change this:
VBA Code:
Paul = Round(Usl, 1)

For this:
VBA Code:
Paul = Mid(Usl, 1, 3)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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