【发布时间】:2017-06-20 23:43:11
【问题描述】:
我已经启动了一个简单的宏来清理报告。我需要添加一个 vlookup,并且很难弄清楚该往哪个方向发展,是否应该使用 match 或 vlookup 表达式,以及如何解决动态且每周都在变化的文件名。
我更像是一个 SharePoint 工作流编写者,并且倾向于需要一个变量来存储(第二个打开的工作表的)文件名,以便稍后在 vlookup 中回忆,但我不确定这是否是正确的想法,或者确切的执行方式。由于我是初学者,您会看到我一行一行地写了这篇文章,其中包含许多 cmets,诗句必然是浓缩的步骤。
目标:
获取已经被用户打开的审计报告,运行宏清理文件,选择以前版本的文件(在对话框中由用户选择),并查看列,以原始格式文档返回结果,粘贴值vlookup,清理所有#n/a 值,查找后关闭源文件,保存目标文件。 Vlookup 信息位于代码中的 cmets 中应该写入的位置。
Sub AuditRptCleanup()
'Verify Correct File Is Open
If ActiveWorkbook.Name Like "*Audit*" Or ActiveWorkbook.Name Like "*AuditReport*" Or
ActiveWorkbook.Name Like "*audit*" Or ActiveWorkbook.Name Like "*auditreport*" Then
On Error GoTo ErrorFileIncompatiable:
'Verify Macro has not ran on Workbook Previously
If Cells(1, 1).Value = "Product Number" And Cells(1, 2).Value = "Prod Type" Then
MsgBox "Macro has already been used on this workbook"
Exit Sub
End If
'Select Starting Cell
Range("A1").Select
'Unmerge all Cells in Worksheet
ActiveSheet.Cells.UnMerge
'Delete Columns A1 thru D1
Range("$A$1:$D$1").EntireColumn.Delete
'Delete Rows A1 thru A9
Range("$A$1:$A$9").EntireRow.Delete
'Cut and Paste Cells
Range("$A$2").Cut Range("$A$1")
Range("$G$1").Cut Range("$F$1")
Range("$P$1").Cut Range("$O$1")
Range("$AA$1").Cut Range("$Z$1")
'Sort by Column A to Remove Extra Rows from View
Columns("$A:$AM").Sort key1:=Range("$A:$A"), order1:=xlAscending, Header:=xlYes
'Auto Fit Contents in Columns and Rows
ActiveCell.Columns("$A:$AG").EntireColumn.Select
ActiveCell.Columns("$A:$AG").EntireColumn.AutoFit
ActiveSheet.Rows.EntireRow.AutoFit
'Delete Empty Columns
Range("$B:$B, $D:$D, $G:$I, $K:$L, $N:$N, $P:$Q, $T:$V, $X:$Y, $AA:$AB, $AD:$AF").EntireColumn.Delete
'Remove Wrap Text from Cell B1
Range("$B$1").WrapText = False
'Autofit Contents of Columns
Range("$A$1:$AF$1").Columns.AutoFit
'Autofit Row A2 Contents
Range("$A$2:$A$2").Rows.AutoFit
'Delete Columns B and C
Range("$B:$C").EntireColumn.Delete
'Remove Wrap Text on L1 and M1
Range("$L$1:$M$1").WrapText = False
'Label Cell L1
Range("$L$1").Value = "Qty from Previous Report"
'Label Cell M1
Range("$M$1").Value = "Change in Qty"
'Label Cell N1
Range("$N$1").Value = "Date New"
'Label Cell O1
Range("$O$1").Value = "Comments"
'Label Cell P1
Range("$P$1").Value = "Action"
'Label Cell Q1
Range("$Q$1").Value = "Status"
'Label Cell R1
Range("$R$1").Value = "Production Storage Quantity"
'Autofit Contents of Columns
Range("$L$1:$R$1").Columns.AutoFit
'Open Previous Day Source file from User Selection
Dim val As String
Dim intChoice As Integer
Dim strPath1 As String
'Alert User to open file
MsgBox ("Please browse to the previous day Audit file you wish to use for the VlookUp")
'Open File Dialog Box
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Open File Dialog Box and prompt User to select single file
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'Determine what file User selected
If intChoice <> 0 Then
'Get File Path selected by User
strPath1 = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
'VLookup Column L thru R and Paste to Target File
‘This is where I need to take the file opened at the very beginning, and look up value A2, from the new
‘file the user just opened, from Sheet 1! A:L, col index 12, FALSE, and paste into column L2 in the file
‘opened from the beginning, to the last row that has data (last row containing data changes each
‘time).
‘I need to then do the same for M-R, changing the col index to 13 for M, 14 for O, so forth and so on,
‘and end each look up to the last row with data in the column.
‘I need to be able to IFERROR,0 in my steps to avoid #n/a
‘I then to need to copy and paste the values I just entered from row 2 – L:R, to last Row of data, and
‘paste value.
'Save File As
Application.GetSaveAsFilename
End If
'Notify User of Incompatible File
ErrorFileIncompatiable:
MsgBox "This is not an Audit Report"
Exit Sub
End If
End Sub
【问题讨论】:
-
您不需要
.Select。对于第一部分,您可以使用Option Compare Text(不区分大小写)和If ActiveWorkbook.Name Like "*Audit*"然后. Why do you need a vlookup? You just seem to copy and paste values into another workbook. For Lastrow use sht.Cells(sht.Rows.Count, "A").End(xlUp).Row(注意:必须将 sht 声明为您要使用的工作表) -
复制和粘贴步骤是独立的,发生在需要进行 vlookup 的第二张工作表打开之前。完成复制和粘贴格式化步骤后,我需要调用打开另一个文件,并完成 vlookup。我遇到的问题是知道哪个工作表处于活动状态并且能够分别调用源文件和目标文件,这就是为什么我认为我可能需要一个变量作为文件名的更改并且它从来没有一周又一周地被命名.
-
如果您在屏幕截图之前和之后添加带有垃圾数据的截图会很好。
-
我想我不明白。前后拍什么?上面所有步骤都是正确的,我只需要你在我的代码的注释部分看到的地方,了解,从文件对话框调用打开文件后,如何引用它(因为名称不会是静态的,并且在 Vlookup 中每周更改)。在运行宏之前,此宏在用户将打开的一个文件上运行。我也没有编程,同样,文件名每周都在变化。
-
他们从打开的审计报告开始。运行宏。宏清除从另一个报告系统(不是基于 excel 或其他我可以访问的)导出的文件。宏执行您看到的复制、粘贴和删除步骤,以从原始版本格式化文件,因为它是一团糟。它清理格式并为 vlookup 创建占位符列。然后我让用户打开他们选择的第二个文件(源文件),用于需要执行的 vlookup,最终结果放在原始打开的文件中。如果我不知道名称,如何从用户选择的文件中启动 vlookup?
标签: vba excel excel-formula excel-2010