【问题标题】:Copy data from another worksheet based on cell values根据单元格值从另一个工作表复制数据
【发布时间】:2014-11-29 05:53:18
【问题描述】:

我不确定如何使用 VBA,并希望就我在 Excel 上使用 VBA 实现的目标寻求帮助。

  • 宏的目标是从工作表中复制一列数据“B”(即“选定的问题”)并将其粘贴到另一个工作表的固定列“A”中(即“试卷” )。

  • 然后,粘贴的值将匹配到另一个工作表(即“第 1 章”),如果“试卷”中的单元格包含与“第 1 章”的“A”列中的另一个单元格匹配的值,则它会将“第 1 章”中的特定数据行复制到“试卷”中

  • 为了让宏从“C”列的第一步开始重复,将值粘贴到“试卷”的“A”列中下一个可用空白单元格中

我目前的代码如下:

Sub Test()

'

Set Source = Sheets("Questions Selected")
Worksheets.Add(After:=Worksheets("Main Page")).Name = "Test Paper" 'Adds a Sheet called "Test Paper"
Dim rng As Range
Set Destination = Sheets("Test Paper")
Source.Select
Set rng = Range("B2:B" & Source.Cells(Source.Rows.Count, "B").End(xlUp).Row)
With rng
    .Copy
Destination.Columns(1).PasteSpecial xlPasteValues
End With
End Sub

我不明白如何继续我的 VBA,目前的编码只能让我迈出第一步。

感谢我能得到的所有帮助。

【问题讨论】:

  • 使用循环,用 .Cells(rowNum, colNum) 代替 .Range("A1") 在尝试评估动态范围集时为您提供更大的灵活性。这样你就可以得到最后一行或最后一列,并按数字循环遍历行和列。

标签: excel vba


【解决方案1】:

试试这个方法。它应该可以为您提供所需的东西。未测试:
这些循环和检查单个单元格的概念可以应用于许多不同的情况。策略性地将循环放置在其他循环中,使用行号和列号作为计数器并利用 .Cells(row,col) 格式。

Private Sub TableCheck()

Dim lastQsRow           'Last Row on Questions Selected
Dim lastQCol As Long    'Last Column on Questions Selected
Dim qColNum As Long     'Questions Selected Column Number

Dim lastTestRow As Long 'Last Test Paper Row
Dim tempRow As Long     'tempRow to keep track of place on Test Paper between loops
Dim testRow As Long     'Editing row on Test paper

Dim chapNum As Long     'Chapter Number for the Sheet Name
Dim lastChCol As Long   'Last Chapter Column
Dim lastChRow As Long   'Last Chapter Row
Dim chRow As Long       'Chaper Row
Dim chColNum As Long    'Chapter Column Number for copying entire row Loop.

'Set the Last Column on "Questions Selected"
lastQCol = Sheets("Questions Selected").Cells(1, Columns.Count).End(xlToLeft).Column
testRow = 2  'Set Row of "Test Paper" to 2 or whatever your first Non-Header Row is.

'-----PHASE ONE - COPY COLUMNS FROM "QUESTIONS SELECTED" TO "TEST PAPER" ----- BIG LOOP
For qColNum = 2 To lastQCol  'Begin Column Loop at column 2("B")
    tempRow = testRow
    lastQsRow = Sheets("Questions Selected").Cells(Rows.Count, qColNum).End(xlUp).Row
    'Get the Last Row of Column
    For qsRow = 2 To lastQsRow 'Loop from first NON-Header Row to the Last Row) on "Questions Selected"
        Sheets("Test Paper").Cells(testRow, "A").Value = Sheets("Questions Selected").Cells(qsRow, qColNum).Value
        testRow = testRow + 1
    Next qsRow

    '----PHASE TWO - COMPARE EACH ROW OF "TEST PAPER" TO "CHAPTERs" AND COPY MATCHING ROWS ---- INNER LOOP
    chapNum = 1
    'Get Last Row of "Chapter" & "Test Paper"
    lastChRow = Sheets("Chapter " & chapNum).Cells(Rows.Count, "A").End(xlUp).Row
    lastTestRow = Sheets("Test Paper").Cells(Rows.Count, "A").End(xlUp).Row

    'Loop through "Test Paper"
    For testRow = tempRow To lastTestRow
        'Loop through "Chapter"
        For chRow = 2 To lastChRow
            'Compare Value of Current Row on "Test Paper" to "Chapter"
            If Sheets("Test Paper").Cells(testRow, "A").Value = Sheets("Chapter " & chapNum).Cells(chRow, "A").Value Then
                lastChCol = Sheets("Chapter " & chapNum).Cells(chRow, Columns.Count).End(xlToLeft).Column
                'If Matching, copy every column from "Chapter" to "Test Paper"
                For chColNum = 2 To lastChCol
                    Sheets("Test Paper").Cells(testRow, chColNum).Value = Sheets("Chapter " & chapNum).Cells(chRow, chColNum).Value
                Next chColNum
            End If
        Next chRow
    Next testRow

    chapNum = chapNum + 1

Next qColNum

End Sub

【讨论】:

  • 您好,感谢您的回复,我已将代码输入到 VBA 中,它几乎可以完全运行。是否可以修改代码,使宏开始匹配来自不同工作表的问题,而不仅仅是第 1 章?例如,在它第一次将“B”列从“Questions Selected”复制到“Test paper”并匹配“Chapter 1”之后,它是否可以将“C”列从“Questions Selected”复制到“试卷”并将其与“第2章”匹配?如果你能帮助完成前两个循环,我应该能够在我的其余章节中继续它。
  • 好的。那么 B 列与第 1 章有关,C 列与第 2 章有关?
  • 更新了一次解决一个聊天者的解决方案,基本上只是将第二个主要循环移到了第一个循环内部。将该列复制到试卷后,为章节编号创建一个变量,然后使用该计数器来决定下一张是哪个表。插入了一个 tempRow 变量来跟踪我们在试卷上的中断位置,以免重复整个循环。如果不是全部的话,那应该可以帮助您完成大部分工作。
  • 感谢您的回复,我将尝试VBA代码并再次通知您是否可以工作! (:
  • 嗨,我已经尝试过编码。但是,对于我使用的 VBA 代码,我无法从除第 1 章和第 2 章之外的任何其他章节中检索问题。我有标记为“第 1 章 - 第 19 章”的章节。感谢您对此事的建议。
猜你喜欢
  • 2020-11-11
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多