我有一个Excel Spreedsheet,它需要在单元格A2中包含数据,因此我需要能够单击该按钮,并将该数据作为特定名称(无扩展名)导出到网络驱动器中的文本文件中。
然后,我还需要同一张纸上的另一个按钮,以将单元格E2和F2保存为相同的网络位置(作为单独的文件名(无扩展名))。
我有以下代码,但是当每个按钮只需要保存特定单元格中的数据时,它将表上的所有数据复制到文件名。我猜这是我无法弄清楚的RANGE选项。
Private Sub CommandButton1_Click()
Dim path As String
Dim filename1 As String
path = "S:\"
filename1 = "scal0091"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=path & filename1 & ".", FileFormat:=xlText
End Sub
有什么建议吗?
谢谢!
答案 0 :(得分:0)
这应该做:
Private Sub CommandButton1_Click()
Dim path As String
Dim filename1 As String
'New Variables:
Dim MyVal as Variant
Dim FileNum as Integer
Dim FSO as Object
Dim MySheet as Sheet
path = "S:\"
filename1 = "scal0091"
Set MySheet = Thisworkbook.Sheets("MySheetName") 'Put Your Sheet Name Here
MyVal = MySheet.range("A2").value 'Your specific cell value
Application.DisplayAlerts = False 'why?
'Create a new text file:
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CreateTextFile path & filename1, False
'Write to Text File
FileNum = FreeFile()
Open path & filename1 for output as #FileNum
Print #FileNum, MyVal
Close #FileNum
Set FSO = Nothing
End Sub