2016-12-02 54 views
0

你好傢伙我試圖用currentdate更新我的一些單元格,所以我做了以下操作,但無法更新單元格。對於每個循環更新單元格

編輯:從整個代碼中添加缺少的功能。

Sub GetField32AFromCell() 

    For Each Cell In Worksheets("MM_Creation_Success").Range("G2:G9") 

     'Get cell which is right side of the current cell in for each loop 
     Dim nextRange As Variant: nextRange = Cell.Offset(0, 1).Value 

     'split the value of the cell into string array 
     Dim splitString() As String: splitString = SplitStringByNewLine(Cell) 

     For Each sString In splitString 

      Dim newLine As String: newLine = sString 
      newLine = TryGetField32A(newLine) 

      If Not newLine = "" Then 

       Dim newDateString As String: newDateString = GetCurrentDate("yymmdd") 
       Dim new32AField As String: new32AField = Mid(newLine, 7) 

       new32AField = newDateString + new32AField 
       'Set nextRange = new32AField 
       'MsgBox (nextRange) 
      End If 
     Next 
    Next 

End Sub 


    Public Function SplitStringByNewLine(stringArray As Variant) As String() 
     SplitStringByNewLine = Split(stringArray, Chr(10)) 
    End Function 

    Public Function TryGetField32A(newLine As String) As String 
     If newLine Like ("*:32A:*") Then 
     TryGetField32A = Mid(newLine, Len("*32A::"), Len(newLine)) 
     'MsgBox ("Found Tag 32A: " + field32AVal) 
    End If 
    End Function 

    Public Function GetCurrentDate(dateFormat As String) As String 
     Dim todayDate As String: todayDate = Format(Date, dateFormat,   vbMonday, vbUseSystem) 
     GetCurrentDate = todayDate 
     'MsgBox ("Current Date in Format (" + dateFormat + "): " + todayDate) 
    End Function 
+0

是不是你到這兒'splitString = SplitStringByNewLine(小區)'錯誤?試圖分配到一個數組 –

+0

對不起,我忘了粘貼其他功能 – user3167398

回答

0

如果你的意思是把new32AFieldnextRange代表的單元格,然後你首先需要確保nextRange實際上代表一個細胞,而不是包含它的值:

Dim nextRange As Range: Set nextRange = Cell.Offset(0, 1) 

然後你需要設置它的價值,而不是試圖改變所代表的範圍:

nextRange.Value = new32AField 
+0

我設法根據你的幫助修復!非常感謝! – user3167398