2016-05-13 378 views
3
Sub Exercise()    ' ' to read data from file tasks.xls and 
    Dim Arr As Variant, Arr1 As Variant ' feed the task name for the person 
    Dim iRow As Integer     ' in a month in this file 
    Dim iCol As Integer 
    Dim i As Integer, x As Integer 
    Dim name As String 

    'name = Cells(1, 1).Value 
    Arr = Workbooks.Open("E:tasks.xlsx").Sheets("Sheet1").Range("B1:E1").Value 
    Arr1 = Workbooks.Open("E:tasks.xlsx").Sheets("Sheet1").Range("B2:E2").Value 
    Sheets(1).Cells(1, 1).Select     ' go to beginning cell 

    For i = 1 To Arr1(1, 1) 
     Cells(6, 4 + i).Value = Arr(1, 1) 
     a = i + 4 
    Next i 


    For i = 1 To Arr1(1, 2) 
     Cells(6, a + i).Value = Arr(1, 2) 
     b = a + i 
    Next i 

    For i = 1 To Arr1(1, 3) 
     Cells(6, b + i).Value = Arr(1, 3) 
     C = b + i 
    Next i 

    For i = 1 To Arr1(1, 4) 
     Cells(6, C + i).Value = Arr(1, 4) 
     d = a + i 
    Next i            

    Do While ActiveCell.Row <> Sheets(1).Range("A" & Rows.Count).End(xlUp).Row 
               ' some times i get infinte loop 
    ActiveCell.Offset(2, 0).Select    ' span till the last 
    name = ActiveCell.Value      ' non empty row 
    Arr = Sheets(1).Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 5)).Value 
    Arr1 = Sheets(1).Range(ActiveCell.Offset(1, 1), ActiveCell.Offset(1, 5)).Value 


    With ThisWorkbook.Sheets(3)   'algorithm to search the name                       '            positon in this excel file 
    Dim findrow As Range 
    Set findrow = .Range("A:A").Find(What:=name, LookIn:=xlValues) 
    iRow = findrow.Row     ' required row where name is found 

    For i = 1 To Arr1(1, 1) 
     Cells(iRow, 4 + i).Value = Arr(1, 1) 
     a = i + 4 
    Next i 

    For i = 1 To Arr1(1, 2) 
     Cells(iRow, a + i).Value = Arr(1, 2) 
     b = a + i 
    Next i 

    For i = 1 To Arr1(1, 3) 
     Cells(iRow, b + i).Value = Arr(1, 3) 
     C = b + i 
    Next i 

    For i = 1 To Arr1(1, 4) 
     Cells(iRow, C + i).Value = Arr(1, 4) 
     d = a + i 
    Next i 

    Loop 

End Sub 

我被要求爲公司設計工作分配流程。 以給定的方式分配工作: 如果假設任務T1被分配給一個人5天,則應在工作分配文件中連續5天顯示他的姓名。我已經使用宏在MS Excel中編寫了一個Visual Basic代碼。我能夠在日期正確地分配工作,但不能向正確的人員分配工作。 。我得到錯誤信息「Object variable or With block variable not set」

**Workallotment.xlsm** - **Output** 
Anand-Web apps    1 2 3 4 5 6 7 8 9 10 11 12 
Praveen      T1 T1 T1 T1 T2 T2 T2 T3 T4 T4      
Bharath Vijay                   
Kailash                   
Sriram                   
Walter      c1 c2 c2 c3 c3 c3 c4 c4 c4 c4      
Harshith                    
Karthik      P1 P1 P1 P1 P1 P1 P2 P2 P2 P3 P3 P4 
Arvind                   
Anirudh-Mob apps                    
Sharath                   

**Tasks.xls** 

Praveen  T1 T2 T3 T4 
      4 3 1 2 
Karthik  P1 P2 P3 P4 
      6 3 2 1 
Walter  c1 c2 c3 c4 
      1 2 3 4 

I m executing the macro from 3rd sheet -May from workallotment.xlsm and invoking tasks.xls from the macro in workallotment.xlsm.The final output is in workallotment.xlsm 
+0

我試圖在循環和結束子之前添加「結束」。我仍然得到相同的錯誤 –

+0

在哪一行你收到錯誤信息? (另外,請注意'End With'是絕對必要的,但是您的代碼中可能還有其他錯誤。) – Sun

+0

我沒有其他錯誤。我嘗試評論「with」代碼並執行它正在正確顯示任務但如果包含「with」代碼,則會給出RUNTIME ERROR 91:Object變量或未設置塊變量 –

回答

2

放線

End With 

Loop 

End Sub 

和您的錯誤信息應該消失。

編輯:我已經重寫了您的代碼,現在它適用於我。請注意,您必須更改一些圖紙名稱和文件路徑以適合您的工作簿。這個代碼進入Workallotment工作簿(作爲一個單獨的模塊):

Sub workallotment() 

Dim workallotmentWB, tasksWB As Workbook 
Dim waSheet As Worksheet 

Dim wa_nameRng As Range 

Dim wa_nameRow, wa_firstRow, wa_lastRow As Integer 'work allotment rows 
Dim t_firstRow, t_lastrow As Integer    'task rows 

Dim curTaskCol As Integer 'current task column 
Dim wa_tmpcol As Integer 'work allotment, temp column 


    Set workallotmentWB = ThisWorkbook 
    Set tasksWB = Workbooks.Open("C:/users/q393996/Desktop/tasks.xlsx") 

    'notes on data structure: 
    '- tasks workbook: 
     'first name starts in A1 of "Sheet1" 
    '- workallotment workbook: 
     'first name starts in A2 of Sheet named "workallotment" 
     'tasks are to be written starting in B2 
     'in Row 1 are headers (number of days) 

    t_firstRow = 1 
    wa_firstRow = 2 
    wa_nameRow = 0 

    Set waSheet = workallotmentWB.Worksheets("workallotment") 

    With tasksWB.Worksheets("Sheet1") 

     'finding the last rows 
     t_lastrow = .Range("A1000000").End(xlUp).Row + 1 
     wa_lastRow = waSheet.Range("A1000000").End(xlUp).Row 

     'goes through all the names in tasks_Sheet1 
     For r = t_firstRow To t_lastrow Step 2 

      Set wa_nameRng = waSheet.Range("A:A").Find(.Range("A" & r).Value, _ 
      LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False) 

      If Not wa_nameRng Is Nothing Then 

       wa_nameRow = wa_nameRng.Row 

       curTaskCol = 2 
       wa_tmpcol = 2 

       Do While Not IsEmpty(.Cells(r, curTaskCol).Value) 

        For c = 1 To .Cells(r + 1, curTaskCol).Value 
         waSheet.Cells(wa_nameRow, wa_tmpcol).Value = .Cells(r, curTaskCol).Value 
         wa_tmpcol = wa_tmpcol + 1 
        Next c 

        curTaskCol = curTaskCol + 1 

       Loop 

      End If 

     Next r 

    End With 

    MsgBox ("done") 

End Sub 

在一般情況下,你應該總是指定要在代碼工作在其簿和工作表。不要依賴ActiveWorkbook,ActiveCell,.Select等,因爲這些可能會產生太多的錯誤,而這些錯誤您可能還沒有意識到。首先,很難理解代碼,但更重要的是,如果用戶無意中選擇了另一個工作簿,會發生什麼? ActiveCell會在某個地方完全不同於你的意圖。

請注意代碼中的註釋。隨意問你是否有任何問題! :)

+0

我得到一個錯誤在上面的代碼行 「設置waSheet = workallotmentWB.Worksheets(」 workallotment「)」 運行時錯誤9:下標越界 –

+0

是的,在這裏你需要改變「workallotment」到你的工作表的實際名稱,你想要的結果。我剛剛在我的文件中將其命名爲工作分配 – Sun

+0

我仍然遇到運行時錯誤91:對象變量或塊變量未設置....我發現錯誤在行中:「。find(.Range(「A」&r).Value「)。因爲我搜索了個人名字,所以我沒有任何錯誤。但是在這段代碼中,我得到了運行時錯誤91.順便說一句,你的input-tasks.xls如何?發給我你使用過的excel文件,因爲即使我把個人名字也混合在輸出中的兩個元素之間,但與正確的成員混合 –

相關問題