【问题标题】:Finding status of data and copying across sheets查找数据状态并跨工作表复制
【发布时间】:2016-12-23 20:20:03
【问题描述】:

我对 VBA 很陌生。希望有人可以帮助我。非常感谢。

表 1(要复制到表 4 的数据)

     A       B        C           D
 1  ID    Header 2  Header 3    Orders
 2 5000                      455,476,497
 3 5012                          500
 4 5015                        502,503 

表 2(数据)

     A         B         C         D ........ Q
1  Orders ID         Header 2   Status   Header 4
2   455                         Closed
3   456                          Open
4   476                         Closed
5   497                         Closed

表 3

   A    B    C    D
1 455  476  497
2 500
3 502  503

表 4(输出表)

     A       B        C           D
 1  ID    Header 2  Header 3    Orders
 2 5000                      455,476,497
 3

任务:我需要检查工作表 3 中以下 ID 455、476 和 497 的状态。如果一行中所有 ID 的状态都已关闭,则将工作表 1 中的整行复制到工作表 4,如果不移动到下一行。

For a = 1 To Range("A1").End(xlDown).Row

    For b = 1 To Range("A1").End(xlToRight).Column
        Cells(1, b).Select

        Selection.Copy
        Sheets("Orders").Select            

       (Unsure what to put here)

    Next b
Next a

我需要更多声望才能在此处发布图片。所以,发布链接 (只允许 2 个)

http://imgur.com/K8H2JhDhttp://imgur.com/KjeIDVm、U0Z7mfm、qWOJ3VM

【问题讨论】:

  • 指定要粘贴复制值的单元格。
  • 我想将单元格 A1 中的表 3 中复制的单元格与表 1 中的标题一起粘贴。它有从 A1 到 Q1 的标题。
  • 对不起,我有点困惑。您能否粘贴您的数据和预期输出的示例屏幕打印。如果不解释清楚一点。
  • 我的理解是,Sheet1 的 id 对应于 sheet2 中存在的值,因此在 sheet1、500..etc 中会有一个 id 为 455 的条目,所以当你看到所有 455,476,497 的状态时已关闭,您需要复制对应于 455,476,497 的行(来自 sheet1 的 3 行)并粘贴到 sheet3。如果 502 已关闭且 503 已打开,则在同一行上,应该跳过它我的理解是否正确?
  • 我已更新工作表详细信息。我想你明白我想做什么。检查 456,476,497,如果它们的所有状态都已关闭,则在工作表 1 中找到原始行并将其粘贴到工作表 4 中,包括工作表 1 中的标题。

标签: excel ms-access vba excel-2007


【解决方案1】:

请尝试以下代码

Sub FindStausAndCopy()

Dim sheet1Range As Range
Dim sheet2Range As Range
Dim sheet3Range As Range

Dim sheet1RowCount As Integer
Dim sheet1ColCount As Integer

Dim sheet2RowCount As Integer
Dim sheet2ColCount As Integer

Dim sheet3RowCount As Integer
Dim sheet3ColCount As Integer

Dim shtRowNum As Integer
Dim totalCellsinRow  As Integer
Dim statusCount As Integer
Dim orders As String

Dim range1Row As Variant
Dim range2Row As Variant
Dim range3Row As Variant
Dim cellVal As Variant



sheet1RowCount = Worksheets("Sheet1").UsedRange.Rows.Count
sheet1ColCount = Worksheets("Sheet1").UsedRange.Columns.Count

sheet2RowCount = Worksheets("Sheet2").UsedRange.Rows.Count
sheet2ColCount = Worksheets("Sheet2").UsedRange.Columns.Count

sheet3RowCount = Worksheets("Sheet3").UsedRange.Rows.Count
sheet3ColCount = Worksheets("Sheet3").UsedRange.Columns.Count

Worksheets("sheet1").Activate
Set sheet1Range = Worksheets("Sheet1").Range(Cells(1, 1), Cells(sheet1RowCount, sheet1ColCount))
Worksheets("sheet2").Activate
Set sheet2Range = Worksheets("Sheet2").Range(Cells(1, 1), Cells(sheet2RowCount, sheet2ColCount))
Worksheets("sheet3").Activate
Set sheet3Range = Worksheets("Sheet3").Range(Cells(1, 1), Cells(sheet3RowCount, sheet3ColCount))

shtRowNum = 1 'This is for incrementing the Row in Sheet4
'Iterating through Each row in Sheet3 and then through
'each cell in a particular row
'Loop1
For Each range3Row In sheet3Range.Rows
totalCellsinRow = 0  ' to count no of order numbers in sheet3 rows
statusCount = 0      ' to count the status of orders 
orders = ""          ' to store all order numbers with coma seperated

    'Iterating throgh each Order in a row and identifing the status
    'Loop2
    For Each cellVal In range3Row.Cells
    If (cellVal <> "") Then
     totalCellsinRow = totalCellsinRow + 1 'Increments for every order
     'Iterating through each row in sheet2 to check the status and
     ' Increment status count
     'Loop3
         For Each range2Row In sheet2Range.Rows
            If (range2Row.Cells(1) = cellVal And range2Row.Cells(4) = "Closed") Then
            statusCount = statusCount + 1 'Increments only when order is closed
            orders = orders & ", " & cellVal
            End If
        Next range2Row
        'By the time Loop3 is completed we get the status of one order
        End If
    Next cellVal
    'By the time Loop2 is completed, we get the overall status of all orders
    ' in a row of sheet3
    ' If statusCount = totalCellsinRow which implies every order
    ' present in a row is closed
    If (totalCellsinRow = statusCount) Then
        'Lopp4: Iterating throgh each row of sheet1 to find Matching ID
        'The reason for iterating through rows,even if the order of the ID
        ' changes, code should be in a position to identify the right row
        ' to copy
        For Each range1Row In sheet1Range.Rows
            If (range1Row.Cells(4) = Trim(Right(orders, Len(orders) - 1))) Then
              If (shtRowNum = 1) Then
              'Copying the Header row to sheet4 only once.
              sheet1Range.Rows(1).Copy Destination:=Worksheets("sheet4").Cells(1, 1) 
              shtRowNum = shtRowNum + 1
            End If
            'Copying the row from sheet1 to sheet4
            range1Row.Copy Destination:=Worksheets("Sheet4").Cells(shtRowNum, 1)
            shtRowNum = shtRowNum + 1
          End If
        Next range1Row
        'By the time Loop4 is completed a ID row for the closed Orders will 
        ' be copied to Sheet4
    End If
Next range3Row
'By the time Loop1 is completed all the orders status will be read
' Corresponding Id rows will be copied to sheet4 with Header row

End Sub

以下是结果

【讨论】:

  • 如果找到该元素,您可以通过 sheet2Range.Rows 中的元素打破循环。此外,您不必遍历 Sheet1,只需在第 N+1 行找到它,如果您在 Sheet 3 的第 N 行找到它。
  • 非常感谢您抽出宝贵时间! :) 我会试试看。
  • 嗨 Siva,当我尝试将它集成到我的工作表中时,我遇到了一些错误。你能解释一下你的代码是如何从 ShtRowNum = 1 工作的吗?我已经相应地编辑了我的代码。
  • @Manick9,我在代码中添加了我的 cmets。希望我说清楚了,如果您有任何不清楚的地方,请告诉我。如果可能,请在 Question 中分享您的整个代码,我们可以解决问题并使其正常工作。
  • 我已经上传了链接。请告知如何解决此问题。我认为这是因为assigned_tasks 是空的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
相关资源
最近更新 更多