简体   繁体   English

VBA 填充工作表

[英]VBA to populate sheet

Source Data Sheet源数据表

在此处输入图像描述

Data To be populated sheet要填充的数据表

在此处输入图像描述

I have two sheets the source and the sheet where data need to be populated.我有两张表源和需要填充数据的表。

I want to fetch the numeric value from the source sheet under the corresponding column of the other sheet.我想从另一张表对应列下的源表中获取数值。

I tried this我试过这个

I tried with my code adding it but its going wrong somewhere can u please check.我尝试使用我的代码添加它,但它在某个地方出错了,你可以检查一下。 Considering my data is already formatted with, .考虑到我的数据已经用 .

Sub pop_codes() '
    Dim wsdata, wsPop As Worksheet
    Dim lngLoop1 As Long
    Dim lngLoop2 As Long
    Dim aData() As String
    Dim strData As String
    Dim DataLastRow As Integer
    Dim DataLastCol As Integer
    Set wsdata = Sheets("SourceData")
    Set wsPop = Sheets("TempData")
    DataLastRow = wsdata.Cells(wsdata.Rows.Count, "A").End(xlUp).Row
    DataLastCol = wsdata.Cells(1, wsdata.Columns.Count).End(xlToLeft).Column

    OutputRow = 2
    SearchArr = Array("AV", "CS", "P", "X", "FW", "H", "J", "L", "M", "N", "P", "PD", "PK", "R", "S", "T", "V", "W", "X", "BK", "CP", "FX", "HD", "IP", "IU")
    For OutputRow = 2 To DataLastRow
        For OutputCol = 2 To DataLastCol
           strData = wsdata.Cells(OutputRow, OutputCol)
           ' strData = Replace(strData, ")", ",")
           ' strData = Replace(strData, "(", ",")
           'strData = Replace(strData, " ", "")
            aData() = Split(strData, ",")
            For lngLoop1 = LBound(aData, 1) To UBound(aData, 1)
                For lngLoop2 = LBound(SearchArr) To UBound(SearchArr)
                    If InStr(aData(lngLoop1), SearchArr(lngLoop2)) > 0 Then
                        wsPop.Cells(OutputRow, 1) = wsdata.Cells(OutputRow, 1)
                        wsPop.Cells(OutputRow, 2) = wsdata.Cells(1, DataLastCol)
                        wsPop.Cells(OutputRow, 3) = SearchArr(lngLoop2)
                        wsPop.Cells(OutputRow, 4) = Replace(aData(lngLoop1), SearchArr(lngLoop2), "")
                        OutputRow = OutputRow + 1
                    End If
                Next lngLoop2
            Next lngLoop1
        Next OutputCol
    Next OutputRow
sExit:
    On Error Resume Next
    Set wbData = Nothing
    Set wsPop = Nothing
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "sDataSource", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

I'd use a "stepping" worksheet that I would populate with the split data from your first worksheet.我将使用一个“步进”工作表,我将使用您的第一个工作表中的拆分数据填充该工作表。 This could then be used as the basis for your final worksheet.然后可以将其用作最终工作表的基础。

Some VBA code to do this would be:执行此操作的一些 VBA 代码是:

Sub sDataSource()
    On Error GoTo E_Handle
    Dim wsIn As Worksheet
    Dim lngInLastRow As Long
    Dim lngInLastCol As Long
    Dim wsOut As Worksheet
    Dim strData As String
    Dim aData() As String
    Dim aSearch() As Variant
    Dim lngLoop1 As Long
    Dim lngLoop2 As Long
    Dim lngOutRow As Long
    Dim lngInRow As Long
    Dim lngInCol As Long
    Set wsIn = Worksheets("SourceData")
    lngInLastRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
    lngInLastCol = wsIn.Cells(1, wsIn.Columns.Count).End(xlToLeft).Column
    Set wsOut = Worksheets("TempData")
    lngOutRow = 2
    aSearch = Array("AV", "BK", "CP", "CS", "FW", "FX", "HD", "IP", "IU", "PD", "PK", "P", "H", "J", "L", "M", "N", "R", "S", "T", "V", "W", "X")
    For lngInRow = 2 To lngInLastRow
        For lngInCol = 2 To lngInLastCol
            strData = wsIn.Cells(lngInRow, lngInCol)
            strData = Replace(strData, ")", ",")
            strData = Replace(strData, "(", ",")
            strData = Replace(strData, " ", "")
            aData() = Split(strData, ",")
            For lngLoop1 = LBound(aData, 1) To UBound(aData, 1)
                For lngLoop2 = LBound(aSearch) To UBound(aSearch)
                    If InStr(aData(lngLoop1), aSearch(lngLoop2)) > 0 Then
                        wsOut.Cells(lngOutRow, 1) = wsIn.Cells(lngInRow, 1)
                        wsOut.Cells(lngOutRow, 2) = wsIn.Cells(1, lngInCol)
                        wsOut.Cells(lngOutRow, 3) = aSearch(lngLoop2)
                        wsOut.Cells(lngOutRow, 4) = Replace(aData(lngLoop1), aSearch(lngLoop2), "")
                        aData(lngLoop1) = ""
                        lngOutRow = lngOutRow + 1
                    End If
                Next lngLoop2
            Next lngLoop1
        Next lngInCol
    Next lngInRow
sExit:
    On Error Resume Next
    Set wsIn = Nothing
    Set wsOut = Nothing
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "sDataSource", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

In this code, I've looped the worksheet and got the value for each week/user.在这段代码中,我循环了工作表并获得了每周/用户的值。 I've replaced the brackets with commas, and removed any spaces.我用逗号替换了括号,并删除了所有空格。 This has then been split into an array, and I then walk this array, checking for each of the different values (ie CS, P, AV, X) that I am looking for.然后它被分割成一个数组,然后我遍历这个数组,检查我正在寻找的每个不同的值(即 CS、P、AV、X)。 If I find it, then output this element of the array, replacing the text part with an empty string).如果我找到了,那么output这个数组元素,用空字符串替换文本部分)。

Code has been modified to deal with the fact that some data names can cause duplication (ie "P" and "CP") when using InStr() , and I have dealt with this by putting the two character data names at the start of the array, and if there is a match, then setting the element of the data array to be a zero length string.代码已被修改以处理使用InStr()时某些数据名称可能导致重复(即“P”和“CP”)的事实,我通过将两个字符数据名称放在开头来解决这个问题数组,如果匹配,则将数据数组的元素设置为零长度字符串。

Regards,问候,

There's no easy solution for such an elaborated task.对于如此复杂的任务,没有简单的解决方案。

If I were you, I'll first split this into different pages: one page, containing the AV results, one with the CS results, ...如果我是你,我会先把它分成不同的页面:一页包含AV结果,一页包含CS结果,...

You also need to find a way to read the contents of the cells, I see following things to be done:您还需要找到一种方法来读取单元格的内容,我看到以下要做的事情:

  • Remove all AVO( and ) from all cells (at least that's how I understand the task)从所有单元格中删除所有AVO() (至少我是这样理解任务的)
  • Make a difference between cells, containing a comma, and the ones without (use arrays to store values of cells with commas)区分包含逗号的单元格和不包含逗号的单元格(使用 arrays 存储带逗号的单元格的值)
  • While reading the contents of the cells, beware of the space (sometimes present ( 40 AV ), sometimes not ( 40CS ))在阅读单元格内容时,请注意空格(有时存在( 40 AV ),有时不存在( 40CS ))

Once you have decyphered everything into different pages (and checked the correctness), you might summarise everything into one page.一旦您将所有内容解密为不同的页面(并检查了正确性),您可能会将所有内容汇总到一个页面中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM