2017-01-16 90 views
1

我有以下的Excel工作表和VBA宏,它取代每個x在列AY,AZ,通過細胞AY1,AZ1,BA1的價值BA:在Excel VBA宏中,如何遍歷大量列?

Excel sheet

macro

正如你可以看到,我的腳本的列名是硬連線的。我有很多專欄(約300)。如何讓我的腳本自動將列索引增加1 /遍歷許多列?

回答

0

嘗試這樣:

Sub ReplaceValues() 

Dim myRng, myColumn As Range 

Set myRng = Range("A:C").Columns ' <- this is you range, change A:C as needed 

For Each myColumn In myRng '<- this will loop through each column in your range 

    myColumn.EntireColumn.Replace What:="x", Replacement:=Cells(1, myColumn.Column).Text, LookAt:=xlWhole, _ 
     SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
     ReplaceFormat:=False 
Next 

End Sub 
+0

它的工作。就是我想要的。謝謝。 –

0

我還發現了另一種工作方法,通過finaly自己:

Dim col As Integer 

For col = 8 To 300 
    Columns(col).Replace What:="x", Replacement:=Cells(1, col).Text, LookAt:=xlWhole, _ 
     SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
     ReplaceFormat:=False 
Next