VBA 将列与特定的 header 相加并捕获到主文件中
VBA to sum column with particular header and capture in a master file
我有以下代码,当指向特定文件夹时,将在我的检查选项卡中捕获以下数据:文件名、行数、列数。我需要帮助的最后一部分是找到一个 header,说出它的“值”,并对列求和,将总数发布到从单元格 d8 开始的每个文件名的相邻位置。下面的代码。有什么想法可以轻松做到这一点吗?
Sub CollectData()
Dim fso As Object, xlFile As Object
Dim sFolder$
Dim r&, j&, k&
'*
Sheets("Check").Activate
Range("F8:I50").ClearContents
Range("A8:D50").Copy Range("F8")
Range("A8:D50").ClearContents
'*
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.path
If .Show Then sFolder = .SelectedItems(1) Else Exit Sub
End With
Set fso = CreateObject("Scripting.FileSystemObject")
For Each xlFile In fso.GetFolder(sFolder).Files
With Workbooks.Open(xlFile.path, Password:="password")
With .Sheets(1)
j = .Cells(.Rows.Count, 1).End(xlUp).Row
k = .Cells(1, Sheet1.Columns.Count).End(xlToLeft).Column
End With
.Close False
End With
r = r + 1
Cells(r + 7, 1).Value = xlFile.Name
Cells(r + 7, 2).Value = j
Cells(r + 7, 3).Value = k
ActiveWorkbook.Save
Next
End Sub
我将遍历 header 个单元格并检查 cell.value:
Dim headers As Range
Dim c As Range
Dim SumRange As Range
Dim Sum As Double
Set headers = Range("F8:I8")
For Each c In headers
If c.Value = "value" Then
'From the header, go 1 cell down and get range of continous non blank cells
'Set the SumRange variable to this range of cells
Set SumRange = Range(c.Offset(1, 0), c.End(xlDown))
End If
Next
'Iterate over the SumRange cells, and add to Sum variable as you go
For Each c In SumRange
Sum = Sum + c.Value
Next
'Display Sum in destination cell
Cells(r + 7, 4).Value = Sum
干杯!
我有以下代码,当指向特定文件夹时,将在我的检查选项卡中捕获以下数据:文件名、行数、列数。我需要帮助的最后一部分是找到一个 header,说出它的“值”,并对列求和,将总数发布到从单元格 d8 开始的每个文件名的相邻位置。下面的代码。有什么想法可以轻松做到这一点吗?
Sub CollectData()
Dim fso As Object, xlFile As Object
Dim sFolder$
Dim r&, j&, k&
'*
Sheets("Check").Activate
Range("F8:I50").ClearContents
Range("A8:D50").Copy Range("F8")
Range("A8:D50").ClearContents
'*
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.path
If .Show Then sFolder = .SelectedItems(1) Else Exit Sub
End With
Set fso = CreateObject("Scripting.FileSystemObject")
For Each xlFile In fso.GetFolder(sFolder).Files
With Workbooks.Open(xlFile.path, Password:="password")
With .Sheets(1)
j = .Cells(.Rows.Count, 1).End(xlUp).Row
k = .Cells(1, Sheet1.Columns.Count).End(xlToLeft).Column
End With
.Close False
End With
r = r + 1
Cells(r + 7, 1).Value = xlFile.Name
Cells(r + 7, 2).Value = j
Cells(r + 7, 3).Value = k
ActiveWorkbook.Save
Next
End Sub
我将遍历 header 个单元格并检查 cell.value:
Dim headers As Range
Dim c As Range
Dim SumRange As Range
Dim Sum As Double
Set headers = Range("F8:I8")
For Each c In headers
If c.Value = "value" Then
'From the header, go 1 cell down and get range of continous non blank cells
'Set the SumRange variable to this range of cells
Set SumRange = Range(c.Offset(1, 0), c.End(xlDown))
End If
Next
'Iterate over the SumRange cells, and add to Sum variable as you go
For Each c In SumRange
Sum = Sum + c.Value
Next
'Display Sum in destination cell
Cells(r + 7, 4).Value = Sum
干杯!