如何使用VBA在一个excel里面创造出一个按钮,点击后可选择.CSV文件导入此excel里并运行之后的代码

如何使用VBA在一个excel里面创造出一个按钮,点击以后可以选择任意一个.CSV文件导入此excel里并运行之后的代码,比如复制第几行到第几行什么的。
目前我只写出了之后的代码,至于如何导入excel里面,我在试了很多网上的代码,都没有很成功的导入。求VBA大神帮帮忙

1.插入按钮,在开发工具项-插入表单工具-Button
2.导入代码
Sub Inport_Renkei_CSV_Click()
Dim fTextDir As String, rowIndex As Integer, i As Integer
myFile = Application.GetOpenFilename("连携CSVファイル(*.csv),*.csv")
If VarType(myFile) = vbBoolean Then
Exit Sub
End If
rowIndex = 1
Open myFile For Input As #1
Do While Not EOF(1)
Line Input #1, currLine
If rowIndex > 1 Then
rowDataArr = Split(currLine, Chr(9))
For i = 0 To UBound(rowDataArr)
Cells(rowIndex + 1, i + 1).FormulaR1C1 = rowDataArr(i)
Next i
End If
rowIndex = rowIndex + 1
Loop
Close #1
MsgBox "success"
End Sub追问

导入之后CSV文件的格式完全变了,csv文件中是有数据与数据之前空白单元格的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-04
dim shp1 as shape
set shp1 =sheet1.addformcontrol(xlbuttoncontrol,10,50,100,20)
shp1.textframe.characters.text="按钮"
shp1.onaction=" 宏1"
with active sheet.querrytables.add(connection:="text",path&文件名,range("a1"))
.Textfileplatform=936
. textfilecommadelimiter=true
.Refresh

end with