我是VBA的初学者,我想知道我的代码出了什么问题。
Sub Worksheet_Calculate()
Application.EnableEvents = False
If
code
End If
Application.EnableEvents = True:
If Range("A3") <> Range("B4") Then
Columns("B1:F10000").Select
ChDir "C:\Users\Francesco\Desktop"
ActiveWorkbook.SaveAs:="C:\Users\Francesco\Desktop\TXT.txt", _
FileFormat:=xlTextMSDOS, CreateBackup:=False
End If
End Sub
我的重点是:当条件If Range("A3") <> Range("B4")
为真时,我希望vba创建一个.txt。
我在哪里错了?
谢谢。
答案 0 :(得分:1)
尝试一下:
Error CS0012 The type 'Package' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
根据您的代码,您将保存整个工作表。所以我看不到选择列的任何意义。
答案 1 :(得分:0)
Dim wb as workbook, wb1 as workbook
set wb = activeworkbook
set wb1 = application.workbooks.new
If Range("A3") <> Range("B4") Then
wb.activate
Columns("B1:F10000").copy
wb1.activate
range("A1").paste
ChDir "C:\Users\Francesco\Desktop"
wb1.SaveAs:="C:\Users\Francesco\Desktop\TXT.txt", FileFormat:=xlTextMSDOS, CreateBackup:=False
call wb1.close(false)
End If
未经测试。
答案 2 :(得分:0)
尝试一下
Function SimpleWriteToText(ByVal FileName, ByVal strText, Optional Wtype = "output")
If Wtype = "output" Then
Open FileName For Output As #1
Else
Open FileName For Append As #1
End If
Print #1, strText;
Close #1
End Function
用法
SimpleWriteToText ThisWorkbook.path & "\Text.txt", "Hello" ' Append
SimpleWriteToText ThisWorkbook.path & "\Text.txt", "Hello", "output" ' NewOne