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

2024-11-12 20:03:12
推荐回答(2个)
回答1:

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

回答2:

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