gpt4 book ai didi

Excel VBA 从另一个名为范围的工作簿中提取非空行

转载 作者:行者123 更新时间:2023-12-04 20:17:45 24 4
gpt4 key购买 nike

VBA 技能较弱的网站新手。希望我能在几天来一直在努力解决的问题上找到一些帮助。我发现了许多相似的例子,似乎无法将它们结合在一起。我正在使用 Excel 2007。我有一个“Summary_Reports”WB,以及由员工命名的其他几个工作簿(例如“Jim.xls”、“bob.xls”等)。每个员工工作簿都有一个命名范围“caps”,来自工作表“Tasks”。每个员工 wb 中的这个命名范围的宽度(列数)相同,但高度(行数)可能不同,并且某些行可能为空。尝试在“Summary_Reports”wb 中设置一个宏,该宏将打开每个员工 wb,复制命名范围“caps”,并将第一列中包含数据的该范围的行插入/粘贴到“报告”表在“Summary_Reports”wb 中。我认为最简单的粘贴方法就是在顶部选择一个单元格并始终将这些行插入那里,这样每个员工就会从同一个位置开始插入到前一个员工的上方。这样就不会计算或寻找工作表上最后填充的行。起初我尝试打开“Jim.xls”并直接从工作簿中复制命名范围,但收效甚微,语法也遇到了很多麻烦。所以我最终得到了下面的代码,它将员工表拉到“Summery_Reports”中,然后从自身而不是另一个 wb 复制命名范围。最后可能会删除这些工作表。

我在下面开始的有点工作,但我知道的数据验证是不正确的。如果我错了,请纠正我,但它只是检查“大写”的左上角单元格;如果有内容,则粘贴所有“大写字母”,如果该单个单元格为空,则不粘贴任何内容。如何更正验证以检查每一行的第一列,以及如何让它只给我带有数据的行?

另外,我知道有一种更好的方法可以直接从每个员工 wb 获取“caps”数据,而无需先导入工作表。如果这可以轻松完成,我会对这方面的任何建议非常感兴趣。

如果您愿意帮助我,请尽可能地简化它,因为我真的很想知道代码的作用,而不仅仅是复制和粘贴。先感谢您。

Sub Import_Sheets()
Application.Workbooks.Open ("jim.xls")
Workbooks("jim.xls").Activate
Sheets("Tasks").Copy After:=Workbooks("Summary_Report.xlsm").Sheets("Report")
Application.Workbooks("Jim.xls").Close

'Go to newly copied sheet and name it.
ActiveSheet.Name = "jim"

'Copy the "caps" named range.
With Range("Caps")
If .Cells(1, 1).Value = "" Then
Else
Range("Caps").Select
Selection.Copy
Sheets("Report").Select
Range("B2").Select
Selection.Insert Shift:=xlDown
End If
End With
End Sub

最佳答案

注释代码:

Sub Import_Sheets()

'Declare variables
Dim wsDest As Worksheet 'This is the sheet that data will be pasted to
Dim rngCaps As Range 'This is used to determine if there is a named range "Caps"
Dim rngFound As Range 'This is used to loop through the first column in the named range "Caps"
Dim rngSearch As Range 'This is used to determine where to search
Dim rngCopy As Range 'This is used to store the rows with data that will be copied
Dim strFirst As String 'This is used to store the first cell address to prevent an infinite loop
Dim i As Long 'This is used to loop through the selected workbooks

'Create an "Open File" dialogue for the user to choose which files to import
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear 'Clear existing filters (if any)
.Filters.Add "Excel Files", "*.xls*" 'Filter for Excel files
.AllowMultiSelect = True 'Allow user to select multiple files at a time with Shift or Ctrl

If .Show = False Then Exit Sub 'Pressed cancel, exit macro

'The destination is this workbook, sheet 'Report'
Set wsDest = ActiveWorkbook.Sheets("Report")

'Turn off screenupdating. This prevents "Screen Flickering" and allows the code to run faster
Application.ScreenUpdating = False

'Begin loop through selected files
For i = 1 To .SelectedItems.Count

'Open a selected file
With Workbooks.Open(.SelectedItems(i))

'Attempt to find a sheet named 'TimeEntry' with a named range "Caps"
On Error Resume Next
Set rngCaps = .Sheets("TimeEntry").Range("Caps")
On Error GoTo 0 'Remove the On Error Resume Next condition

'Was it able to set rngCaps successfully?
If Not rngCaps Is Nothing Then
'Yes, proceed to find rows with data
'Define rngSearch which will be used to find rows with data
Set rngSearch = Intersect(rngCaps, rngCaps.Cells(1).MergeArea.EntireColumn)

'Use a find loop to only get rows with data
'We can do this by utilizing the wildcard *
'The .Resize(, 1) will make sure we are only looking in the first column of rngCaps
Set rngFound = rngSearch.Find("*", rngSearch.Cells(rngSearch.Cells.Count), xlValues, xlWhole)

'Was there a cell found with data?
If Not rngFound Is Nothing Then
'Yes, record this first cell's address to prevent infinite loop
strFirst = rngFound.Address

'Also start storing the rows where data was found
Set rngCopy = rngFound

'Begin the find loop
Do
'Add found rows to the rngCopy variable
Set rngCopy = Union(rngCopy, rngFound)

'Advance loop to the next cell that contains data
Set rngFound = rngSearch.Find("*", rngFound, xlValues, xlWhole)

'Exit the loop when we are back to the first cell
Loop While rngFound.Address <> strFirst

'Copy the rows with data and paste them into the next available row in the destination worksheet
Intersect(rngCaps, rngCopy.EntireRow).Copy wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1)

'Clear rngFound and rngCopy to get ready for next workbook
Set rngFound = Nothing
Set rngCopy = Nothing
End If

'Clear rngCaps to get ready for next workbook
Set rngCaps = Nothing
End If

'Close this opened workbook and don't save changes
.Close False
End With

'Advance to the next workbook that was selected
Next i

'Re-enable screen updating
Application.ScreenUpdating = True

'Object variable cleanup
Set wsDest = Nothing

End With

End Sub

关于Excel VBA 从另一个名为范围的工作簿中提取非空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18388000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com