2016-06-20 52 views
0

我是vba的新手,我創建了一個用戶窗體並處理與「好」按鈕相關的代碼。我想檢查用戶窗體中的空字段,然後轉到另一個工作表,選擇頂部單元格(B3),然後填充下面空白單元格中的數據(B4),沿着行移動,直到列O.我希望它每次有人填寫用戶表單時都要做同樣的事情,並點擊「好」。Activecell.end.offset問題vba

然而,有一個錯誤消息突出線

工作表( 「Data_raw」)。選擇 範圍( 「B3」)。選擇

。我試圖用另一個文件,它是工作,我不知道爲什麼它會顯示爲這個文件:(錯誤。

誰能幫助我弄清楚什麼是錯我的代碼,請?.

這裏是我的代碼:

Private Sub CmdOKAY_Click() 


    'check all the fields to see if they are empty 


    If BoxRM = "" Then 
       MsgBox "Select a Relationship Manager" 

      End If 

    If TextCompany_name = "" Then 
       MsgBox " Enter a company name" 

      End If 


    If BoxCountry = "" Then 
       MsgBox "Select a country" 

      End If 

    If BoxTransactions = "" Then 
       MsgBox "Enter a volume for the transactions" 

      End If 

    If Btn_Yearly_Transactions.Value = False And Btn_Monthly_Transactions.Value = False Then 
       MsgBox "Select a frequency for the volume of transactions inputted" 

      End If 

    If BoxPosition_Vol = "" Then 
       MsgBox "Enter a volume for the positions" 

      End If 

    If Btn_Yearly_Positions.Value = False And Btn_Monthly_Positions.Value = False Then 
       MsgBox "Select a frequency for the volume of positions inputted" 

      End If 

    If CheckBox_RP.Value = False And CheckBox_TP.Value = False And CheckBox_TE.Value = False Then 
       MsgBox " Select at least one type of account" 

      End If 

    Worksheets("Data_raw").Range("b3").Select 

    ActiveCell.End(xlDown).Offset(1, 0).Select 
    ActiveCell.Value = RM 


    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = NameCo 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = Pays 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = RepTrd 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = FreqYrTrd 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = OP 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = FreqYrOP 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = AvrgDays 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = AcRP 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = AcTP 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = AcTE 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = RP_Per 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = TP_Per 

    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Value = TE_Per 


    'copy the formats from the row above 
    Range("b4:o4").Copy 
    ActiveCell.Offset(0, -12).PasteSpecial xlPasteFormats 
     Application.CutCopyMode = False 

    Unload Me 
End sub 
+0

您必須確保表格已被激活,然後才能在其上選擇範圍。不是你需要在這裏選擇它... – Rory

回答

0

在該行之前只需添加1行:

Worksheets("Data_raw").Activate 
Worksheets("Data_raw").Range("b3").Select 
+0

謝謝,但一旦我已經完成了以下行ActiveCell.End(xlDown).Offset(1,0).Select突出顯示 – Izuma23

0

您可以直接與物體通過重構你的C工作,使你的代碼更短,更易於閱讀和調試像下面的內容一樣。

我認爲值得指出的另一件事是,如果您打算在代碼不能粘貼任何數據,如果一個或任何檢查失敗,您需要在每個If塊中添加Exit Sub。否則,它將繼續並粘貼每次單擊該按鈕時可用的內容。

Dim wsRawData as Worksheet 
Set wsRawData = Worksheets("Data_raw") 

With wsRawData 

    Dim lNextRow as Long 
    lNextRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).Row 

    .Cells(lNextRow, 2).Value = RM 
    .Cells(lNextRow, 3).Value = NameCo 
    .Cells(lNextRow, 4).Value = Pays 

    '... and so on 

End With 
+1

感謝您的輸入!!!!!它工作得很好!感謝Scott :)) – Izuma23