Tariq Khalaf
Tariq Khalaf

Reputation: 89

Changing VBA macro code to change number part 2

I have had to open a new question following a previous question i had to decrease and increment a number which is on the link bellow

Changing VBA macro code to change number

this is the code that i am trying to work with and i got it almost to work but somewhere its gone wrong.

Bulkwks.[B5] is M20

historywks.[a2] is the time

historywks.[b2] is the name

historywks.[C2] is 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

Upvotes: 0

Views: 415

Answers (1)

glh
glh

Reputation: 4972

You'll need to use the myOut variable.

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

Upvotes: 1

Related Questions