我正在尝试创建一个脚本,以帮助将多个Google Analytics CSV报告导入一个主工作簿。每周生成的不同网站有超过70个报告,此时脚本设置为遍历所选文件夹中的所有CSV文件,并在删除文件夹中的CSV之前对所有这些文件执行导入将其清除以供下周进口。我遇到的问题是每个CSV不一定包含相同的数据,也不一定包含相同的行,因此我无法使用精确的单元格引用来复制值。例如:
这些是可用于导出的所有类型的流量,加上一个"总计"在第一列中从未有过任何内容的行
与第二份报告相比,该报告仅包含此特定报告日期的自然流量数据,因为其他流量类型不存在数据
因此,因为每个报告可能包含任意数量的流量类型,所以"总计"行每次都会以不同的行结束,我不能轻易地弄清楚如何将Excel指向该行,因为行中的第一个单元格始终为空白。这也是第二次在A列中出现空白单元格(上面有一个空白单元格用于分隔数据表格)。
我该如何处理?我是否尝试找到一种方法来定位总计行,或者是否有办法让excel总计有机,付费,直接,推荐和显示流量(如果它可用)我需要的每个变量导入并使用该数据?
到目前为止,这是我的代码,带有占位符:
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder That Contains Your Files"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.csv*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
wb.Activate
'Ensure Workbook has opened and is active before moving on to next line of code
DoEvents
Dim rng1 As Range
Dim strSearch As String
Dim sheet_name_result As String
strSearch = ActiveWorkbook.Sheets(1).Range("A2")
Set rng1 = Workbooks("Google Analytics trends.xlsm").Worksheets("LISTS").Range("B:B").Find(strSearch, , xlValues, xlPart)
sheet_name_result = rng1.Offset(0, -1).Value
If Not rng1 Is Nothing Then
MsgBox strSearch & " was found" & vbNewLine & "corresponding table is " & rng1.Offset(0, 1) & " on sheet " & sheet_name_result
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set table_list_object = Workbooks("Google Analytics trends.xlsm").Worksheets(sheet_name_result).ListObjects(1)
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = "start date placeholder"
table_object_row.Range(1, 2).Value = "end date placeholder"
table_object_row.Range(1, 3).Value = "pageviews placeholder"
table_object_row.Range(1, 4).Value = "visitors placeholder"
table_object_row.Range(1, 5).Value = "unique visitors placeholder"
table_object_row.Range(1, 6).Value = "pages/visit placeholder"
table_object_row.Range(1, 7).Value = "organic traffic placeholder"
table_object_row.Range(1, 8).Value = "referral traffic placeholder"
Else
MsgBox strSearch & "was not found"
End If
'Save and Close Workbook
wb.Close SaveChanges:=False
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Deletes all CSV files from the targeted folder
On Error Resume Next
Kill (myPath & "*.csv*")
On Error GoTo 0
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub