使用宏将整个列的英尺转换为Excel中的米

时间:2017-01-24 18:32:57

标签: excel vba

我不是程序员我是一名试图操纵喷气式飞机数据的航空电子技术人员。我创建了一个宏来消除所有不必要的列,并为其添加列和值,以便正确显示为kml。但是,我无法找到一种方法将宏转换为数千个单元格中的米。

Sub sbVBS_To_Delete_Specific_Multiple_Columns()
Sheets("GRT Flight Data    Log_raw").Range("A:B,H:I,K:L,P:P,AB:AH,AK:AN,AQ:AQ,AT:AT,AZ:BJ").EntireColumn.Delete

Columns("G:G").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("G1").Value = "AppendDataColumnsToDescription"

Range("G2:G363").Value = "Yes"

Range("F1").Value = "IconAltitude"

Columns("H:H").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("H1").Value = "IconAltitudeMode"

Range("H2:H363").Value = "MSL"

Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("I1").Value = "Icon"

Range("I2:I363").Value = "222"

Columns("J:J").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("J1").Value = "IconHeading"

Range("J2:J363").Value = "line-0"

Columns("K:K").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("K1").Value = "IconScale"

Range("K2:K363").Value = ".5"

Columns("L:L").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("L1").Value = "IconLineColor"

Range("L2:L363").Value = "Cyan"

Columns("M:M").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("M1").Value = "LineStringColor"

Range("M2:M363").Value = "Lime"

End Sub

2 个答案:

答案 0 :(得分:4)

您可以使用 CONVERT 功能将英尺转换为公里:

  

= CONVERT(A2,"英尺""公里&#34)

enter image description here

在VBA中,您可以将其称为工作表函数:

Application.WorksheetFunction.Convert(.Value, "ft", "km") 

要在代码中包含此内容,请在代码中添加以下行:

Dim lastrow as long

lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row

For x=2 to lastrow
    Cells(x,6).value = Application.WorksheetFunction.Convert(Cells(x,6).value, "ft", "km")
Next x

如果您希望第363行成为最后一行,我就会改变您的代码。请注意,如果最后一行必须是363以外的其他内容,则xLng可以更新为任何数字:

Sub sbVBS_To_Delete_Specific_Multiple_Columns()

Dim xLng As Long

xLng = 363

Sheets("GRT Flight Data    Log_raw").Range("A:B,H:I,K:L,P:P,AB:AH,AK:AN,AQ:AQ,AT:AT,AZ:BJ").EntireColumn.Delete

Columns("G:G").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G1").Value = "AppendDataColumnsToDescription"
Range("G2:G" & xLng).Value = "Yes"

Range("F1").Value = "IconAltitude"
For x = 2 To xLng
    Cells(x, 6).Value = Application.WorksheetFunction.Convert(Cells(x, 6).Value, "ft", "km")
Next x

Columns("H:H").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("H1").Value = "IconAltitudeMode"
Range("H2:H" & xLng).Value = "MSL"

Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Value = "Icon"
Range("I2:I" & xLng).Value = "222"

Columns("J:J").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("J1").Value = "IconHeading"
Range("J2:J" & xLng).Value = "line-0"

Columns("K:K").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K1").Value = "IconScale"
Range("K2:K" & xLng).Value = ".5"

Columns("L:L").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("L1").Value = "IconLineColor"
Range("L2:L" & xLng).Value = "Cyan"

Columns("M:M").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("M1").Value = "LineStringColor"
Range("M2:M" & xLng).Value = "Lime"

End Sub

答案 1 :(得分:0)

要从列中获取所有值,转换它们并将结果写入不同的列,请按照以下示例进行操作

Sub ConvertFtToMetersExample(ByRef r_in as Range, ByRef r_out as Range)

    Dim nr as long, i as Long
    'Count the rows on the input range
    nr = r_in.Rows.Count
    'Set output range to be the same size as the input range
    Set r_out = r_out.Resize(nr,1)
    'Set temporary array storage in memory
    Dim values() as Variant
    'Read all values in one swoop
    values = r_in.Value2
    'Go through all of the and transform them.
    'This is a fast operation as it is done in memory and away from the GUI
    For i=1 to nr
        values(i,1) = (12#*0.0254)*values(i,1)
    Next i
    'Export out all the values to the r_out range
    r_out.Value2 = values

Exit Sub

修改1

您还可以使用CONVERT()工作表功能

values(i,1) = WorksheetFunctions.Convert(values(i,1), "ft", "m")