我有一个Excel VBA代码,该代码可查看文件夹并将数据复制到当前工作簿。再次运行代码时,我将在哪里放置要移动到下一个可用空白行的代码行?
我假设我将使用下面的代码行,但是在“从单元格中选择值”部分中的位置看来,它似乎不起作用:
twb.Worksheets("Log").Range("A65536").End(xlUp).Offset(1, 0).Select
我的完整代码是:
Sub RenameExcelInDir()
Dim MyPath As String
Dim MyFile As String
Dim MyExt As String
Dim MyNewName As String
Dim MyVendor As String
Dim MyFAC As String
Dim MyFabric As String
Dim MyFiberC As String
Dim MyKnit As String
Dim MyWoven As String
Dim MyDesc As String
Dim wb As Workbook
Dim twb As Workbook
Dim wks As Worksheet
Dim r As Range
Dim getDate As String
'Opens File Dialog Window to choose dir to search in
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
MyPath = .SelectedItems(1)
End With
getDate = Date
Set twb = ThisWorkbook
Set r = twb.Worksheets("Log").Range("A2")
MyFile = Dir(MyPath & "\*.*")
Do While Len(MyFile) > 0
MyExt = Split(MyFile, ".")(UBound(Split(MyFile, ".")))
Set wb = Workbooks.Open(MyPath & "\" & MyFile, UpdateLinks:=0)
'Loops through the worksheet collection
For Each wks In wb.Worksheets
Select Case wks.Name
Case "ISU Form"
MyNewName = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C23").Value & "." & MyExt)
MyVendor = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C21").Value)
MyFAC = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C25").Value)
MyDesc = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C14").Value)
MyFabric = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C15").Value)
MyFiberC = ValidFileName(FileName:=wb.Sheets("ISU FORM").Range("C17").Value)
Case "GLOBAL ISU"
MyNewName = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("M26").Value & "." & MyExt)
MyVendor = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D18").Value)
MyFAC = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D21").Value)
MyDesc = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D26").Value)
MyFabric = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D64").Value)
MyFiberC = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D66").Value)
MyKnit = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("D68").Value)
MyWoven = ValidFileName(FileName:=wb.Sheets("GLOBAL ISU").Range("F68").Value)
End Select
Next wks
wb.Close
'Select Values from cells
twb.Worksheets("Log").Range("A65536").End(xlUp).Offset(1, 0).Select
r.Value = MyFile
r.Offset(, 1).Value = MyNewName
r.Offset(, 2).Value = getDate
r.Offset(, 3).Value = MyVendor
r.Offset(, 4).Value = MyFAC
r.Offset(, 5).Value = MyDesc
r.Offset(, 6).Value = MyFabric
r.Offset(, 7).Value = MyFiberC
r.Offset(, 8).Value = MyKnit
r.Offset(, 9).Value = MyWoven
r.Offset(, 10).Value = MyPath
Set r = r.Offset(1)
Name MyPath & "\" & MyFile As MyPath & "\" & MyNewName
MyFile = Dir
Loop
End Sub
,而ValidFileNameFunction是:
Function ValidFileName(ByVal FileName As String) As String
Dim myarray() As Variant
Dim x
'check for illegal characters
myarray = Array("[", "]", "\\", "/", "*", "\", "?", "<>", "<", ">", ":", "|", "&")
For x = LBound(myarray) To UBound(myarray)
FileName = Replace(FileName, myarray(x), "", 1)
Next x
ValidFileName = FileName
End Function
当代码再次运行时,我希望它移至工作表“日志”的下一行。我当前的测试结果只是覆盖了测试文件夹中的前两行。任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
谢谢大家的帮助。我能够使用以下代码成功完成此任务:
Dim nextrow As Long
nextrow = Cells(Rows.Count, "A").End(xlUp).row
我必须在现有代码中更改一两行才能使其运行,但是它可以工作! :)谢谢大家提供的指向我的文章的链接。干杯!