将数据从数据库推送到生成的模板工作表中 [英] Pushing Data from Datasheet Into Generated Template Worksheets

查看:166
本文介绍了将数据从数据库推送到生成的模板工作表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展我的宏观建设工作,以组织和转移从大型机检索的数据。数据是字符串形式,类似于所讨论的这里。我也利用由SO提供的开发宏,并从这些问题中获得帮助( 1 )( 2 )。



由于我在开发过程中遇到的困难,我花了很多时间开发宏观的这个特定部分,同时处理其他部分,也许是由于缺乏经验。



简而言之,我正在生成工作表,重命名它们并将数据推送到那些生成的工作表中,然后填充一个空白表单。我试图在行的基础上做,因为每一行本质上是一个我正在推送到工作表格的记录。我正在利用和推动每个新工作表的20个领域。



我最初尝试一个高度嵌套的循环,然后考虑如何使用结构。然而,随着我越来越困惑,我转向一个离散的模型,正如我所指出的,我仍然没有想到如何正确使用Range对象的单元格(单元格地址属性)。



代码如下:

 '此子例程旨在将过滤后的数据用于填写表格。 
'这些表单使用非常基本的文本模板工作表,这是每个工作表复制的。
'一般来说,这些表格的编号从1到100,供讨论。
'的想法是DataSheet中的每一行数据将用于填充每个工作表选项卡。

Sub DataShifter()


Dim RngOne As Range,RngCell As Range
Dim RngTwo As Range
Dim RngThree As Range,RngCell2因为Range'RngCell2目前没有使用
Dim RngRow As Range

Dim LastCell As Long

Dim arrList()As String,LongCount As Long

'定义Crtieria Sheet中的范围数据
带表格(Criteria)
LastCell = .Range(A& Sheets(Criteria)。Rows.Count)。结束(xlUp).row
设置RngOne = .Range(A2:A& LastCell)
结束

'将值推入数组
LongCount = 0
每个RngCell在RngOne
ReDim保留arrList(LongCount)
arrList(LongCount)= RngCell.Text
LongCount = LongCount + 1
下一个


'将值过滤到存储在数组中的所需条件。
带表格(Sheet1)

'对于何时重复此过程。
IfFilterMode Then .ShowAllData

.Range(A:A)。AutoFilter Field:= 1,Criteria1:= arrList,Operator:= xlFilterValues

结束

'添加工作表以包含过滤的条件
Sheets.Add After:= Sheets(1)
表(2).Name =DataSheet

'使用原始数据集,根据表格条件范围内的所有现有数据。
'这样可以避免潜在的空垃圾数据和潜在的空白从主机拉出来。
带表格(Sheet1)

LastCell = .Range(A& Sheets(Criteria)。Rows.Count).End(xlUp).row
设置RngTwo = .Range(A2:AA& LastCell)

结束

'将数据推送到DataSheet工作表中,因此数据是顺序的
表(1)。选择
RngTwo.Copy
表格(DataSheet)。选择
ActiveSheet.Paste

'定义工作表内使用的范围
With Sheets(DataSheet)
LastCell = .Range(A& Sheets(Criteria)。Rows.Count).End(xlUp).row
设置RngThree = .Range A2:A& LastCell)

结束

'对于范围中的每一行,(1)生成一个新的数据表,并从模板中复制表单到新的表。
'(2)将数据表重命名为行1,列1(A1)中的值。
'(3)根据数据表中的列位置将信息复制到表单。
'这种方法即使功能齐全,也是程序性的,范围有限。带有文本匹配的递归将成为此表单的最终目标。
每个RngRow在RngThree.Rows

Sheets.Add After:= Sheets(1)

'从模板中抓取文本格式并将其推入新表。
Sheets(TemplateSheet2)。选择
Cells.Select
Selection.Copy
表格(2)。选择
ActiveSheet.Paste

Sheets(2).Name = Sheets(DataSheet)。Cells(RngRow,1).Value

Sheets(2).Range(B3)= Sheets(DataSheet)。单元格(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)。值
表格(2).Range(B10)=表格(DataSheet)。单元格(RngRow,5).Value
表格(2).Range(B7)= 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)。 RngRow,9).Value
Sheets(2).Range(D13)= Sheets(DataSheet)。Cells(RngRow,10).Value
Sheets(2).Range )= Sheets(DataSheet)。Cells(RngRow,11).Value
表格(2).Range(B16)=表格(DataSheet)。单元格(RngRow,12).Value
表格(2).Range(D16)= .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
(2).Range(B23)= Sheets(DataSheet)。Cells(RngRow,20).Value
Sheets(2).Range(D23)= Sheets(DataSheet)。 (RngRow,21).Value

'将某些字段的值连接到一个字段
表(2).Range(B26)=表(DataSheet)。单元格(RngRow ,23).Value&细胞(RngRow,24).Value&细胞(RngRow,24).Value&细胞(RngRow,25).Value&细胞(RngRow,26).Value&细胞(RngRow,27).Value


下一个RngRow


End Sub

目前,执行此代码会导致类型不匹配,首先在第84行: Sheets(2).Name = Sheets(DataSheet) .Cells(RngRow,1).Value ,然后如果第84行注释掉,则在进程行上。我不清楚我应该如何纠正这个问题,使我的代码有效地运行,而且我要求解决这个问题。



更普遍的关注是我的做法,我也欢迎任何关于这个宏的建议,建议,方法或改进来考虑 - 尽管在进行任何优化工作之前,修复是重要的。

解决方案

在导致错误的行中,而不是 RngRow 使用 RngRow.Row / p>

RngRow 是一个范围 RngRow.Row 将返回 RngRow 的第一行的号码。



单元格期望一个 RowIndex (一个数字)和一个 ColumnIndex 。当您提供范围(而不是数字)和ColumnIndex时,会引发您指出的类型匹配错误。



以下是一个示例,您可以缩短/改善你的代码,而不是:

  Sheets.Add After:= Sheets(1)
'模板并将其推入新的工作表。
Sheets(TemplateSheet2)。选择
Cells.Select
Selection.Copy
表格(2)。选择
ActiveSheet.Paste

您应该可以使用相同的结果,尽管我会尽量避免 select 尽可能多:

 '将模板复制到新工作表中。 
表格(TemplateSheet2)。复制后:=表格(1)
表格(2).select


I am extending my macro building efforts to organize and shift data retrieved from a mainframe. The data is in string form, similar to that discussed here. I am also making use of the macros developed with SO advice and aid from these questions(1)(2).

I have spent many hours developing this particular portion of the macro, to the point of working on other parts concurrently due to the difficulties I have encountered in development - perhaps due to inexperience.

To put it succinctly, I am generating worksheets, renaming them and pushing data into those worksheets, which are generated and then filled with a blank form. I am attempting to do it on the basis of rows, as each row is essentially a record that I am pushing to a worksheet form. There are 20-some fields I am utilizing and pushing over to each new worksheet.

I originally attempted a highly nested loop, and then considered how I might make use of structures. However, as I grew more confused, I switched to a discrete model, as I noted I still had not figured out how to make proper use of the Cells (cell address property) of the Range Object.

The code is as follows:

'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

Currently, executing this code results in a type mismatch, first on line 84: Sheets(2).Name = Sheets("DataSheet").Cells(RngRow, 1).Value, and then on the proceeding lines if line 84 is commented out. I am uncertain how I should correct this to make my code function effectively, and I am asking for a fix to this particular issue.

Of more general concern is my approach, and I would please also welcome any suggestions, advice, approaches or improvements to consider for this macro - although the fix is of paramont importance before I make any optimization efforts whatsoever.

解决方案

In the lines causing errors instead of RngRow Use RngRow.Row.

RngRow is a Range. RngRow.Row will return the number for the first row of the RngRow.

Cells expects a RowIndex(a number) and a ColumnIndex. When you provide a Range (Instead of a number) and a ColumnIndex it throws the type match error you've indicated.

Here is one example of how you could shorten/improve your code instead of:

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

You should be able to use this with the same results though I would try to avoid select as much as possible:

'Copy the Template into a new sheet.
Sheets("TemplateSheet2").Copy After:=Sheets(1)
Sheets(2).select

这篇关于将数据从数据库推送到生成的模板工作表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆