我正在使用Excel for Mac,v16.53,操作系统 Catalina v10.15.7
我有一个名为SCRIPT的Excel工作簿,有两张工作表。
工作表1有数据输入区,工作表2将这些输入编辑到一个伪表中。工作表1中的数据随着每个新的受访者而变化。
工作表2中的数据位于A、B、H、I和J列中。它是不连续的,并不总是填充第1行。
我可以将这五列复制到名为Telesales-Leads-TODAY 'S DATE的新csv文件中。
问题是当已经存在Telesales-Leads-TODAY 'S DATE文件时。
该脚本应该:
1.如果电话销售-销售线索-今天的日期文件不存在:
重新开始。
复制/粘贴新脚本数据并保存Telesales-Leads-TODAY 'S DATE文件。
1.如果电话销售-销售线索-今天的日期文件确实存在:
将新数据从SCRIPT工作簿复制到Telesales-Leads-TODAY 'S DATE文件的第一个100%空列。
以csv格式保存同名文件(电话销售-销售线索-今天的日期)。
它在从SCRIPT工作簿复制数据之后但在有机会完全打开Telesales-Leads-TODAY 'S DATE文件之前抛出错误。
我正在使用MsgBox进行调试。
Sub BackUpScriptData()
Dim strFileName As String
Dim strFileExists As String
Dim finalcolumn As Integer
Dim firstemptycolumn As Integer
Dim csvOpened As Workbook
Dim oneCell As Range
Dim myCSVFileName As String
Dim myWB As Workbook
Dim tempWB As Workbook
Dim rngToSave As Range
Dim col As String
Dim ColumnNumber As Integer
Dim ColumnLetter As String
Dim colstart As String
Dim CellAddress As String
Dim TestChar As String
Dim NumberToLetter As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
On Error GoTo err
strFileName = "/Users/XXXXXXXX/Library/Group Containers/XXXXXXXX.Office/User Content.localized/Startup.localized/Excel/" & "Telesales-Leads-" & VBA.Format(VBA.Now, "mm-dd-yyyy") & ".csv"
strFileExists = Dir(strFileName)
If strFileExists = "" Then
MsgBox strFileName & " ~~~~~~~~doesn't exist"
Set myWB = ThisWorkbook
myCSVFileName = "/Users/XXXXXXXX/Library/Group Containers/XXXXXXXX.Office/User Content.localized/Startup.localized/Excel/" & "Telesales-Leads-" & VBA.Format(VBA.Now, "mm-dd-yyyy") & ".csv"
Set rngToSave = Range("A1:B69,H1:J69")
rngToSave.Copy
Set tempWB = Application.Workbooks.Add(1)
With tempWB
.Sheets(1).Range("A1").PasteSpecial xlPasteValues
.SaveAs FileName:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
.Close
End With
Else
Set myWB = ThisWorkbook
Set rngToSave = Range("A1:B69,H1:J69")
rngToSave.Copy
Set csvOpened = Workbooks.Open(FileName:=strFileName)
MsgBox "csvOpened is " & csvOpened
With csvOpened
Set oneCell = Range("A1")
Do While WorksheetFunction.CountA(oneCell.EntireColumn)
Set oneCell = oneCell.Offset(0, 1)
Loop
MsgBox "oneCell.Column is " & oneCell.Column
End With
CellAddress = Cells(1, ColNum).Address
For i = 2 To Len(CellAddress)
TestChar = Mid(CellAddress, i, 1)
If TestChar = "$" Then Exit For
NumberToLetter = NumberToLetter & Mid(CellAddress, i, 1)
Next i
MsgBox "colstart is " & colstart
With csvOpened
.Sheets(1).Range(colstart & "1").PasteSpecial xlPasteValues
.SaveAs FileName:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
.Close
End With
End If
err: MsgBox "failed to copy."
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
1条答案
按热度按时间bzzcjhmw1#
创建新工作簿或更新现有工作簿的代码基本相同,唯一的区别是要粘贴数据的列。由于这是一个csv文件,因此UsedRange是确定最后一个清除列的简单方法。