2016-12-15 32 views
-1

我有一套代碼,我正面臨增量數字問題。 請檢查這部分的「第一批代碼」,我需要幫助或指導我卡住的部分。開發增量+1功能的問題

str = field1 & "|" & field2 & "|" & combine & "|" 

我想做一些類似於我附加的「第二批代碼」的函數。 「第二批代碼」:如果日期等於今天的日期,則數字將增加,例如,如果我保存Excel表格並關閉它,當我重新打開表格(第二批代碼)時,數字因爲[B2]會遞增+1。

我已經堅持了一段時間的「第一束代碼」,因爲我需要讓這部分(第一一堆代碼)

|" & combine & "|" 

輸出爲我的「第一次幫代碼」(只是一個例子)是

00|?????|AJ_20161216_001||||||||||||||||||||||||||| 

輸出被保存在File01.txt和源是從‘C:\文件Header.xls’

對於‘第一束代碼’,我需要製作'聯合'變量這是字符串增量像第二組代碼沒有保存並關閉Excel並重新打開它再次執行+1的增量功能。讓說 當我按下按鈕,第一個輸出將

00|?????|AJ_20161216_001||||||||||||||||||||||||||| 

當我按下了第二次按鈕生成的輸出必須(這部分也保存在file01.txt如前file01.txt包含AJ_20161216_001將被刪除)

00|?????|AJ_20161216_002||||||||||||||||||||||||||| 

下面的代碼是「代碼的第一束」我一直在談論

Sub CreatePFHeaderFooter() 
Dim myfile As String 
//file location 
myfile = "C:\File Header.xls" 
Application.Workbooks.Open FileName:=myfile 
DatFile1Name = ThisWorkbook.path + "\File01.txt" 
Open DatFile1Name For Output As #1 'create csv file 

//declaration of all cell into variable 
vRow = 2 
While Cells(vRow, 1).Value <> "" 
field1 = Cells(vRow, 1).Value 
field2 = Cells(vRow, 2).Value 
Field3 = Format(Now(), "AJ""_""YYYYMMDD""_") 
'realfield3 = Cells(vRow, 3).Value 

field4 = Cells(vRow, 4).Value 
field5 = Cells(vRow, 5).Value 
field6 = Cells(vRow, 6).Value 
field7 = Cells(vRow, 7).Value 
field8 = Cells(vRow, 8).Value 
field9 = Cells(vRow, 9).Value 
field10 = Cells(vRow, 10).Value 
field11 = Cells(vRow, 11).Value 
field12 = Cells(vRow, 12).Value 
field13 = Cells(vRow, 13).Value 
field14 = Cells(vRow, 14).Value 
field15 = Cells(vRow, 15).Value 
field16 = Cells(vRow, 16).Value 
field17 = Cells(vRow, 17).Value 
field18 = Cells(vRow, 18).Value 
field19 = Cells(vRow, 19).Value 
field20 = Cells(vRow, 20).Value 
field21 = Cells(vRow, 21).Value 
field22 = Cells(vRow, 22).Value 
field23 = Cells(vRow, 23).Value 
field24 = Cells(vRow, 24).Value 
field25 = Cells(vRow, 25).Value 
field26 = Cells(vRow, 26).Value 
field27 = Cells(vRow, 27).Value 
field28 = Cells(vRow, 28).Value 
field29 = Cells(vRow, 29).Value 

//Incomplete parts 
Dim str As String 
Dim calc As Long 
calc = 1 + 1 
formcalc = Format(calc, "000") 
combine = 0 
combine1 = "" 

//This is the part where i got stuck 
If [C2] = "" Then 
    combine = Field3 + formcalc 
Else 

str = "" 
str = field1 & "|" & field2 & "|" & combine & "|" & field4 & "|" & field5 & "|" & field6 & "|" & field7 & "|" & field8 & "|" & field9 & "|" & field10 & "|" 
str = str & field11 & "|" & field12 & "|" & field13 & "|" & field14 & "|" & field15 & "|" & field16 & "|" & field17 & "|" & field18 & "|" & field19 & "|" & field20 & "|" 
str = str & field21 & "|" & field22 & "|" & field23 & "|" & field24 & "|" & field25 & "|" & field26 & "|" & field27 & "|" & field28 & "|" & field29 & "|" 


Print #1, str 
vRow = vRow + 1 

Wend 
Close #1 

ActiveWorkbook.Close 

這裏是「的代碼第二束」,其是相似的功能我需要第一束碼

Private Sub Workbook_Open() 

If [B1] = "" Then 
    [B1] = Format(Now(), "dd/mm/yyyy") 
    [B2] = 1 
Else 
    If Trim([B1]) <> Format(Now(), "dd/mm/yyyy") Then 
     [B1] = Format(Now(), "dd/mm/yyyy") 
     [B2] = 1 
Else 
    [B1] = Format(Now(), "dd/mm/yyyy") 
    [B2] = [B2] + 1 
End If 
End If 

End Sub 

對不起要做,我的無組織的格式道歉。

+0

'calc'有它的被保留值的子以外的聲明。 – Slai

+0

好吧,指出,我會做出更正 –

回答

1

複雜的任務應該從主子程序中提取到他們自己的子程序中。

這裏我創建了getNewID來增加ID。

最後一個單元格後面不應有分隔符。這將創建一個空列,這將導致讀取文件時出現問題。

str = field1 & "|" & ... & field29 & "|"

Sub CreatePFHeaderFooter() 

    Dim x As Long, y As Long 
    Dim data(1 To 29) As String 
    Dim myfile As String 
    'file location 
    myfile = "C:\File Header.xls" 
    Application.Workbooks.Open Filename:=myfile 
    DatFile1Name = ThisWorkbook.Path + "\File01.txt" 
    Open DatFile1Name For Output As #1 'create csv file 

    x = 2 
    While Cells(x, 1).Value <> "" 

     If Cells(x, 3) = "" Then Cells(x, 3) = getNewID(Cells(x - 1, 3)) 

     For y = 1 To 28 
      data(y) = Cells(x, y) 
     Next 

     Print #1, Join(data, "|") 
     x = x + 1 
    Wend 

    Close #1 

    ActiveWorkbook.Close 
End Sub 

Function getNewID(OldID As String) As String 
    Dim arr() As String, strDate As String 
    Dim d As Date 

    arr = Split(OldID, "_") 
    strDate = arr(1) 
    d = DateSerial(Left(strDate, 4), Mid(strDate, 5, 2), Right(strDate, 2)) 

    If d = Date Then 
     arr(2) = Format(CInt(arr(3)) + 1, "000") 
    Else 
     arr(1) = Format(Date, "yyyymmdd") 
     arr(2) = "001" 
    End If 

    getNewID = Join(arr, "_") 
End Function 
+0

@lyeyannian是'getNewID'在一個標準的模塊? – 2016-12-16 01:25:53

+0

嗨,忽略這個問題,我已經解決了它,現在我遇到了最後一個問題,如果我在函數getnewID結束後添加messagebox,我會得到編譯錯誤:只有註釋可能出現在結束子,結束函數,或結束財產。 –

+0

@lyeyannian你應該看這個系列:[Excel VBA介紹](https://www.youtube.com/playlist?列表= PLNIs-AWhQzckr8Dgmgb3akx_gFMnpxTN5)。這是相當重要的:[Excel VBA簡介第3部分 - 如何處理事情出錯(錯誤和調試)](https://www.youtube.com/watch?v=DlqKfYTgc9Q&index=3&list=PLNIs-AWhQzckr8Dgmgb3akx_gFMnpxTN5&t=40s) – 2016-12-16 02:00:09