2013-02-15 40 views
-3
Sub Concat() 

    Do While ActiveCell <> "" 

     'The rows are filtered to display only "London" 
     'The changes required are for "London" only 

     If ActiveCell.Offset(0,0) = "London" Then 
     ActiveCell.Offset(0, 0).FormulaR1C1 = _ 
      ActiveCell.Offset(0, 0) & " " & ActiveCell.Offset(0, 1) 

     ActiveCell.Offset(0, 1) = "" 
     End If 
     ActiveCell.Offset(1, 0).Select 
    Loop 

End Sub 

我有接近13500行。 這不起作用。我看不到任何明顯的變化。Excel VBA腳本將兩個單元格內容連接成一個

+0

我沒有寫任何。我不知道從哪裏開始。或如何。 – Selvam 2013-02-15 05:15:53

+0

我試過了 - Sub ConcatColumns() Do While ActiveCell <>「」'循環直到活動單元格爲空。 'The「&」必須在兩邊都有空格,否則將被視爲長整數的可變類型「 」。 ActiveCell.Offset(0,1).FormulaR1C1 = _ ActiveCell.Offset(0,-1) 「」 &ActiveCell.Offset(0,0) ActiveCell.Offset(1,0)。選擇 環路 End Sub – Selvam 2013-02-15 06:18:30

回答

1
Sub Concat() 

    Do While ActiveCell <> "" 

     With ActiveCell 
     If .Value = "London" Then 
      .Value = .Value & .Offset(0, 1).Value 
      .Offset(0, 1).Value = "" 
     End If 
     End With 

     ActiveCell.Offset(1, 0).Select 

    Loop 

End Sub 
+0

你好,這是拋出一個「沒有做的循環」的錯誤。儘管在代碼中刪除了多餘的End If。 – Selvam 2013-02-15 08:03:34

+1

@Selvam你可以再試一次嗎? – Larry 2013-02-15 08:39:11

+0

謝謝。有效。進一步同樣,當行被過濾器摺疊,而不使用VBA腳本時,我可以用「值」填充所有顯示的行,而不更改隱藏行的內容嗎? – Selvam 2013-02-15 09:09:35

0

您可以使用for each語句來遍歷工作表的UsedRange所有行寫一個循環。對於每一行,請檢查行的第一列中的值是否爲「倫敦」,如果是,則使用column1 & column2的組合覆蓋該列的內容。

然後只需將值從欄3複製到列2和使用ClearContents明確欄3的值:

Sub Concat() 

    'Loop through all the rows 
    For Each Row In ActiveSheet.UsedRange.Rows 

    'Look at the cell in the first column of the current row 
    'If value is "London" concatinate column 1 and 2 and store in column1 
    'Note, the check for "London" is an exact, case sensative check! 

     If Row.Columns(1) = "London" Then 
      Row.Columns(1) = Row.Columns(1) & " " & Row.Columns(2) 
      Row.Columns(2) = Row.Columns(3) 
      Row.Columns(3).ClearContents 
     End If 
    Next Row 
End Sub 
+0

也感謝您的解決方案。 – Selvam 2013-02-15 08:55:11

相關問題