首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将csv导入Excel,CSV

将csv导入Excel,CSV
EN

Stack Overflow用户
提问于 2013-07-23 02:28:51
回答 1查看 5K关注 0票数 0

我有一个excel 2007工作簿,当前的工作表名为a,现在我想要的是,

当用户单击工作表a中的按钮时,它应该会问,

要导入哪个csv文件,

询问新的工作表用户想要的名称( csv文件要放在哪里)..现在说简化用户说'b‘。

在将“工作表a”复制到新工作表b之后,将csv导入到新工作表中,用逗号分隔,允许在复制的工作表中覆盖现有单元格

什么是基本的启动级代码来完成所有这些任务?

我将感谢在这方面的任何帮助。

谢谢

销售

EN

回答 1

Stack Overflow用户

发布于 2013-07-23 08:48:33

试试这个:

代码语言:javascript
复制
    Public strFile As String

    Sub Main()

    Dim WS As Worksheet

        strFile = Application.GetOpenFilename("Excel workbooks,*.csv*")
        If strFile = "False" Then
            ' the user clicked Cancel
        Else

        y = Right(strFile, Len(strFile) - InStrRev(strFile, "\", -1, vbTextCompare))

        zz = Left(y, InStr(1, y, ".", vbTextCompare) - 1)


        flag = 0
            For k = 1 To Worksheets.Count
                If Sheets(k).Name = zz Then
                    flag = 1
                End If
            Next
                 Set WS = Sheets.Add(After:=Sheets(Worksheets.Count))
            If flag = 0 Then
                 WS.Name = zz
            Else
                MsgBox ("Sheet with same name already exist. Imported to default sheet")
            End If


            importcsv
        End If
    End Sub


    Sub importcsv()
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & strFile, Destination:=Range( _
            "$A$1"))
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17800539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档