[英]How to move to next available row - EXCEL VBA
I have a Excel VBA code that looks through a folder and copies data to the current workbook. 我有一个Excel VBA代码,可以查看文件夹并将数据复制到当前工作簿。 Where would I place a code line for move to next available blank row when the code is run again?
当代码再次运行时,我会在哪里放置代码行以移动到下一个可用的空白行?
I assume I would use the following line of code but it doesn't seem to work in the place where I have it located in the "Select Values from Cells" section: 我假设我会使用以下代码行,但它似乎不适用于我在“从单元格中选择值”部分中所在的位置:
twb.Worksheets("Log").Range("A65536").End(xlUp).Offset(1, 0).Select
My full code is: 我的完整代码是:
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
and the ValidFileNameFunction is: 和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
I would like it to move to the next line in the worksheet "Log" when the code is ran again. 当我再次运行代码时,我希望它移动到工作表“Log”中的下一行。 My current testing results is just overwriting the first two lines in the test folder.
我目前的测试结果只是覆盖了测试文件夹中的前两行。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks!
谢谢!
Thank you all for your help. 谢谢大家的帮助。 I was able to successfully able to accomplish this using the following bit of code:
我能够使用以下代码成功地完成此任务:
Dim nextrow As Long
nextrow = Cells(Rows.Count, "A").End(xlUp).row
I had to change a line or two in the existing code to get it to run, but it works! 我不得不在现有代码中更改一行或两行以使其运行,但它可以工作! :) Thank you all for your links to articles that explained what I needed.
:)谢谢大家的链接到解释我需要的文章。 Cheers!
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.