【问题标题】:Match a Date between 2 Worksheets then Copy and Paste Specific Data匹配 2 个工作表之间的日期,然后复制和粘贴特定数据
【发布时间】:2015-08-06 05:08:49
【问题描述】:
  • 我有 2 个工作表。
  • 在 Sh1 中,我在单元格“R2”中输入日期。
  • 宏 然后应该在 Sh2 列“C”中搜索匹配项。
  • 找到匹配项时 它将从我的比赛下方的 2 个单元格和下面的 74 个单元格复制 在 Sh1 单元格“R3”中粘贴特殊 xlPasteValues。

下面的例子做了类似的事情,但不是想要的结果。

Option Explicit 
Sub FindStr() 

Dim rFndCell As Range 
Dim strData As String 
Dim stFnd As String 
Dim fCol As Integer 
Dim sh As Worksheet 
Dim ws As Worksheet 

Set ws = Sheets("CTN ORIGINAL") 
Set sh = Sheets("Ctn Daily - (enter data here)") 
stFnd = ws.Range("R2").Value 

With sh 
    Set rFndCell = .Range("C:C").Find(stFnd, LookIn:=xlValues) 
    If Not rFndCell Is Nothing Then 
        fCol = rFndCell.Column 
        ws.Range("B3:B33").Copy
        sh.Cells(6, fCol).PasteSpecial xlPasteValues
    Else 'Can't find the item
        MsgBox "No Find" 
    End If 
End With 

End Sub

【问题讨论】:

  • 不清除此部分“它将从我的匹配项下方的 2 个单元格和下方的 74 个单元格复制”。你能解释更多吗?
  • 例如,表 2 中的匹配项位于 C50 我希望将 C52:C156 中的所有单元格复制并粘贴到表 1 的 R3 中(仅限值)。匹配项的位置将始终更改所以不能是代码中的静态位置

标签: excel find copy-paste worksheet-function vba


【解决方案1】:

这里,我给你弄了一个,如果它不起作用,请告诉我。我已经对其进行了测试,它非常适合我。

Option Explicit

Sub findAndCopy()

    Dim foundCell As Range
    Dim strFind As String
    Dim fRow, fCol As Integer
    Dim sh1, sh2 As Worksheet

    'Set sheets
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")

    'Get find string
    strFind = sh1.Range("R2").Value

    'Find string in column C of Sheet2
    Set foundCell = sh2.Range("C:G").Find(strFind, LookIn:=xlValues)

    'If match cell is found
    If Not foundCell Is Nothing Then

        'Get the row and column
        fRow = foundCell.Row
        fCol = foundCell.Column

        'copy data from Sheet2 (from 2 cell below & 74 cells down)
        sh2.Range(Cells(fRow + 2, fCol).Address & ":" & Cells(fRow + 76, fCol).Address).Copy

        'paste in range R3 of Sheet1
        sh1.Range("R3").PasteSpecial xlPasteValues

        'Clear cache
        Application.CutCopyMode = False

    'If not found, show message.
    Else

        Call MsgBox("Not found the match cell!", vbExclamation, "Finding String")

    End If

End Sub

【讨论】:

  • 嗨尼古拉斯。对于标准数字,这工作得很好,但是我需要它来查找日期,到目前为止它是不成功的。
  • 我用 Date 测试过,它对我有用。你的输入数据是什么?给我看看。一件事是,如果您想使用日期,您的 Sheet1 中的 R2 单元格 必须是 Date 格式并且 Sheet2 的 C 列中的所有单元格也必须是 Date 格式。之后,它将起作用。如果没有,则不确定是否有效。
  • 两个单元格的日期格式完全相同 9/02/2015。匹配项下方的单元格为数字格式。
  • 您可以发布您的输入数据吗?所以,我可以考虑更多。
  • 我应该使用这样的东西吗? strdate = Format(strdate, "短日期")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
相关资源
最近更新 更多