2017-08-10 61 views
-1

我試圖創建一個宏以在產品名稱末尾添加一個點(。),如果股票帳戶等於「Amaysim - Woolworths的」。vba代碼添加一個字符到一個基於另一個值的行

到目前爲止,我已經創建了下面的代碼,但我不知道如何添加只是點,而不是更改單元格的值旁邊的「Amaysim - Woolworths的」

Option Explicit 

    Public Sub Woolworths_update() 

    With ThisWorkbook.ActiveSheet.UsedRange 

     If ActiveSheet.AutoFilter Is Nothing Then .AutoFilter 
      .AutoFilter Field:=2, Criteria1:="Amaysim - Woolworths" 

      'I know I need to change something on the following line code but I do not know how to do it' 
      .Columns(3).Offset(1).Resize(.Rows.Count - 1) = "1/1/2010" 
      .AutoFilter 

     End With 

    End Sub 

This is a sample of the report

enter image description here

回答

0

下面已經過測試和工作。請注意,我爲了最佳實踐清理了一些代碼。

Option Explicit 

Public Sub Woolworths_update() 

    Dim ws As Worksheet 
    Set ws = Worksheets("myName") 'adjust to your sheet 

    With ws 

     .UsedRange.AutoFilter 'to turn off any existing autofilters 
     .UsedRange.AutoFilter Field:=2, Criteria1:="Amaysim - Woolworths" 

     Dim rChange As Range, rCellChange As Range 
     Set rChange = Intersect(.UsedRange, .UsedRange.Offset(1), .Cells(1, 3).EntireColumn).SpecialCells(xlCellTypeVisible) 

     For Each rCellChange In rChange 

      rCellChange.Value = rCellChange.Value & "." 

     Next 

     .UsedRange.AutoFilter 

    End With 

End Sub 
+0

你太棒了!你從哪裏學到了如何創建如此驚人的vba代碼? :) – Samayoa

+0

@Samayoa - 我不知道代碼有多棒,但我一直在寫VBA近15年,所以經驗是最好的老師。 (SO也是一位出色的老師)。 –

相關問題