简体   繁体   English

select 如果满足条件,则复制并粘贴到另一个单元格中的范围

[英]select a range to copy and paste in another cell if a condition is met

Dim word As Variant
Dim range As Variant


If cells(5, [6]) = "Bulk Density" Then
range = ("A3:K11")


For Each word In range
        If word = "Moisture Content" Then
            [M20] = word.Offset(0, 1)
            Exit For
        End If
        Next
        End If
End Sub

Hi im trying to select a range based on cell f5 .嗨,我正在尝试 select 基于单元格f5的范围。 And then if that word is met in f5 i want to select a certain range ( A3:K11 ) where i can copy data from and move it to another cells.然后,如果在f5中遇到该词,我想 select 一定范围( A3:K11 ),我可以从中复制数据并将其移动到另一个单元格。 For example if cells f5 contains "Bulk density" i then want to copy the moisture content value in the range of cells (which is not always in the same row) and then paste the value into M20 .例如,如果单元格f5包含“体积密度”,那么我想复制单元格范围内的水分含量值(并不总是在同一行中),然后将该值粘贴到M20中。 The copy and paste of the value works when i put the range in automatically but when i want it select the range itself the word variable is always empty and im not sure why.当我自动放入范围时,值的复制和粘贴有效,但是当我想要它 select 范围本身时,单词变量始终为空,我不知道为什么。 Any help would be greatly appreicated thanks max任何帮助将不胜感激,感谢max

在此处输入图像描述

A Kind of LookUp一种查找

  • The first Sub addresses your immediate case ( Bulk Density... ).第一个子解决您的直接情况( Bulk Density... )。
  • If you want to apply the same logic to other 'properties', edit the third Sub (which is calling the second Sub) to fit your needs.如果您想将相同的逻辑应用于其他“属性”,请编辑第三个 Sub(调用第二个 Sub)以满足您的需要。

The Code编码

Option Explicit

Sub writeValueConst()

    Const CheckString As String = "Bulk Density"
    Const CheckCellAddress As String = "F6"
    Const Searchstring As String = "Moisture Content"
    Const SearchRangeAddress As String = "F3:F11"
    Const ColumnOffset As Long = 1
    Const WriteCellAddress As String = "M20"
    
    Dim cel As Range
    If Range(CheckCellAddress).Value = CheckString Then
        For Each cel In Range(SearchRangeAddress).Cells
            If cel.Value = Searchstring Then
                Range(WriteCellAddress).Value _
                  = cel.Offset(0, ColumnOffset).Value
                Exit For
            End If
        Next cel
    End If

End Sub

Sub writeValue(ByVal CheckString As String, _
               ByVal CheckCellAddress As String, _
               ByVal Searchstring As String, _
               ByVal SearchRangeAddress As String, _
               ByVal WriteCellAddress As String, _
               Optional ByVal ColumnOffset As Long = 1)

    Dim cel As Range
    If Range(CheckCellAddress).Value = CheckString Then
        For Each cel In Range(SearchRangeAddress).Cells
            If cel.Value = Searchstring Then
                Range(WriteCellAddress).Value _
                  = cel.Offset(0, ColumnOffset).Value
                Exit For
            End If
        Next cel
    End If

End Sub

Sub writeDensities()
    Dim Series(1) As Variant
    
    ' If you add or remove, you have to adjust the previous number after Series.
    Series(0) = Array("Bulk Density", "F6", "Moisture Content", _
                      "F3:F11", "M20", 1)
    Series(1) = Array("Dry Density", "F8", "Degree of Compaction", _
                      "F3:F11", "M21", 1)
    
    Dim j As Long
    For j = 0 To UBound(Series)
        writeValue Series(j)(0), Series(j)(1), Series(j)(2), _
                   Series(j)(3), Series(j)(4), Series(j)(5)
    Next j

End Sub

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

相关问题 如果条件满足,如何复制单元格并粘贴到另一个单元格中? - How to copy cell if condition is met and paste in another cell? 如果满足两个范围标准,请复制第三个单元格并将其粘贴到另一张纸上 - If two range criteria are met, copy a third cell and paste it to another sheet 宏:如果满足条件,则复制粘贴单元格 - macro: copy paste cell if condition met 满足条件时,将仅行的一部分复制粘贴到另一张纸上,其中一个单元格包含图片 - Copy paste only part of rows, with one cell containing a picture, on another sheet when condition is met 如果满足条件,则将单元格复制到另一张工作表 - Copy cell to another sheet if condition is met 在满足条件的情况下将单元格复制到另一个工作表 - Copy Cell to another sheet in condition is met Loop 满足条件时将各种单元格复制并粘贴到另一个工作表 - Copy and Paste various cells to another sheet when condition met 如果满足条件,则将数据从一个单元格复制到另一个单元格 VBA - Copy data from one cell to another if condition met VBA 如何从另一个工作簿复制一系列单元格并将其粘贴到另一个工作簿? - How to copy a range of cell from another workbook and paste it to another one? 复制单元格范围并根据日期粘贴到另一个工作表中? - Copy cell range and paste in another worksheet based on the date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM