2015-09-07 47 views
0

早上好,如何将突出显示的单元格值发送到另一列中的另一个单元格?

我做了一个宏,突出显示用户输入的单元格值,基本上它突出显示了来自列G,I和J的值。来自列G的值的值为列I或J有,但他们不是为了。我想让我的宏做的是匹配已经突出显示的这些值,并将它们移到列H,例如,如果G3具有与I5相同的值,请将值从I5移至H3。

Public Sub series() 
    'Definición de variables (Definition of variables) 
    Dim rango As String, valor As String, resultado As Range 
    Dim primerResultado As String, cont As Integer 

    'Solicitar información al usuario (Get information from the user) 
    rango = "A1:XFD1048576" 
    valor = InputBox("Ingresa el VALOR a buscar:") 
    If valor = "" Then Exit Sub 

    cont = 0 'Inicializar contador de coincidencias (Initialize Find) 

    'Primera búsqueda del valor dentro del rango (First search for value in the range) 
    Set resultado = Range(rango).Find(What:=valor, _ 
        LookIn:=xlValues, _ 
        LookAt:=xlWhole, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlNext, _ 
        MatchCase:=False, _ 
        SearchFormat:=False) 

    If Not resultado Is Nothing Then 'Si el resultado de la búsqueda no es vacío 
     primerResultado = resultado.Address 
     Do        'Inicia bucle para hacer varias búsquedas 
      If MsgBox("Resaltar Valor?", vbYesNo) = vbYes Then 
       cont = cont + 1 
       resultado.Interior.ColorIndex = 4 'Cambia el color de fondo de la celda 

      End If 
      Set resultado = Range(rango).FindNext(resultado) 'Vuelve a buscar el valor 



      ' Display a simple message box. 


     Loop While Not resultado Is Nothing And resultado.Address <> primerResultado 
      MsgBox ("Valores Encontrados: " & cont) 
    Else 
     MsgBox "Se encontraron " & cont & " coincidencias." 
    End If End Sub 
+0

什么是不为你工作:

值可以很容易地通过添加代码,你行后感动? – xidgel

回答

0

如果G3的值与I5的值相同,则将值从I5移到H3。

你的要求很奇怪。

您已经编写了突出显示单元格的代码。

你实际上说的是,以及高度你想移动它们的细胞。

但是,您是否确实想要移动该值(因此它不再位于原始单元格中)?如果不是,那么您可以简单地将公式添加到希望将值复制到的单元格中。

如果您确实希望VBA移动该值,则需要添加用于在应用颜色后移动该值的代码。

If Not resultado Is Nothing Then 

添加以下代码

Dim G as integer, H as integer, I as integer 
    ' note 7 represents the 7th column ie G 

    colG = 7 
    colH = 8 
    colI = 9 

    if cells(resultado.row,colG) = cells(resultado.row,colI) then 

     ' OPTION 1   
     ' if value in column G has the same value that in column I 
     ' move the value from column I to column H 
     cells(resultado.row,colH) = cells(resultado.row,colI) 
     resultado.value = "" 

     ' OPTION 2   
     ' if G3 has the same value that I5, move the value from I5 to H3. 
     ' Note the use of -2 
     cells(resultado.row,colH) = cells(resultado.row,colI-2) 

     ' now clear teh source cell 
     resultado.value = "" 


    end if 
+0

colG = 7 colH = 8 colI = 9 这些值不取列,它们取行。有没有其他方法可以引用列?谢谢你的帮助! –

+0

抱歉。我已经改变了这个:cells(colG,resultado.row)是这样的:cells(resultado.row,colG) – HarveyFrench

+0

嗨哈维,这个宏只适用于值在列I中的同一行,有没有办法搜索所有列I中的所有值,然后将其与列G中的值进行匹配?在这之后,将列I中的值移到列H中的相同值旁边。例如,如果$ 650在G:3中并且$ 650在I:7中,则将$ 650从I:7移到H:3旁边G:3的650美元。 –

相关问题