Reputation: 1466
I have 2 values, where: A2
is a latitude and B2
is a longitude. Both changes on 0.25
step.
I would like to use these values to autofill the table using loop.
The idea is, that for every single value (e.g 49
) I need another few values (for instance -2
, -1.75
, -1.5
, -1.25
, etc.) set in the same row, as per in the picture attached.
Firstly this loop (referring to one, latitude value):
Sub calc2()
Dim i As Single
With Worksheets("9")
.UsedRange.clear
.Range("a1").Value = "Input"
.Range("f1").Value = "Output"
For i = 40 To 55 Step 0.25
Worksheets("1_GENERAL").Range("A2").Value = i
With .Cells(Rows.Count, 1).End(xlUp)
.Offset(1, 0).Value = i
.Offset(1, 1).Value = Worksheets("4.MINUTES").Range("BL371").Value
End With
Next i
End With
End Sub
, which makes an autofill in 2 column (latitude value in 1st and output value in 2nd). Secondly I was trying to modify this loop in order to vertical autofill, which allow me to set values in rows (1st row a longitude value, 2nd row output value). Next steps should contain only an output values corresponding to relevant lon, lat value.
The loop looks like this:
Sub calc_full()
Dim i As Single
With Worksheets("9")
.UsedRange.clear
.Range("a1").Value = "Input"
.Range("b1").Value = "Output"
.Range("c1").Value = "Input"
.Range("d1").Value = "Output"
For i = 40 To 45 Step 0.25
For k = -2 To 2 Step 0.25
Worksheets("1_GENERAL").Range("A2").Value = i
With .Cells(Rows.Count, 1).End(xlUp)
.Offset(1, 0).Value = i
.Offset(1, 1).Value = Worksheets("4.MINUTES").Range("BL371").Value
End With
With .Cells(Cols.Count, 1).End(xlUp)
.Offset(2, 0).Value = k
.Offset(2, 1).Value = Worksheets("4.MINUTES").Range("BL371").Value
End With
Next i
Next k
End With
End Sub
However it doesn't work. I must have mix something. Basically my output value is located in different worksheet (4.MINUTES) and I would like to paste it in one place for different coordinates, making a table eventually (see an example in the attachment). Any ideas how to do this? I will be grateful a lot. Thank you in advance.
Upvotes: 1
Views: 187
Reputation: 1466
I was trying your code, but the problem is, that no error shown but code doesn't work.
Sub autofill_example()
Dim i As Long
Dim LAT As Double
Dim LON As Double
Dim Lastrow As Long
Dim Lastcolumn As Long
Dim Times As Integer
With Sheets("9")
LAT = Sheets("1_GENERAL").Range("A2").Value
LON = Sheets("1_GENERAL").Range("B2").Value
Times = (55 - LAT) / 0.25
For i = 1 To Times
Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
LAT = LAT + 0.25
.Range("A" & Lastrow + 1) = LAT
Next i
Debug.Print LAT
Times = (LON + 7.75) / 0.25
For i = 1 To Times
Lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
LON = LON - 0.25
.Cells(1, Lastcolumn + 1) = LON
Next i
End With
End Sub
Could you advise about the With value? My LON and LAT values are on separate sheet.
Upvotes: 0
Reputation: 8220
@Mariusz Krukar import the values 49 & 25 as in the picture below and try:
Option Explicit
Sub test()
Dim i As Long
Dim LAT As Double
Dim LON As Double
Dim Lastrow As Long
Dim Lastcolumn As Long
Dim Times As Integer
With wsTest
LAT = .Range("A2").Value
LON = .Range("B1").Value
Times = (55 - LAT) / 0.25
For i = 1 To Times
Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
LAT = LAT + 0.25
.Range("A" & Lastrow + 1) = LAT
Next i
Debug.Print LAT
Times = (LON + 7.75) / 0.25
For i = 1 To Times
Lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
LON = LON - 0.25
.Cells(1, Lastcolumn + 1) = LON
Next i
End With
End Sub
the result is like the picture:
Upvotes: 1