2017-01-17 22 views
0

我有一個用於從工作表中的信息來創建一個數組裏面的代碼。然後填充數組(給定一些條件),創建一個新的工作簿並將該數組轉置到工作簿。如何直接使用功能的代碼

而不是做這多個時間(每個輸出文件)中,我試圖創建一個不完全一樣的功能。問題是我不知道如何從代碼中調用這個函數(沒有分配變量)。

代碼如下:

Sub FixerAndExporter() 
Dim w As Workbook 
Dim w2 As Workbook 
Dim WSArray() As Variant, PArray() As Variant, P0Array() As Variant 
Dim lRow As Long, lColumn As Long 
Dim Pr As Integer, Pr0 As Integer 
Dim ws As Worksheet  

Set w = ThisWorkbook 

Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

For Each ws In w.Worksheets 
    If ws.Name = "Pr" Then 

     PArray = ws.UsedRange.Value 

    ElseIf ws.Name = "Pr0" Then 

     P0Array = ws.UsedRange.Value 

    End If 

Next ws 

'this is what I don't know how to do: 
'ArrayFiller(PArray, P0Array) 

'what the code is doing is this: 


    For lRow = LBound(PArray, 1) To UBound(PArray, 1) 
      For lColumn = LBound(PArray, 2) + 1 To UBound(PArray, 2) 
       If PArray(lRow, lColumn) <> "" And PArray(lRow, lColumn - 1) = "" Then 

         If P0Array(lRow, lColumn) <> "" And P0Array(lRow, lColumn) <> "--" Then 
          PArray(lRow, lColumn - 1) = P0Array(lRow, lColumn) 
          'PArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 

         ElseIf P0Array(lRow, lColumn) = "" Or P0Array(lRow, lColumn) = "--" Then 
          PArray(lRow, lColumn - 1) = PArray(lRow, lColumn) 
          'PArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 

         End If 

       End If 
      Next 
    Next 


Workbooks.Add 

Set w2 = ActiveWorkbook 
w2.Sheets("Sheet1").Range("A1").Resize(UBound(PArray, 2), UBound(PArray, 1)) = Application.WorksheetFunction.Transpose(PArray()) 

w2.SaveAs Filename:=ThisWorkbook.path & "\POutput", FileFormat:=6 


    w2.Close True 


End Sub 

這是函數:

Function ArrayFiller(arr As Variant, arr0 As Variant) As Variant 
Dim lRow As Long, lColumn As Long 
Dim w2 As Workbook 

Workbooks.Add 

    For lRow = LBound(arr, 1) To UBound(arr, 1) 
     For lColumn = LBound(arr, 2) + 1 To UBound(arr, 2) 
      If arr(lRow, lColumn) <> "" And arr(lRow, lColumn - 1) = "" Then 

        If arr0(lRow, lColumn) <> "" And arr0(lRow, lColumn) <> "--" Then 
         arr(lRow, lColumn - 1) = arr0(lRow, lColumn) 
          'PriceArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 

        ElseIf arr0(lRow, lColumn) = "" Or arr0(lRow, lColumn) = "--" Then 
         arr(lRow, lColumn - 1) = arr(lRow, lColumn) 
          'PriceArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 
        End If 
      End If 
     Next 
    Next 

Set w2 = ActiveWorkbook 

w2.Sheets("Sheet1").Range("A1").Resize(UBound(PriceArray, 2), UBound(PriceArray, 1)) = Application.WorksheetFunction.Transpose(PriceArray()) 

w2.SaveAs Filename:=ThisWorkbook.path & "\PriceOutput.xls", FileFormat:=6 

w2.Close True 

Set w = ActiveWorkbook 

End Function 

代碼已經工作。我的疑問是如何直接使用功能,所以我不必一遍遍寫的代碼塊爲每個新的不同的項目,我需要(有多個)。

有什麼建議嗎?

+0

要調用'Function'你只需要使用你把作爲註釋的確切行:那麼你將有'ArrayFiller(粒子陣列,P0Array)',然後一旦函數執行了它的行,它就會回到原來的Sub。林不知道我理解的問題完全 –

+0

[刪除括號:(http://stackoverflow.com/documentation/vba/1179/procedure-calls/3818/this-is-confusing-why-not-just-always-使用括號)'ArrayFiller PArray,P0Array'。你也有'.Transpose(PriceArray())'的語法錯誤。除去那裏的圓括號(並將數組聲明爲變量或將名稱更改爲實際應該使用的名稱)。 – Comintern

+0

@Comintern那就像魔術一樣工作。非常感謝。謹慎地寫它作爲答案,所以我可以標記它? – DGMS89

回答

1

您應該使用Option Explicit(每個模塊的開始)!

因爲你寫的功能,你會輸出什麼爲PriceArray沒有定義,也沒有填寫!


與你所寫的內容,功能是沒有用的,你不輸出任何東西,你可以只使用一個子帶參數。

Sub FixerAndExporter() 
Dim w As Workbook 
Dim WSArray() As Variant, PArray() As Variant, P0Array() As Variant 
Dim lRow As Long, lColumn As Long 
Dim Pr As Integer, Pr0 As Integer 
Dim ws As Worksheet 

Set w = ThisWorkbook 

Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

For Each ws In w.Worksheets 
    If ws.Name = "Pr" Then 
     PArray = ws.UsedRange.Value 
    ElseIf ws.Name = "Pr0" Then 
     P0Array = ws.UsedRange.Value 
    End If 
Next ws 

Dim PathToOutputFile As String 
PathToOutputFile = ArrayFiller(PArray, P0Array) 
MsgBox PathToOutputFile 


End Sub 

而且功能(帶有輸出)

Function ArrayFiller(arr As Variant, arr0 As Variant) As String 
    Dim lRow As Long, lColumn As Long 
    Dim w2 As Workbook 
    Dim TempStr As String 

    For lRow = LBound(arr, 1) To UBound(arr, 1) 
     For lColumn = LBound(arr, 2) + 1 To UBound(arr, 2) 
      If arr(lRow, lColumn) <> "" And arr(lRow, lColumn - 1) = "" Then 

        If arr0(lRow, lColumn) <> "" And arr0(lRow, lColumn) <> "--" Then 
         arr(lRow, lColumn - 1) = arr0(lRow, lColumn) 
          'PriceArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 

        ElseIf arr0(lRow, lColumn) = "" Or arr0(lRow, lColumn) = "--" Then 
         arr(lRow, lColumn - 1) = arr(lRow, lColumn) 
          'PriceArray(lRow, lColumn - 1).Interior.Color = RGB(255, 0, 0) 
        End If 
      End If 
     Next lColumn 
    Next lRow 

    TempStr = ThisWorkbook.Path & "\PriceOutput.xls" 

    Set w2 = Workbooks.Add 
    With w2 
     .Sheets(1).Range("A1").Resize(UBound(arr, 2), UBound(arr, 1)) = Application.WorksheetFunction.Transpose(arr()) 
     .SaveAs Filename:=TempStr, FileFormat:=6 
     .Close True 
    End With 'w2 
    Set w2 = Nothing 

ArrayFiller = TempStr 
End Function 
+0

感謝您的回答。對於PriceArray抱歉,當我將代碼轉移到這篇文章時,這是一個錯字。它應該只是拋磚引玉。除此之外,你的代碼工作得很好。 – DGMS89

相關問題