我正在扩展我的宏构建工作,以组织和转移从大型机检索到的数据。数据为字符串形式,与此处讨论的类似。我还利用这些问题(1)(2)的 SO 建议和帮助开发的宏。
由于我在开发中遇到的困难——也许是因为缺乏经验,我花了很多时间来开发宏的这个特定部分,以至于同时处理其他部分。
简而言之,我正在生成工作表,重命名它们并将数据推送到这些工作表中,然后生成这些工作表,然后填写一个空白表格。我试图根据行来做,因为每一行本质上都是我推送到工作表表单的记录。我正在使用 20 多个字段并将其推送到每个新工作表。
我最初尝试了一个高度嵌套的循环,然后考虑了如何使用结构。然而,随着我越来越困惑,我切换到离散模型,因为我注意到我仍然没有弄清楚如何正确使用 Range 对象的单元格(单元格地址属性)。
代码如下:
'This subroutine is intended to take filtered data and use it to fill forms.
'These forms use a very basic text template worksheet, which is copied over for each worksheet.
'In general, these forms will number from 1 to 100, for discussion purposes.
'The idea is that each row of data in the DataSheet will be used to fill each worksheet tab.
Sub DataShifter()
Dim RngOne As Range, RngCell As Range
Dim RngTwo As Range
Dim RngThree As Range, RngCell2 As Range 'RngCell2 is not currently in use
Dim RngRow As Range
Dim LastCell As Long
Dim arrList() As String, LongCount As Long
'Define range data within the Crtieria Sheet
With Sheets("Criteria")
LastCell = .Range("A" & Sheets("Criteria").Rows.Count).End(xlUp).row
Set RngOne = .Range("A2:A" & LastCell)
End With
'Push values into the array
LongCount = 0
For Each RngCell In RngOne
ReDim Preserve arrList(LongCount)
arrList(LongCount) = RngCell.Text
LongCount = LongCount + 1
Next
'Filter the values to the desired criteria stored in the array.
With Sheets("Sheet1")
'For when this process is repeated.
If .FilterMode Then .ShowAllData
.Range("A:A").AutoFilter Field:=1, Criteria1:=arrList, Operator:=xlFilterValues
End With
'Add a Sheet to contain the filtered criteria
Sheets.Add After:=Sheets(1)
Sheets(2).Name = "DataSheet"
'With the original dataset, snag all existing data based on the range in Sheet Criteria.
'This avoids potential empty junk data and potential blanks pulled from the mainframe.
With Sheets("Sheet1")
LastCell = .Range("A" & Sheets("Criteria").Rows.Count).End(xlUp).row
Set RngTwo = .Range("A2:AA" & LastCell)
End With
'Push data into DataSheet worksheet, so data is sequential
Sheets(1).Select
RngTwo.Copy
Sheets("DataSheet").Select
ActiveSheet.Paste
'Define the ranges used within the sheet
With Sheets("DataSheet")
LastCell = .Range("A" & Sheets("Criteria").Rows.Count).End(xlUp).row
Set RngThree = .Range("A2:A" & LastCell)
End With
'For each row in the range, (1) generate a new datasheet, and copy the form from the template to the new sheet.
'(2) Rename the datasheet to be the value in Row 1, Column 1 ("A1").
'(3) Copy over information to the form based on column location in the Datasheet.
'This method, even if made functional, is both procedural and limited in scope. Recursion with text matching will be the end goal for this form.
For Each RngRow In RngThree.Rows
Sheets.Add After:=Sheets(1)
'Grab the text form from the Template and push it into the new sheet.
Sheets("TemplateSheet2").Select
Cells.Select
Selection.Copy
Sheets(2).Select
ActiveSheet.Paste
Sheets(2).Name = Sheets("DataSheet").Cells(RngRow, 1).Value
Sheets(2).Range("B3") = Sheets("DataSheet").Cells(RngRow, 1).Value
Sheets(2).Range("D3") = Sheets("DataSheet").Cells(RngRow, 2).Value
Sheets(2).Range("F3") = Sheets("DataSheet").Cells(RngRow, 3).Value
Sheets(2).Range("B5") = Sheets("DataSheet").Cells(RngRow, 4).Value
Sheets(2).Range("B10") = Sheets("DataSheet").Cells(RngRow, 5).Value
Sheets(2).Range("B7") = Sheets("DataSheet").Cells(RngRow, 6).Value
Sheets(2).Range("D10") = Sheets("DataSheet").Cells(RngRow, 7).Value
Sheets(2).Range("F10") = Sheets("DataSheet").Cells(RngRow, 8).Value
Sheets(2).Range("B13") = Sheets("DataSheet").Cells(RngRow, 9).Value
Sheets(2).Range("D13") = Sheets("DataSheet").Cells(RngRow, 10).Value
Sheets(2).Range("F13") = Sheets("DataSheet").Cells(RngRow, 11).Value
Sheets(2).Range("B16") = Sheets("DataSheet").Cells(RngRow, 12).Value
Sheets(2).Range("D16") = Sheets("DataSheet").Cells(RngRow, 13).Value
Sheets(2).Range("F16") = Sheets("DataSheet").Cells(RngRow, 14).Value
Sheets(2).Range("B19") = Sheets("DataSheet").Cells(RngRow, 15).Value
Sheets(2).Range("D19") = Sheets("DataSheet").Cells(RngRow, 16).Value
Sheets(2).Range("F19") = Sheets("DataSheet").Cells(RngRow, 17).Value
Sheets(2).Range("B21") = Sheets("DataSheet").Cells(RngRow, 18).Value
Sheets(2).Range("D21") = Sheets("DataSheet").Cells(RngRow, 19).Value
Sheets(2).Range("B23") = Sheets("DataSheet").Cells(RngRow, 20).Value
Sheets(2).Range("D23") = Sheets("DataSheet").Cells(RngRow, 21).Value
'Concatenate values from certain fields into one field
Sheets(2).Range("B26") = Sheets("DataSheet").Cells(RngRow, 23).Value & Cells(RngRow, 24).Value & Cells(RngRow, 24).Value & Cells(RngRow, 25).Value & Cells(RngRow, 26).Value & Cells(RngRow, 27).Value
Next RngRow
End Sub
目前,执行此代码会导致类型不匹配,首先在第 84 行:Sheets(2).Name = Sheets("DataSheet").Cells(RngRow, 1).Value
,如果第 84 行被注释掉,则在后续行中。我不确定我应该如何纠正这个问题以使我的代码有效地发挥作用,我要求修复这个特定问题。
更普遍关注的是我的方法,我也欢迎任何建议、建议、方法或改进来考虑这个宏 - 尽管在我进行任何优化工作之前修复是最重要的。