Your code should look something like this.
Sub Simulation()
Dim i As Long
Dim Temperature As Long
Dim WindSpeed As Long
Dim SolarRadiation As Long
Dim Humidity As Long
Dim SimulatedValue As Long
With Worksheets("DataLoggerValues")
For i = 1 To 100
Temperature = .Cells(i, 1).Value
WindSpeed = .Cells(i, 2).Value
SolarRadiation = .Cells(i, 3).Value
Humidity = .Cells(i, 4).Value
.Cells(i, 5).Value = (Temperature + SolarRadiation / WindSpeed) ^ 2 + Humidity / 37
Next i
End With
End Sub
You can also use something like this. Just use that function as a normal excel function. You can then drag this with autofill. You can use it after you add the code to a module.
Function Simulation(Temperature As Long, WindSpeed As Long, SolarRadiation As Long, Humidity As Long)
Simulation = (Temperature + SolarRadiation / WindSpeed) ^ 2 + Humidity / 37
End Function
Edit:
An answer to the comment below.
Sub Simulation()
Dim i As Long
Dim Temperature As Long
Dim WindSpeed As Long
Dim SolarRadiation As Long
Dim Humidity As Long
Dim SimulatedValue As Double
With Worksheets("DataLoggerValues")
For i = 1 To 100
Temperature = .Cells(i, 1).Value
WindSpeed = .Cells(i, 2).Value
SolarRadiation = .Cells(i, 3).Value
Humidity = .Cells(i, 4).Value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SimulatedValue = (Temperature + SolarRadiation / WindSpeed) ^ 2 + Humidity / 37
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
.Cells(i, 5).Value = SimulatedValue
Next i
End With
End Sub
I guess you should read something about what objects are in a programming language. You can start with this:
https://analysistabs.com/excel-vba/objects-properties-methods/
What a problem was with your code is:
Worksheets("DataLoggerValues")
is an object. It has some propeties. For example it has a name: "DataLoggerValues".
You can check it with
Sub Test()
MsgBox Worksheets(1).Name
End Sub
If its index is one. It may be different. You can check it using another property of that object.
Sub Test()
MsgBox Worksheets("DataLoggerValues").Index
End Sub
And now we are getting to the problem. Worksheets("DataLoggerValues").Cells(i, 5)
is another object. It is a child object (a child class object).
It is not a single value!!! It is an object. It has different properties and methods. This is why: .Cells(i,5) =...
doesn't make sense. Are you assigning that value to the .Cells(i,5).Address
or .Cells(i,5).Value
or maybe some different property? All of them 'store' values. You can read them or change them.
Maybe I should clear out why you have to use SimulatedValue
differently.
Well, with those first four cells it is simple. You create a variable, e.g. Dim Temperature As Long
. It can store later assigned value somewhere in the computer's memory!!! Just assign some cell's value to it Temperature = .Cells(i, 1).Value
.
You do the same with different variables and perform some calculations. As I did it in the new code, you can store that number (It has to be Double because it has to 'store some numbers after the comma') in another variable (If your Temperature, WindSpeed etc. are not integers then change Long to Double).
Now you can assign that value to the cells value .Cells(i, 5).Value = SimulatedValue
. If you would have added the line SimulatedValue = .Cells(i, 5).Value
somewhere before the calculations, you would just assign to the variable (SimulatedValue) a cell's value (which is 0 if the cell is empty or doesn't exist, I am not an expert. If you try to print the value, you get nothing but you can multiply anything by its value and you get 0). If you then write something like SimulatedValue = 5
you just assign new value to the variable. You don't do anything to that cell object (the cell, the cell's value).
I hope it is clear. I tried to be as concise as I could
I am just a beginner when it comes to vba and I don't know too much about programming so I may have used some terms incorrectly, bear that in mind. Maybe somebody will correct me. :)
Important stuff, when it comes to using floating-point variables (e.g. that Double). Read this:
Whats wrong with this simple 'double' calculation?
Compare double in VBA precision problem
I guess that if you assign that value straight to some cell's value the problem persists, although I haven't read that or try that.
.Cells(i, 5).Value = (Temperature + SolarRadiation / WindSpeed) ^ 2 + Humidity / 37
It may save you some time in the future if you do some calculations with floating-point numbers.