我有一个excel 2007工作簿,当前的工作表名为a,现在我想要的是,
当用户单击工作表a中的按钮时,它应该会问,
要导入哪个csv文件,
询问新的工作表用户想要的名称( csv文件要放在哪里)..现在说简化用户说'b‘。
在将“工作表a”复制到新工作表b之后,将csv导入到新工作表中,用逗号分隔,允许在复制的工作表中覆盖现有单元格。
什么是基本的启动级代码来完成所有这些任务?
我将感谢在这方面的任何帮助。
谢谢
销售
发布于 2013-07-23 08:48:33
试试这个:
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 Subhttps://stackoverflow.com/questions/17800539
复制相似问题