2013-02-21 34 views
0

我不得不打開一個新的問題,下面前一個問題,我不得不降低,並增加了一些它是鏈接波紋管更改VBA宏代碼變更號碼,第2部分

Changing VBA macro code to change number

這是代碼,我正在努力工作,我得到它幾乎工作,但在某個地方出錯了。

Bulkwks.[B5]M20

historywks.[a2]是時間

historywks.[b2]是名

historywks.[C2]m201001

Sub bulkON_Click() 

    Dim trnwkbk As Workbook 
    Dim Bulkwks As Worksheet 
    Dim Deswkbk As Workbook 
    Dim LogNum As Range, LastNum, NewNum, 
    Dim historywks As Worksheet 

    Dim nextRow As Long 
    Dim lOR As Long 
    Dim myIn As String 
    Dim myLeft As String 
    Dim myMid As Integer, myRight As Integer, i As Integer 
    Dim myOut As String 

    Set trnwkbk = Workbooks("Transport.xls") 
    Set Bulkwks = trnwkbk.Worksheets("Bulk") 

    lOR = MsgBox("Have you selected the right MIS or HUB or PSA number?", vbQuestion + vbYesNo, "Number Order") 
    If lOR = vbNo Then 
    MsgBox "Please select right Order Number" 
    Else 
    Application.ScreenUpdating = False 
    ' for testing i just made it post in test sheet in same workbook 
    'Set Deswkbk = Workbooks.Open("\\dunton01\Inspections\TRANSPORT\New_transport\data\Febuary_2013.xls") 
    'Set historywks = Deswkbk.Worksheets("Data") 
    Set historywks = Worksheets("test") 


    Set LogNum = historywks.[C2] 


    With historywks 
      nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row 
    End With 

    If LogNum(2, 1) = "" Then 
     LastNum = LogNum 
    Else 
     LastNum = LogNum(LogNum.End(xlDown).Row - 1, 1) 
    End If 

    NewNum = Bulkwks.[B5] & Val(Mid(LastNum, 2)) + 1 

    If LogNum(2, 1) = "" Then 
     LogNum(2, 1) = NewNum 

    Else 

    myIn = LogNum 
    myLeft = Left(myIn, 1) 
    myMid = CInt(Mid(myIn, 2, 2)) 
    myRight = CInt(Right(myIn, 4)) 
    myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000") 
    i = 0 

    Debug.Print "IN:  " & myIn 
    Debug.Print "BROKEN UP: " & myOut 

    Do Until myMid = -1 
     Debug.Print "ITERATION " & Format(i, "00") & ": " & myLeft & Format(myMid, "00") & Format(myRight, "0000") 

     myMid = myMid - 1 
     myRight = myRight + 1 
     myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000") 
     i = i + 1 

    With historywks 
      'enter date and time stamp in record 
      With .Cells(nextRow, "A") 
       .Value = Now 
       .NumberFormat = "mm/dd/yyyy hh:mm:ss" 
      End With 

      'enter user name in column B 
      .Cells(nextRow, "B").Value = Application.UserName 
      .Cells(nextRow, "C").Value = myIn 
    End With ' for testing i just disabled this Deswkbk.save 
    Loop 
    ' for testing i just disabled this 
    'Deswkbk.Close savechanges:=True 
    Application.ScreenUpdating = True 

    Bulkwks.[E3] = NewNum 

    End If 

    ' for testing i just disabled this 
    'Call File_In_Network_Folder 

    End If 
    End Sub 
+0

什麼不行? – glh 2013-02-21 09:01:30

回答

1

你需要使用myOut變量。

.Cells(nextRow, "C").Value = myOut