2015-04-02 99 views
0

如果我以BeforeRightClick的形式運行它,我有這個子/宏。不過,我想改變它,所以我可以實際使用右鍵並將宏放在按鈕上。

所以我試圖改變BeforeRightClick的名字。
我已經嘗試使用正常窗體按鈕和ActiveX。
這一切+更多的代碼是根據工作表Sheet1,而不是模塊貼

在按鈕下運行子程序 -

Dim tabA As Variant, tabM As Variant 
Dim adrA As String, adrM As String 

' Set columns (MDS tabel) where data should be copied to (APFtabel) 
       'Post to 
       'P1-6 divisions  ' Name adress, etc 
Const APFtabel = "P1;P2;P3;P4;P5;P6;E9;E10;E13;E14;E23;N9;N10;N11;N12;N20" 
       'Load data from 
Const MDStabel = "N;O;P;Q;R;S;H;Y;Z;AB;W;AF;T;D;AA;V;" 
Dim APF As Workbook 
' APFilNavn is the name of the AP form 
Const APFilNavn = "APForm_macro_pdf - test.xlsm" 
' Const APFsti As String = ActiveWorkbook.Path 
Const APFarkNavn = "Disposition of new supplier" 
' APsti is the path of the folder 
Dim sysXls As Object, APFSti As String 

Dim ræk As Integer 

Private Sub CommandButton1_Click() 
APFormRun 
End Sub 

' Here I changed it from BeforeRightClick 
Private Sub APFormRun(ByVal Target As Range, Cancel As Boolean) 
Dim cc As Object 
If Target.Column = 8 Then 
APFSti = ActiveWorkbook.Path & "\" 
    If Target.Address <> "" Then 
     For Each cc In Selection.Rows 
      Cancel = True 
      ræk = cc.Row 
      Set sysXls = ActiveWorkbook 
      åbnAPF 
      overførData 
      opretFiler 

      APF.Save 
      APF.Close 
      Set APF = Nothing 

      Set sysXls = Nothing 
     Next cc 
    End If 
End If 
End Sub 

Private Sub overførData() 
Dim ix As Integer 
    tabA = Split(APFtabel, ";") 
    tabM = Split(MDStabel, ";") 

    Application.ScreenUpdating = False 

    For ix = 0 To UBound(tabM) - 1 
     If Trim(tabM(ix)) <> "" Then 
      adrM = tabM(ix) & ræk 

      If tabA(ix) <> "" Then 
       adrA = tabA(ix) 
      End If 

      With APF.ActiveSheet 
       .Range(adrA).Value = sysXls.Sheets(1).Range(adrM).Value 
      End With 
     End If 
    Next ix 
End Sub 

Private Sub opretFiler() 
' Here I run some other macro exporting the files to Excel and PDF 
    btnExcel 
    btnExportPDF 
End Sub 

回答

0

,如果你在Sheet1把這段代碼,然後從你需要定義它的名字(在按鈕一個按鈕訪問)作爲Sheet1.APFormRun(我認爲你需要使它公開)。

如果您將子對象及其調用的所有內容移動到某個模塊(在執行Insert-> Module之後),則不需要Excel對象名稱前綴。

關於範圍界定的非常詳細的介紹在下面的鏈接。向下滾動到「在適當的模塊中放置宏/子程序」部分:http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=162:excel-vba-calling-sub-procedures-a-functions-placement-in-modules&catid=79&Itemid=475

在上面的代碼中,我必須註釋掉所有未包含的子集,以便爲調試進行編譯。

  1. 要使子訪問宏按鈕或「指定宏...」你必須讓公衆
  2. 還要使子訪問,也不能有任何傳遞的參數。
  3. 所以,你將不得不從公用Sub APFormRun()定義中刪除傳遞的參數
  4. 因此你將不得不重新編寫APFormRun的初始部分...目前您APFormRun依賴於得到一個傳遞的參數(目標)所選單元格的右鍵單擊。當你按下按鈕時,你沒有右鍵點擊單元格。這不是一個細胞識別Excel事件。您將不得不通過Selection excel對象獲取選定的單元格。有很多關於如何做到這一點的StackOverflow答案。
+0

嗨@cybermike謝謝:)我試着用Sheet1.APFormRun,我試圖把它全部移到一個模塊。這兩個都不工作:(如果我嘗試使用'Sheet1.APForm',我得到'方法或數據成員未找到' – Niclas 2015-04-02 13:43:18

+0

我無法調試您的代碼,因爲其中間存在亂碼:åbnAPFoverførDataopretFiler。您的代碼不會「編譯「在我的VBA for Excel中,爲了從一個按鈕中調用一個子按鈕,最好將宏名稱放入按鈕的定義中(右鍵點擊按鈕並選擇」Assign Macro ...「。使用CommandButton1_Click()工程但是在klunky中,同樣在CommandButton1_Click()中,你試圖調用不帶參數的APFormRun,但是子需要參數,你需要清理你的代碼,並且明確地執行一些Debug-> Compile,直到所有的錯誤都被解決。 – cybermike 2015-04-02 13:53:09

+0

我感謝你的努力,但是我不明白我應該如何清理它,它使用rightclick函數完美工作,我希望它在buttonclick上運行,如果我將整個代碼複製到模塊中,我不能在t下找到任何宏他開發人員選項卡/分配宏。我嘗試了公開,私人和沒有。我很困惑。 – Niclas 2015-04-02 14:02:35