VBA IF statement not finding indicated marker - vba

There is a moment where the program has t = 1 but my if statement wont find it.
what gives?
Most of the question is code to fully experiment with the issue
What i am trying to do with my if statement is to find when t = whole number integers example 1,2,3,4,5 then do stuff to return other results but i cant find the moments when t= 1 so im stuck
Dim neq As Double
neq = 2
Dim e As Double
e = Exp(1)
Dim t_int As Integer
t_int = 5
'''''COUNTERS
Dim i As Integer
Dim j As Integer
Dim colOf As Integer
'''''EQUATION CONTROL
Dim h(3) As Double
Dim n As Double
'''''EQUATION CONTROL
Dim u() As Double
Dim uStar() As Double
Dim uOld() As Double
Dim uEx As Double
'''''EQUATION CONTROL
Dim f() As Double
Dim fOld() As Double
'''''EQUATION CONTROL
Dim t As Double
Dim tOld As Double
Dim tNew As Double
'''''SIZING ARRAY
ReDim u(neq)
ReDim uOld(neq)
ReDim uStar(neq)
ReDim f(neq)
ReDim fOld(neq)
'''''INITAL VAULES
h(1) = 0.1
h(2) = 0.05
h(3) = 0.025
u(1) = 2
u(2) = 0
colOf = 12
For j = 1 To 1
Cells(1, 1 + colOf) = "h(" & j & ") = " & h(j)
Cells(2, 1 + colOf) = "t"
Cells(2, 2 + colOf) = "u(1)"
Cells(2, 3 + colOf) = "u(2)"
Cells(2, 4 + colOf) = "uEx"
For n = 1 To (t_int / h(j))
tOld = t
t = tOld + h(j)
For i = 1 To neq
uOld(i) = u(i)
Next i
For i = 1 To neq
fOld(i) = fDeriv(uOld, tOld, i)
uStar(i) = uOld(i) + h(j) * fOld(i)
Next i
For i = 1 To neq
f(i) = fDeriv(uStar, t, i)
u(i) = uOld(i) + (h(j) * (fOld(i) + f(i))) / 2
Next i
i = i - 1
uEx = 2 * e ^ -t * (Cos((3 ^ 0.5) * t) + ((3 ^ 0.5) ^ -1) * Sin((3 ^ 0.5) * t))
Cells(n + 2, 1 + colOf) = t
Cells(n + 2, 2 + colOf) = u(1)
Cells(n + 2, 3 + colOf) = u(2)
Cells(n + 2, 4 + colOf) = uEx
**If t = 1 Then Debug.Print t**
Next n
colOf = colOf + 5
Next j

Related

Find Method Object Variable Not Set

This is an error I've been trying to figure out for awhile now, my find method is not producing any results and I cannot figure out why.
The code is suppose to search InputSheet for a string, report the row number and start moving information over to Background based on that row number. Then the next .find will find the string in SummaryResults and start moving information from Background, reformat it a bit, and paste to SummaryResults.
My find method is not producing any results and leaves FindRow = Nothing even though the strings are present in the sheets and in the correct Ranges.
This error started occurring after running the macro with another Excel sheet open so maybe the ActiveWorkbook was incorrect, but I have not been able to get it to run since.
Some of the variables shown are from other sections of the code but when I hover over them in the debug mode they are showing what they're suppose to.
Option Explicit
Sub CAESARCONVERSION()
Dim InputSheet As Worksheet, SummaryResults As Worksheet, Background As Worksheet
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim y As Integer
Dim h As Integer
Dim v As Integer
Dim c As Integer
Dim z As Integer
Dim myBook As Workbook
Set myBook = Excel.ThisWorkbook
Set InputSheet = myBook.Sheets("Input Sheet")
Set SummaryResults = myBook.Sheets("Summary Results")
Set Background = myBook.Sheets("Background")
Dim NodeList As Integer
Dim TotalCases As Integer
Dim sMyString As String
Dim Nodes As Variant
Dim FindRow As Range
Dim intValueToFind As String
Dim FindRowNumber As Long
Dim SecondRowNumber As Long
'Clear the last run of macro
Background.Range("A2:A1000").Cells.Clear
Background.Range("C2:I10000").Cells.Clear
SummaryResults.Cells.Clear
'Code that will count the total number of load cases
TotalCases = 0
h = 2
Dim text As String
For v = 12 To 100
If InputSheet.Cells(v, 2).Value <> "" Then
text = LTrim(InputSheet.Cells(v, 2).Value)
Background.Cells(h, 3).Value = text
h = h + 1
TotalCases = TotalCases + 1
Else
GoTo NodeCounter
End If
Next v
NodeCounter:
y = TotalCases - 1
x = 0
Dim LoadCaseList() As Variant
ReDim LoadCaseList(y)
LoadCaseList:
For x = 0 To y
LoadCaseList(x) = Background.Cells(2 + x, 3).text
Next x
j = 2
For i = 17 + TotalCases To 20000 'Need to define how far for the program to search, we may exceed 20000 at some point
If InputSheet.Cells(i, 2).Value <> "" Then
Background.Cells(j, 1).Value = InputSheet.Cells(i, 2).Value
j = j + 1
End If
Next i
With Background
NodeList = Background.Cells(2, 2).Value
Background.Range("AA1:XX" & NodeList + 1).Cells.Clear
End With
ReDim Nodes(NodeList - 1)
v = 0
j = 2
For i = 0 To NodeList - 1
Nodes(i) = Background.Cells(j, 1).Value
j = j + 1
Next i
Headers:
Dim LoadCaseHeader() As String
Dim TypHeader()
TypHeader = Array("Node", "L", "Direction", "Magnitude")
Dim LoadDirections()
LoadDirections = Array("X", "Y", "Z", "MX", "MY", "MZ")
x = 0
z = 0
For x = 0 To NodeList - 1
For z = 0 To TotalCases - 1
SummaryResults.Range(("B" & 2 + (NodeList * 6 + 2) * z) & ":" & "E" & 2 + (NodeList * 6 + 2) * z) = TypHeader()
SummaryResults.Range("A" & 2 + (NodeList * 6 + 2) * z) = Background.Range("C" & 2 + z)
Next z
Next x
'Search rows for the first instance of this value.
LoadCases:
'Code that copies information from the InputSheet to the SummaryResults
Dim LoadCases() As Long
ReDim LoadCases(NodeList, 6)
FindRowNumber = 0
SecondRowNumber = 0
For c = 0 To y
intValueToFind = LoadCaseList(c)
For i = 7 To 31 + TotalCases
With InputSheet
If Trim(Cells(i, 3).Value) = intValueToFind Then
MsgBox ("Found")
Set FindRow = InputSheet.Range("C:C").Find(What:=intValueToFind, LookIn:=xlValues)
FindRowNumber = FindRow.Row
End If
End With
Next i
'MsgBox FindRowNumber
With InputSheet
For i = 0 To NodeList - 1
x = 4
For j = 0 To 5
LoadCases(i, j) = InputSheet.Cells(FindRowNumber + (TotalCases + 3) * i, x)
x = x + 1
Next j
Next i
End With
Background.Range("AC2:AH" & NodeList + 1).Offset(0, c * 7) = LoadCases
For i = 1 To NodeList * 6 * TotalCases
With SummaryResults
If Trim(Cells(i, 5).Value) = intValueToFind Then
Set FindRow = SummaryResults.Range("A:A").Find(What:=intValueToFind, LookIn:=xlValues)
SecondRowNumber = FindRow.Row
GoTo Step2
End If
End With
Next i
Step2:
With SummaryResults
For x = 0 To NodeList - 1
For j = 0 To 5
SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 5) = Background.Cells(x + 2, 29 + j)
SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 3) = TypHeader(1)
SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 4) = LoadDirections(j)
SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 2) = Nodes(x)
Next j
Next x
End With
Next c
End Sub
Any help would be appreciated. EDIT: Uploaded the entire code. Additional information, the code works when not tabbed into excel but will fail when tabbed in a ran again.
The issue seems to be that the LoadCaseList() array is never getting populated. This is your Find statement:
Set FindRow = InputSheet.Range("C:C").Find(What:=intValueToFind, LookIn:=xlValues)
intValueToFind is set by this statement:
intValueToFind = LoadCaseList(c)
But the LoadCaseList() array is populated by the following code which is a label that is never called by a GoTo statement (as far as I can tell):
LoadCaseList:
For x = 0 To y
LoadCaseList(x) = Background.Cells(2 + x, 3).text
Next x
So because the LoadCaseList label statement is never being called by a GoTo statement, the LoadCaseList() array is never being populated so intValueToFind has no value and therefore the Find method has no value to search for (except for maybe the empty string).

The Fastest Permutation Code Permutating Numbers in VB NET

There is a famous and the fastest permutation code without any "function" for VB .Net to permutate numbers just in several rows, that I can't remember unfortunately.
Is there anyone know this code? Or know like this?
Some part of the code is here:
UPDATE: I FOUND IT. ALL THE WORKING CODES HERE:
Dim L(4) As Byte
Dim I As Byte
Dim K As Byte
Dim J As Byte
Dim RESULTS As String
Dim UB, UBm1 As Integer
L = {1, 2, 3, 4, 5}
UB = L.GetUpperBound(0)
UBm1 = UB - 1
Do
I = UBm1
Do While I > 0 And L(I) >= L(I + 1)
I -= 1
Loop
K = L(I)
J = UB
Do While J > 0 And L(J) <= K
J -= 1
Loop
RESULTS = L(0) & "," & L(1) & "," & L(2) & "," & L(3) & "," & L(4)
L(I) = L(J)
L(J) = K
Array.Reverse(L, I + 1, UB - I)
Loop While J
It seems like you're looking for this ...
http://www.cut-the-knot.org/do_you_know/AllPerm.shtml
(2. Lexicographic order and finding the next permutation)
... ?
In case you are, the starting values for I and J are incorrect (they should be 4 and 5 instead of 3 and 4 respectively).
(I know the example uses swap, but that can be replaced with a single colon-delimited line.)
Dim L(4) As Byte
L = {1, 2, 3, 4, 5}
Dim K as Byte
For N as integer = 1 to 120 'No. of permutations: 5!
Dim I As Byte = 4, J as Byte = 5
While L(I - 1) >= L(I)
I -= 1
End While
While L(J - 1) <= L(I - 1)
J -= 1
End While
K = L(I - 1) : L(I - 1) = L(J - 1) : L(J - 1) = K
I += 1 : J = 5
While I < J
K = L(I - 1) : L(I - 1) = L(J - 1) : L(J - 1) = K
I += 1 : J -= 1
End While
Dim RESULT as String = L(0) & "," & L(1) & "," & L(2) & "," & L(3) & "," & L(4)
'Export / print RESULT as you like, e.g. Console.WriteLine or something
Next
For obtaining a set of permutations of a natural number (recommended less than 10 though it can be bigger than that), this VBA Excel sub-routine is very fast and short. I believe it can be easy to convert it to VB.NET. Have a look.
Const P = 5 'Recommended Max P = 9
Dim m As Long, PArray(1 To 1000, 1 To P) 'Recommended Max 9! = 362880
Public Sub PermutationNaturalNumber()
Dim Q(1 To P) As Long
For m = 1 To P: Q(m) = m: Next: m = 0
PermutationGenerator P, Q
Range("A1").Resize(UBound(PArray), P) = PArray: End
End Sub
Sub PermutationGenerator(n As Long, Q() As Long)
Dim i As Long, j As Long, k As Long
If n > 1 Then
For i = 1 To n - 1
PermutationGenerator n - 1, Q
If n Mod 2 = 1 Then j = 1 Else j = i
k = Q(j): Q(j) = Q(n): Q(n) = k
Next
PermutationGenerator n - 1, Q
Else
m = m + 1: For i = 1 To P: PArray(m, i) = Q(i): Next
End If
End Sub
P can be any natural number, in this case {1, 2, 3, 4, 5}. Make sure you change the upper bound of PArray with P! meaning (P*(P-1)*(P-2)*...*1).

Function in VBA returns #Value in one Sheet but works in another

I have a function in VBA that makes a few operations by calling other smaller functions. This function doesn't work in any sheets, just in one. In the sheets it doesn't work it just gets out of the whole algorithm when getting the value from another function.
This is my Function determinarCantidadPorPedido(ByVal material As String, ByVal mes As String)
Dim demanda As Double
Dim pProgramado As Double
Dim numPedidos As Integer
demanda = determinarDemanda(material, mes)
Call contarProductosMateriales
Sheets("LlegadaMateriales").Activate
Dim fila As Integer
Dim columna As Integer
For i = 1 To numMateriales Step 1
If Sheets("LlegadaMateriales").Cells(1 + i, 1) = material Then
fila = 1 + i
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("LlegadaMateriales").Cells(1, j + 1) = mes Then
columna = j + 1
Exit For
End If
Next
pProgramado = Sheets("LlegadaMateriales").Cells(fila, columna)
numPedidos = darCantidadPedidos(determinarDiasMes(mes), darLeadTime(material))
determinarCantidadPorPedido = WorksheetFunction.Round((demanda - pProgramado) / numPedidos, 0)
and it calls Public Function determinarDemanda(ByVal material As String, ByVal mes As String) As Double
Dim filaProducto As Double
Dim numMat As Double
Dim columnaDemanda As Double
Worksheets("ProductosYMateriales").Activate
numMat = Sheets("ProductosYMateriales").Range(Cells(3, 1), Cells(3, 1).End(xlDown)).Rows.Count
For i = 1 To numMat Step 1
If Sheets("ProductosYMateriales").Cells(i + 2, 1) = material Then
filaProducto = i + 2
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("Demanda").Cells(1, j + 2) = mes Then
columnaDemanda = j + 2
Exit For
End If
Next
Dim numProd As Double
numProd = Sheets("ProductosYMateriales").Range(Cells(2, 2), Cells(2, 2).End(xlToRight)).Columns.Count
Dim demanda As Double
demanda = 0
For k = 1 To numProd Step 1
Dim x As Double
x = Sheets("Demanda").Cells(k + 1, columnaDemanda)
Dim y As Double
y = Sheets("ProductosYMateriales").Cells(filaProducto, k + 1)
Dim prod As Double
prod = x * y
demanda = demanda + prod
Next
determinarDemanda = demanda
and Function determinarCantidadPorPedido(ByVal material As String, ByVal mes As String)
Dim demanda As Double
Dim pProgramado As Double
Dim numPedidos As Integer
demanda = determinarDemanda(material, mes)
Call contarProductosMateriales
Sheets("LlegadaMateriales").Activate
Dim fila As Integer
Dim columna As Integer
For i = 1 To numMateriales Step 1
If Sheets("LlegadaMateriales").Cells(1 + i, 1) = material Then
fila = 1 + i
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("LlegadaMateriales").Cells(1, j + 1) = mes Then
columna = j + 1
Exit For
End If
Next
pProgramado = Sheets("LlegadaMateriales").Cells(fila, columna)
numPedidos = darCantidadPedidos(determinarDiasMes(mes), darLeadTime(material))
determinarCantidadPorPedido = WorksheetFunction.Round((demanda - pProgramado) / numPedidos, 0)
One Problem is in this Line:
numMat = Sheets("ProductosYMateriales").Range(Cells(3, 1), Cells(3, 1).End(xlDown)).Rows.Count
when you call Cells, this reference the current active sheet. Therefore you will get different results, if you call the function on different sheets.
You will have to specify from what sheets you want to reference the cells. I guess you want this:
numMat = Sheets("ProductosYMateriales").Range(Sheets("ProductosYMateriales").Cells(3, 1), Sheets("ProductosYMateriales").Cells(3, 1).End(xlDown)).Rows.Count

Excel VBA - Run-time error 1004

When I run the code below I get the annoying 1004 error.
Sub TEST_MACRO()
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim DateRange As Range
Dim i As Integer, nullcounter As Integer, nullcounterov As Integer, tablelength As Integer, tablelengthov As Integer, DateRangeSize As Integer
Dim q As Integer
Set shSource = ThisWorkbook.Sheets("Sheet1")
Set shDest = ThisWorkbook.Sheets("Sheet2")
Set DateRange = shSource.Application.InputBox("Select date", Type:=8)
DateRangeSize = DateRange.Rows.Count
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
For q = 0 To DateRangeSize - 1
shDest.Range("C4:I17").ClearContents
'THIS IS THE CODE FOR ABC
For i = 0 To 3
If IsEmpty(shSource.Cells(DateRange.Row + q, 2 + i)) = True Or shSource.Cells(DateRange.Row + q, 2 + i) = 0 Then
nullcounter = nullcounter + 1
Else
shDest.Cells(4 + i - nullcounter, 4) = shSource.Cells(DateRange.Row + q, 2 + i)
shDest.Cells(4 + i - nullcounter, 5) = shSource.Cells(DateRange.Row + q, 6 + i)
shDest.Cells(4 + i - nullcounter, 3) = shSource.Cells(1, 2 + i)
tablelength = tablelength + 1
End If
Next
'THIS IS THE CODE FOR XYZ
For i = 0 To 6
If IsEmpty(shSource.Cells(DateRange.Row + q, 10 + i)) = True Or shSource.Cells(DateRange.Row + q, 10 + i) = 0 Then
nullcounterov = nullcounterov + 1
Else
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
shDest.Cells(4 + i - nullcounterov, 9) = shSource.Cells(DateRange.Row + q, 17 + i)
shDest.Cells(4 + i - nullcounterov, 7) = shSource.Cells(1, 10 + i)
tablelengthov = tablelengthov + 1
End If
Next
Next
End Sub
The excel sheet I run this on looks like this:
http://i.imgur.com/V7tWTKq.png
The code works for ABC but it doesn't for XYZ. I'm guessing the 0 value cells are messing it up but I don't understand why.
The goal of the code is:
User is prompted to select a range of size DateRangeSize.
For each row in the range the code copies the values of ABC, ABC-D, XYZ and XYZ-D if they aren't 0 and writes them to sheet 2.
If the number of rows in the range is 1, the code works fine. But if the number of rows is greater than 1, I get the 1004 error where this part of the code is highlight:
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
I appreciate the help.
EDIT: I just want to add that the code ALWAYS works for ABC. If the number of rows is 2 then the ABC values for the second row are printed in sheet2 but the code breaks when it attempts to do the same for XYZ.
EDIT 2: I added 0 values to the ABC portion but the code still works for ABC! This is so frustrating.
I figured it out. I defined
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
Outside of the loop so they kept on increasing.
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
Eventually 4 + i << nullcounterov and excel tried to write to a cell that didn't exist.
The fixed code:
Sub TEST_MACRO()
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim DateRange As Range
Dim i As Integer, nullcounter As Integer, nullcounterov As Integer, tablelength As Integer, tablelengthov As Integer, DateRangeSize As Integer
Dim q As Integer
Set shSource = ThisWorkbook.Sheets("Sheet1")
Set shDest = ThisWorkbook.Sheets("Sheet2")
Set DateRange = shSource.Application.InputBox("Select date", Type:=8)
DateRangeSize = DateRange.Rows.Count
For q = 0 To DateRangeSize - 1
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
shDest.Range("C4:I17").ClearContents
'THIS IS THE CODE FOR ABC
For i = 0 To 3
If IsEmpty(shSource.Cells(DateRange.Row + q, 2 + i)) = True Or shSource.Cells(DateRange.Row + q, 2 + i) = 0 Then
nullcounter = nullcounter + 1
Else
shDest.Cells(4 + i - nullcounter, 4) = shSource.Cells(DateRange.Row + q, 2 + i)
shDest.Cells(4 + i - nullcounter, 5) = shSource.Cells(DateRange.Row + q, 6 + i)
shDest.Cells(4 + i - nullcounter, 3) = shSource.Cells(1, 2 + i)
tablelength = tablelength + 1
End If
Next
'THIS IS THE CODE FOR XYZ
For i = 0 To 6
If IsEmpty(shSource.Cells(DateRange.Row + q, 10 + i)) = True Or shSource.Cells(DateRange.Row + q, 10 + i) = 0 Then
nullcounterov = nullcounterov + 1
Else
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
shDest.Cells(4 + i - nullcounterov, 9) = shSource.Cells(DateRange.Row + q, 17 + i)
shDest.Cells(4 + i - nullcounterov, 7) = shSource.Cells(1, 10 + i)
tablelengthov = tablelengthov + 1
End If
Next
Next
End Sub

Using function in VBA excel

I have been working on this code for a while. As you can see after the code line " With ws(2)" there is an if condition. Now, I have multiple to create multiple such If conditions such as for 0.6, 0.7, 0.8 etc. (and each such condition should use a different table of data) {I am posting the excel file link for the tables as well so that you can get an idea} Can I do this using a function or any method which wont require me to write this code again and again for each new condition ?
https://docs.google.com/file/d/0B1DVNSutDHR0QWd2UUJsVDZ1Tm8/edit
Private Sub CommandButton1_Click()
Dim x(1 To 9000) As Double, y(1 To 9000) As Double, x1 As Double, y1 As Double, x2 As Double, y2 As Double, I1(1 To 9000) As Double, I2(1 To 9000) As Double, R1(1 To 9000) As Double, R2(1 To 9000) As Double, a As Double, b As Double, c As Double, d As Double, Result(1 To 9000) As Double
Dim i As Integer, j As Integer, k As Integer, p As Integer, q As Integer, r As Integer, s As Integer, t As Integer
Dim ws As Sheets
Set ws = ActiveWorkbook.Sheets(Array("Sheet1", "PLP-1", "PLP-2"))
For t = 0 To 120 Step 20
For k = 1 To 9000
With ws(1)
I1(k) = .Cells(k + 2, 13).Value
I2(k) = .Cells(k + 2, 14).Value
End With
With ws(2)
Select Case .Cells(6 + t, 1).Value
Case 0.5:
r = 0
s = 0
Case 0.6:
r = 20
s = 1
Case 0.7:
r = 40
s = 2
Case 0.8:
r = 60
s = 2
Case 0.9:
r = 80
s = 3
Case 1:
r = 100
s = 4
Case 1.1:
r = 120
s = 5
End Select
For i = 7 To 22
If (.Cells(i + r, 1).Value <= I1(k)) And (I1(k) <= .Cells(i + r + 1, 1).Value) And Not (IsEmpty(I1(k))) Then
p = i + r
x(k) = I1(k)
x1 = .Cells(i + r, 1).Value
x2 = .Cells(i + r + 1, 1).Value
End If
Next i
For j = 2 To 8
If (.Cells(6 + r, j).Value <= I2(k)) And (I2(k) <= .Cells(6 + r, j + 1).Value) And Not (IsEmpty(I2(k))) Then
q = j + r
y(k) = I2(k)
y1 = .Cells(6 + r, j).Value
y2 = .Cells(6 + r, j + 1).Value
End If
Next j
If p <> 0 And q <> 0 Then
a = .Cells(p, q).Value
b = .Cells(p, q + 1).Value
c = .Cells(p + 1, q).Value
d = .Cells(p + 1, q + 1).Value
End If
If I1(k) = Empty Then
R1(k) = 0
Else
R1(k) = (((y2 - y(k)) / (y2 - y1)) * a) + (((y(k) - y1) / (y2 - y1)) * b)
End If
If I2(k) = Empty Then
R2(k) = 0
Else
R2(k) = (((y2 - y(k)) / (y2 - y1)) * c) + (((y(k) - y1) / (y2 - y1)) * d)
End If
Result(k) = (((x2 - x(k)) / (x2 - x1)) * R1(k)) + (((x(k) - x1) / (x2 - x1)) * R2(k))
End With
With ws(1)
.Cells(k + 2, 15 + s).Value = Result(k)
End With
Next k
Next t
End Sub
Try using a Select Case statement as below:
Dim iStart As Long, iEnd As long, jStart As Long, jEnd As Long
'...
With ws(2)
Select Case .Cells(6, 1).Value
Case 0.5:
iStart = 7: iEnd = 22
jStart = 2: jEnd = 7
Case 0.6:
'Same as above but substitute new values for iStart etc.
End Select
For i = iStart To iEnd
'DO STUFF WITH i
Next i
For j = jStart To jEnd
'DO STUFF WITH j
Next j
End With
EDIT: Updated to reflect needs clarified in comments
A more in-depth explanation and usage guide for Select Case can be found here
With regard to your looping, if I understand your code, you need to loop through each "table", but your I and J refer to absolute addresses. What you want is to have I and J be relative to the desired table.
I just used values of 2 to 7, but if the tables are different sizes, you could certainly determine that with code; or even read them into a variant array and do your testing on the array (would often be faster).
So something like the following (pseudo code)
Option Explicit
'N is the Value that defines the proper table
Function DoYourThingOnProperRange(N As Double)
Dim C As Range
Dim I As Long, J As Long
With Sheet1.Columns(1)
Set C = .Find(what:=N, after:=Sheet1.Cells(Rows.Count, "A"), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
If Not C Is Nothing Then
Set C = C.CurrentRegion 'C is now set to the proper table
'DoYourThing
'Here's just a dummy routine
For I = 2 To 7
For J = 2 To 7
Debug.Print C(I, J).Address
Next J
Next I
Else
'some kind or error routine for non-existent table
End If
End With
End Function