I have two columns in excel spreadsheet. 'Distance' and 'Bearing'. As shown in the below image the first distance value is 58.23 and the bearing value for the first distance is -43.45 as shown in image 1.
I want to divide the distance into a meter each, as in 1,2,3,4...58 (so that 58 meters is broken into 58 equal parts) in one column (Distance1) and fill the next column with value -43.45 (Bearing1) from 1 to 58 as shown in image 2. I want to generate this in a new sheet.
After the first row, the code should go to the next distance row (ie, 20.70) and repeat step 1, but should add it to the previous 1 to 58 cells. ie 58, 59, 60 and fill the bearing1 column value with -42.48 and so on. As shown in Image 3.
I am new to VBA and will be really helpful if someone can help me out :)
A couple of row counters and Single type variables and a few Loops will do.
As you are new to VBA, will not consider performance optimization here. Hope below untested code will get you love doing VBA programming. Code should self-explain the purpose. Ask if don't understand.
Here assuming Data is on columns A and B as per screenshot. And the output data will be from D2 and E2 downwards.
Option Explicit
Sub FillDistanceBearing()
Dim lRowOutput As Long ' Row to start storing data
Dim lDistance As Long ' Incremental Distance
Dim lLoop1 As Long, lLoop2 As Long ' Row counters for Loops
Dim gDistance As Single, gBearing As Single ' Variables to store the points
lRowOutput = 2 ' Row to start storing the series at some column
lDistance = 0 ' Start from zero
' #1 - Loop the main data
lLoop1 = 2 ' For A2
Do Until IsEmpty(Range(lLoop1, "A"))
' Store the Distance|Bearing values into variables
gDistance = Range(lLoop1, "A").Value
gBearing = Range(lLoop1, "B").Value
' #2 - Loop the distance from 1 to the integer value of the distance
For lLoop2 = 1 To Int(gDistance)
lDistance = lDistance + 1 ' Increment distance
' Store the values in Columns D and E for Distance1, Bearing1
Range(lRowOutput, "D").Value = lDistance
Range(lRowOutput, "E").Value = gBearing
lRowOutput = lRowOutput + 1 ' Move to next row for storing output
Next
lLoop1 = lLoop1 + 1 ' Move to next data row
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.