简体   繁体   English

VBA 向下填充现有值直到最后一行

[英]VBA to fill an existing value down until last row

I already have a working macro that is transposing 5 rows into columns and append them after the last column.我已经有一个工作宏,它将 5 行转换为列,append 在最后一列之后。

The problem is that I need a long format so basically I want to automate the filling up, with the same data, until the end of data (basically the last row).问题是我需要一个长格式,所以基本上我想用相同的数据自动填充,直到数据结束(基本上是最后一行)。 I have 600 files and all these files have a different number of rows.我有 600 个文件,所有这些文件都有不同的行数。

Sub LoopFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'your code here
                Range("A2:B6").Select
                Selection.Copy
                Range("I10").Select
                Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                    False, Transpose:=True
                Rows("1:8").Select
                Application.CutCopyMode = False
                Selection.Delete Shift:=xlUp
                With Sheets("Calculated Saccades")
                    .Range("I3").AutoFill .Range("I4:I" & .Cells(.Rows.Count, "A").End(xlUp).Row)
                End With
                ActiveWorkbook.Save
                ActiveWorkbook.Close
            End With
            xFileName = Dir
        Loop
    End If
End Sub

The With Sheets is returning an error. With Sheets返回错误。 Any ideas?有任何想法吗? Thank you so very much!非常感谢你!

You're already inside a With...End With block ( With Workbooks.Open(xFdItem & xFileName) - this loops through workbooks if you select more than one in the File Open dialog box);您已经在With...End With块中( With Workbooks.Open(xFdItem & xFileName) - 如果您在文件打开对话框中 select 不止一个,则会循环工作簿); you can't nest these.你不能嵌套这些。

You can simply replace您可以简单地替换

    With Sheets("Calculated Saccades")
  .Range("I3").AutoFill .Range("I4:I" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With

with:和:

    Sheets("Calculated Saccades").Range("I3").AutoFill
    Sheets("Calculated Saccades").Range("I4:I" & .Cells(.Rows.Count, "A").End(xlUp).Row)

You should also read How to avoid using Select in Excel VBA .您还应该阅读如何避免在 Excel VBA 中使用 Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM