2017-06-07 3 views
-1

VBAの新機能です。私はマクロを録音しようとしましたが、まだそれを理解していないようです。現在のセルの値を検索してセル番号に置き換えます。

私は次のようにマクロをしたい:

選択したセルでは、私は値を検索し、選択したセルによって発見されたものを「数」を交換したいです。

例:セルA1を選択し、その値は666とします.666を含むシート上のすべてのセルを検索して置換する必要があります。

本質的には、それらのすべてが最初のセルを指し示すようにします。

ありがとうございました。

答えて

0

基本的に、現在のシート上の使用範囲内のセルをループし、セルの値を置き換えます。以下は、あなたが何をしているかを行うコードです。私はそれぞれの行が何が起こっているのかを明確にするようコメントしました。

Sub Test() 
'Range variable to use for looping 
Dim rng As Range 

'String variable for finding the values. 
Dim searchvalue As String 

'We'll loop on the current sheet 
With ActiveSheet 

    'You need to have a single cell selected, if that's not the case we exit the macro. 
    If Selection.Count > 1 Then Exit Sub 

    'Get the value to search for 
    searchvalue = Selection.value 

    'Loop over all cells in the sheet that have values. 
    For Each rng In .UsedRange 

     'We do not want to overwrite the range from where we search (the selection) 
     If Intersect(rng, Selection) Is Nothing Then 

      'And if we find the value that was in the selection 
      If rng.value = searchvalue Then 

       Then create the =cell we selected formula. 
       rng.value = "=" & Selection.Address ' 
      End If 
     End If 

    'Next cell in the loop 
    Next rng 

'And we don't need the activesheet anymore. 
End With 
End Sub 

これはかなり一般的なものなので、一般的にはExcel VBAプログラミングをお読みになることをお勧めします。いくつかの素晴らしいリソースは以下のとおりです。

+0

ジェネリックかどうか、これは初心者のために把握することはそれほど単純ではありません。それにもかかわらず、本当にありがとう、私が必要としていることを正確に達成しました。メルシ! – Phil

関連する問題