如何打开以工作表中的单元格命名的工作簿?
How to open a workbook named after a cell in the worksheet?
我正在尝试将一个 txt 文件转换为 excel 文件。我有这个代码。问题是我的 txt 文件每天都会根据日期使用不同的名称。因此,我的工作簿中有一个单元格具有相同的名称,我想用它来引用正确的工作簿。
当我按 F5 键时,我在调用的文件名上收到一个需要常量表达式的错误(第 3 行:" & varCellvalue & ".xls")
我要打开的文件名在C1.
您知道如何解决此错误吗?
我的代码:
Sub Convert()
DimvarCellvalue As Long
varCellvalue = Range("C1").Value
Const txtFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"
Const xlsFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Payments Holidays"
Dim CurrentFile As String: CurrentFile = Dir(txtFldrPath & "\" & "*.txt")
Dim strLine() As String
Dim LineIndex As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
While CurrentFile <> vbNullString
LineIndex = 0
Close #1
Open txtFldrPath & "\" & CurrentFile For Input As #1
While Not EOF(1)
LineIndex = LineIndex + 1
ReDim Preserve strLine(1 To LineIndex)
Line Input #1, strLine(LineIndex)
Wend
Close #1
With ActiveSheet.Range("A1").Resize(LineIndex, 1)
.Value = WorksheetFunction.Transpose(strLine)
.TextToColumns Other:=True, OtherChar:="|"
End With
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.Copy
ActiveWorkbook.SaveAs xlsFldrPath & "\" & Replace(CurrentFile, ".txt", ".xlsx"), xlOpenXMLWorkbook
ActiveWorkbook.Close False
ActiveSheet.UsedRange.ClearContents
CurrentFile = Dir
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
在这一行
Const txtFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"
您已将 txtFldrPath 声明为常量,因此不能对其使用变量 varCellvalue
将 varCellvalue
声明为变量 Dim txtFldrPath As String
然后根据需要分配值
Dim txtFldrPath As String
txtFldrPath = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"
我正在尝试将一个 txt 文件转换为 excel 文件。我有这个代码。问题是我的 txt 文件每天都会根据日期使用不同的名称。因此,我的工作簿中有一个单元格具有相同的名称,我想用它来引用正确的工作簿。
当我按 F5 键时,我在调用的文件名上收到一个需要常量表达式的错误(第 3 行:" & varCellvalue & ".xls")
我要打开的文件名在C1.
您知道如何解决此错误吗?
我的代码:
Sub Convert()
DimvarCellvalue As Long
varCellvalue = Range("C1").Value
Const txtFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"
Const xlsFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Payments Holidays"
Dim CurrentFile As String: CurrentFile = Dir(txtFldrPath & "\" & "*.txt")
Dim strLine() As String
Dim LineIndex As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
While CurrentFile <> vbNullString
LineIndex = 0
Close #1
Open txtFldrPath & "\" & CurrentFile For Input As #1
While Not EOF(1)
LineIndex = LineIndex + 1
ReDim Preserve strLine(1 To LineIndex)
Line Input #1, strLine(LineIndex)
Wend
Close #1
With ActiveSheet.Range("A1").Resize(LineIndex, 1)
.Value = WorksheetFunction.Transpose(strLine)
.TextToColumns Other:=True, OtherChar:="|"
End With
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.Copy
ActiveWorkbook.SaveAs xlsFldrPath & "\" & Replace(CurrentFile, ".txt", ".xlsx"), xlOpenXMLWorkbook
ActiveWorkbook.Close False
ActiveSheet.UsedRange.ClearContents
CurrentFile = Dir
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
在这一行
Const txtFldrPath As String = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"
您已将 txtFldrPath 声明为常量,因此不能对其使用变量 varCellvalue
将 varCellvalue
声明为变量 Dim txtFldrPath As String
然后根据需要分配值
Dim txtFldrPath As String
txtFldrPath = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & varCellvalue & ".xls"