简体   繁体   English

VBA Excel:宏,该宏在一行中搜索特定单元格中的某个变量值,然后将值复制并粘贴到此列中

[英]VBA Excel : Macro that searches a row for a certain variable value from specific cell and then copy&pastes value to this column

I have done some VBA in the past but just cannot find a solution for this one. 我过去曾做过一些VBA,但无法为此找到解决方案。

I am looking for a macro which searches cells C4 to Z4 (one infinite long row starting from C4) for a value (number) from cell B4 which changes weekly. 我正在寻找一个宏,该宏从单元格C4到Z4(从C4开始的一个无限长行)中搜索一个每周更改的单元格B4的值(数字)。 If a match is found then copy&pastes the values of cells B5 to B100 (one infinite long column starting from B5) into the correct column C to Z (from C5 etc., downwards). 如果找到匹配项,则将单元格B5到B100的值(从B5开始的一个无限长列)复制并粘贴到正确的C到Z列(从C5等向下)中。

With correct column I mean the column where the macro finds the match between B4 and C4 to Z4. 对于正确的列,我的意思是宏在其中找到B4和C4与Z4之间匹配的列。 C4 to Z4 are non-identical. C4至Z4不相同。

I searched long and hard and the nearest I could find is this: Macro that looks for a value in a cell & then paste a range in the column of that cell. 我经过漫长而艰苦的搜索,发现的最接近的是: 宏,宏在单元格中查找值,然后将范围粘贴到该单元格的列中。 EXCEL 2007 2007年卓越

However it does not work for me. 但是,它对我不起作用。 The solution in that thread says that the matching cell values should be in a date format. 该线程中的解决方案说,匹配的单元格值应为日期格式。 I recontructed all of this, but even with dates instead of numbers it does not work. 我重新构造了所有这些内容,但是即使使用日期而不是数字也无法正常工作。 The macro always gives the message according to the VBA line 宏总是根据VBA行给出消息

MsgBox "Date Column for " & CStr([B2].Value) & " Not Found" MsgBox“&CStr([B2] .Value)和”找不到“的日期列

So it does not find any matches for me, even I run it with identical dates in the matching cells. 因此,即使我在匹配的单元格中以相同的日期运行它,也找不到适合我的匹配项。 (I changed of course this macro to my cell locations) (我当然将此宏更改为我的单元格位置)

This forum is my final try :) 这个论坛是我最后的尝试:)

I have following code which does not work: 我有以下代码不起作用:

Private Sub CommandButton2_Click()

Dim ws As Worksheet
Dim rSrc As Range
Dim rDst As Range
Dim cl As Range
Dim dat As Variant

Set ws = ActiveSheet

' Get the Source range
Set rSrc = ws.Range([B5], ws.Columns(2).Cells(ws.Rows.Count, 1).End(xlUp))
dat = rSrc

' Find the Destination column and copy data
Set rDst = ws.Range([D4], ws.Rows(1).Cells(1, ws.Columns.Count).End(xlToLeft))
Set cl = rDst.Find(What:=[B4], _
  After:=rDst.Cells(1, 1), _
  LookIn:=xlValues, _
  LookAt:=xlWhole, _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlNext)
If cl Is Nothing Then
    MsgBox "Column for " & CStr([B4].Value) & " Not Found"
Else
    Set rDst = cl.Offset(1, 0).Resize(UBound(dat, 1), 1)
    rDst = dat
End If

End Sub

Thank you. 谢谢。

Regards 问候

Sub FindandCopy
Dim what as range
dim where as range
dim found as range
set what = range("b4")  'what we're looking for
set where = range("c4")  'start of search range
do
if where = what then 
    set found = where  'that's where we found it
else
 set where = where.offset(0,1) 'otherwise keep looking
end if
loop until where = ""   'stop if blank
if found = "" then  'we fell off the end
      msgbox what & " not found "
else
      range(range("b5"),range("b5").end(xldown)).copy 
      found.offset(1,0).pastespecial xlpastevalues
end if
end sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM