如何将列名映射到其短名称

时间:2014-01-10 09:09:51

标签: excel excel-vba vba

我试图将工作簿中工作表中应用程序ID,编号,地址等名称的列复制并粘贴到工作簿2中的另一个工作表,其中列名为addr,appid,num(列的短名称)在第一张表。)任何人都可以帮助如何将列应用程序ID映射到appid,数字到num等等。第二个工作表中的顺序可能不同。索引也不会在这里工作。这是我试过的代码,但是问题是它被复制,因为它就像源中的第一列被复制到目标中的第一列。我想要的是映射第一列说" applicationid"到目的地工作表中的列" appid"(这可能不是目的地的第一列)..提前谢谢

     Dim thisWB As Workbook, wb1 As Workbook, wb2 As Workbook
     Dim rng1 As Range, rng2 As Range
     Dim tmp
     On Error GoTo errorHandler
     Set thisWB = ThisWorkbook
     tmp = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", 1, "Choose file #1")
     If tmp = False Then Exit Sub  ' File not choosen'
     Workbooks.Open Filename:=tmp, ReadOnly:=True
     Set wb1 = ActiveWorkbook
    Set rng1 = Application.InputBox(Prompt:="Please Select Range to copy", Title:="Range Select", Type:=8)

   tmp = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", 1, "Choose file #2")
   If tmp = False Then ' File not choosen'
   wb1.Close
   Exit Sub
   End If
   Workbooks.Open Filename:=tmp
   Set wb2 = ActiveWorkbook

   Set rng2 = Application.InputBox(Prompt:="Please Select Range where to paste", Title:="Range Select", Type:=8)

 rng1.Copy Destination:=rng2

  wb1.Close
  wb2.Close
  Exit Sub

'Error handler'
 errorHandler:
 MsgBox Err.Description, vbCritical, "Error!"

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

Function GetShortName(longName As String)
    Select Case longName
    Case "application id": GetShortName = "appid"
    Case "number": GetShortName = "num"
    Case "address": GetShortName = "addr"
    Case Else
        Err.Raise 9999, , "Unknown long name"
    End Select
End Function