将列和State缩写合并为完整的州名

时间:2017-06-27 20:21:25

标签: excel string vba excel-vba

我是VBA的新手,并且一直在努力完成这项任务。但没有运气。

我的目标是结合H列(州)和G栏(城市)和以' - '表示国内地址。同时用全名替换所有美国州的缩写。

另外,对于国际地址,组合列J(国家)+' - '+列G(城市)

Sub Move_City()

Sheets("Sheet1").Select
Rows("1:26").Select
Selection.ClearContents

Dim sh As Worksheet
Dim Row As Long
Dim CurrentRow As Long
Dim rng As Range
Dim StateNm("J") As String

    Select Case TCASE("J")
    Case "CA": StateNm = "California"
    Case "NY": StateNm = "Newyork"

    Set sh = Sheets("Sheet1")

    For Each rng In Cells(Column.Count, "J")

    If Columns("J").Cells = "United States" Then
    sh.Range("W").Value = "J" & " - " & "StateNm"

    ElseIf Columns("J").Cells <> "United States" Then
    sh.Range("W").Value = "J" & " - " & "G"


End Sub

当我执行代码时,它给了我一个编译错误:类型不匹配,突出显示StateNm("J") As String。任何人都可以就此错误给我任何建议吗?

任何帮助都将受到高度赞赏。非常感谢你,提前!

1 个答案:

答案 0 :(得分:0)

如果您正在寻找更新列W,国内State - City和国际Country - City,您可以尝试这样的事情:

Sub Move_City()
    Dim i As Integer
    Range("W2", Range("W2").End(xlDown)).ClearContents
    For i = 2 To WorksheetFunction.CountA(Columns(10)) 'assuming 1st row is for heading and there's no blank row in between 1st & last non-blank row
        If Range("J" & i) = "United States" Then Range("W" & i) = StateNm(Range("H" & i)) & " - " & Range("G" & i)
        If Range("J" & i) <> "United States" Then Range("W" & i) = Range("J" & i) & " - " & Range("G" & i)
    Next i
End Sub
Function StateNm(s As String) As String
    Select Case s
        Case "CA": StateNm = "California"
        Case "NY": StateNm = "Newyork"
    End Select
End Function