简体   繁体   English

Excel宏复制包含当前数组值的所有单元格并将其粘贴到新工作簿中

[英]Excel Macro to copy all cells containing current array value and paste them to a new workbook

I've been given a spreadsheet that contains a list of furniture suppliers in Column C. The other columns contain information about the different furniture products they stock. 我得到了一个电子表格,其中包含C列中的家具供应商列表。其他列包含有关他们所库存的不同家具产品的信息。 My task is to copy all the cells containing information about those furniture products for each supplier and paste them into a new workbook. 我的任务是为每个供应商复制包含有关这些家具产品信息的所有单元格,并将它们粘贴到新的工作簿中。 There are around 66 different suppliers so obviously I don't really fancy doing this manually. 大约有66个不同的供应商,因此显然我不愿意手动进行此操作。 I thought someone here would have had to have done a similar task and might know how I could go about writing a Macro to solve the problem. 我以为这里的某人将不得不完成类似的任务,并且可能知道如何编写宏来解决该问题。

So far I've managed to write the following code. 到目前为止,我已经设法编写了以下代码。 It basically takes the users selection, loops through all the Cells in the selection. 它基本上需要用户选择,并遍历选择中的所有单元。 Takes the unique values (each new furniture supplier) and adds them to an array containing only the unique values. 获取唯一值(每个新家具供应商),并将它们添加到仅包含唯一值的数组中。 The problem I'm having is working out what to do next. 我遇到的问题是确定下一步该怎么做。

Sub addItemsToArray()
    Dim varIn As Variant 'User Selection
    Dim varUnique As Variant 'Array containing Unique Values
    Dim iInRow As Long 'Variable storing current row number
    Dim iUnique As Long 'Variable storing current unqiue array value
    Dim nUnique As Long 'Variable storing number of unique values in User Selection.
    Dim isUnique As Boolean 'Boolean Variable indicating whether current value is unique or not
    Dim sValue As Long 'I have included these two values to find start and end position for unique                 values in user Selection
    Dim lValue As Long

    varIn = Selection

    ReDim varUnique(1 To UBound(varIn, 1) * UBound(varIn, 2)) 'Set upper and lower bounds for VarUnique array, lower bound will be 1, upper will be last cell in selection

    nUnique = 0 'Number of Unique values set as 0 by default

    'Looping through all Values in User Selection
    For iInRow = LBound(varIn, 1) To UBound(varIn, 1)

            isUnique = True 'First value will always be unique so set isUnique to True

            'Loop through from 1 to the Number of Unique Values in Array. Set to 0 by default.
            'If CurrentCell Value is equal to element in array then it is not Unique, as such isUnique will be set to False and For loop will be exited.
            For iUnique = 1 To nUnique
                If varIn(iInRow, 1) = varUnique(iUnique) Then
                    isUnique = False
                    Exit For
                End If
            Next iUnique


            If isUnique = True Then
                sValue = lValue
                nUnique = nUnique + 1
                varUnique(nUnique) = varIn(iInRow, 1)
                lValue = iInRow
            End If
    Next iInRow
    '// varUnique now contains only the unique values.
    '// Trim off the empty elements:
    ReDim Preserve varUnique(1 To nUnique)
End Sub

If anyone could help point me in the right direction I would very much appreciate it. 如果有人能帮助我指出正确的方向,我将非常感激。

I've included an image of the worksheet below. 我在下面提供了工作表的图像。 As you can see Column C contains the list of suppliers. 如您所见,列C包含供应商列表。 What I need to do is, copy all cells for each supplier, place those cells in a new worksheet and save it, with the name of the supplier as the file name. 我需要做的是,复制每个供应商的所有单元格,将这些单元格放在新的工作表中并保存,并将供应商的名称作为文件名。 I hope that's made it a bit clearer. 我希望这使它更加清楚。 在此处输入图片说明

Sub Parse_Furniture_Suppliers()
    Dim tmpCell As Range, rngHeaders As Range, rngTarget As Range

    Set rngHeaders = ActiveSheet.Range("A1:F1")
    Set tmpCell = ActiveSheet.Range("C2")

    Workbooks.Add
    ActiveSheet.Range("A1:F1").Value = rngHeaders.Value
    Set rngTarget = ActiveSheet.Range("A2")
    rngTarget.Select
    ActiveWindow.FreezePanes = True
    rngTarget.Resize(1, 6).Value = tmpCell.Offset(0, -2).Resize(1, 6).Value
    Set rngTarget = rngTarget.Offset(1)
    Set tmpCell = tmpCell.Offset(1)

    Do While tmpCell.Value <> ""
        If tmpCell.Value <> tmpCell.Offset(-1).Value Then
            ActiveWorkbook.SaveAs tmpCell.Offset(-1).Value
            ActiveWorkbook.Close
            Workbooks.Add
            ActiveSheet.Range("A1:F1").Value = rngHeaders.Value
            Set rngTarget = ActiveSheet.Range("A2")
            rngTarget.Select
            ActiveWindow.FreezePanes = True
        End If

        rngTarget.Resize(1, 6).Value = tmpCell.Offset(0, -2).Resize(1, 6).Value
        Set rngTarget = rngTarget.Offset(1)
        Set tmpCell = tmpCell.Offset(1)
    Loop

    ActiveWorkbook.SaveAs tmpCell.Offset(-1).Value
    ActiveWorkbook.Close
End Sub

暂无
暂无

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

相关问题 Excel VBA宏,它将复制一系列单元格并粘贴到另一个工作簿中 - Excel VBA macro that will copy a range of cells and paste into another workbook 复制某些单元格并将其粘贴到新工作表中的宏 - Macro to copy certain cells and paste them into a new worksheet Excel VBA代码,用于在一个工作簿中选择包含特定文本字符串的单元格,然后将这些单元格复制并粘贴到新工作簿中 - Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? 如何复制特定的单元格并将其粘贴到新工作簿 - How to copy specific cells and paste to a new workbook 复制并粘贴数组宏Excel - Copy and paste with array macro excel 从一个工作簿复制和粘贴到当前工作簿的宏 - Macro To Copy And Paste From One Workbook To current workbook Excel VBA 选择包含文本字符串的单元格,然后将这些单元格复制并粘贴到新的工作簿中 - Excel VBA to select cells that contain a text string, and then copy and paste these cells into a new workbook Excel VBA宏复制单元格并粘贴到另一个 - Excel vba macro to copy cells and paste in another 宏以复制格式化的单元格并粘贴到新的工作表中 - Macro to copy formatted cells and paste into new sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM