2017-07-21 50 views
0

我已經設置了質量檢查表,它需要將結果作爲一行數據存儲在名爲「數據」的單獨表單中,並將完整檢查表的存檔版本保存在一個單獨的工作簿。 我是VBA的新手,但已經設法得到我需要的工作。當我將宏指定給包含在我的檢查表上的按鈕時,我的問題就出現了。如果我按下按鈕,它會複製錯誤的表單,並且基本上不會執行手動運行宏時的操作。任何人都可以提出建議嗎?分配給按鈕時,我的VBA行爲會有所不同

感謝

我的代碼如下:

Sub SaveForm() 

' SaveForm Macro 
' Saves form data to the Data Sheet 

'Checks for completion of mandatory fields 

If IsEmpty(Range("b3").Value) = True Then 
    MsgBox "Please complete 'Agent Name' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("b4").Value) = True Then 
    MsgBox "Please complete 'Call ID' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("b5").Value) = True Then 
    MsgBox "Please complete 'Call Length' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("D3").Value) = True Then 
    MsgBox "Please complete 'Business Name' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("D4").Value) = True Then 
    MsgBox "Please complete 'Date of Call' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("D5").Value) = True Then 
    MsgBox "Please complete 'Time of Call' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("b7").Value) = True Then 
    MsgBox "Please complete 'Assessor Name' before saving" 
    Exit Sub 

ElseIf IsEmpty(Range("b8").Value) = True Then 
    MsgBox "Please complete 'Date of Assessment' before saving" 
    Exit Sub 


End If 

'Copies a range contained within the "Checksheet" and pastes 
'it into the next available row on the "Data" sheet 

'The reason it is in a straight row as opposed to sporadic cell 
'references is because I have set the sheet up this way for simplicity 

Range("M14:BP14").Copy 

Sheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues 
ActiveWindow.ScrollRow = 1 
Workbooks("Call Feedback Form V0.42.xlsm").Sheets("Checksheet").Activate 

Call CopyRenameWorksheet 

Workbooks("Call Feedback Form V0.42.xlsm").Sheets("Checksheet").Activate 

End Sub 

Sub CopyRenameWorksheet() 

'This renames the worksheet based on cell references and archives to another workbook 

Dim ws As Worksheet 
Set wh = Worksheets(ActiveSheet.Name) 
ActiveSheet.Copy After:=Worksheets(Sheets.Count) 
If wh.Range("B3").Value <> "" Then 
ActiveSheet.Name = wh.Range("B3").Value & " " & Format(wh.Range("D4").Value, ("yymmdd")) & " " & wh.Range("B4").Value 
ActiveSheet.Move After:=Workbooks(_ 
"Archived Quality Forms.xlsx").Sheets(1) 

End If 
+0

您的一些代碼格式不正確(不是灰色框),但我不確定它應該如何。另外,請嘗試提供[MCVE](https://stackoverflow.com/help/mcve)。 – Marein

+0

嗨馬林。對不起,我對此完全陌生。 – RWGFloyd

+0

我不確定如何在灰色框中獲取所有內容,網站通過這種方式進行格式化。有沒有辦法調整它? – RWGFloyd

回答

1

我認爲你的問題是由於你引用了錯誤的工作表。當你使用不同的紙張時,確保你總是完全符合資格。

我會

dim ws as worksheet 
set ws = Worksheets("Sheetname") 

啓動替補,然後你可以改變所有的範圍要像ws.range("A1")

這樣,他們總是引用在正確的紙張範圍。

我會先檢查您的代碼,並確保對範圍的每個引用都引用工作表和工作表上的範圍。

希望它有幫助!

+0

嗨,對不起,我應該真的學會傾聽。這工作完美!非常感謝你的幫助。 – RWGFloyd

+0

沒問題,如果您對答案感到滿意,您能將此標記爲正確答案嗎? @RWGFloyd – wrslphil

0

你的問題是這行代碼

Range("M14:BP14").Copy 

你需要明確說明哪些表參照的youre太。就像

ThisWorkbook.Sheets("Sheet1").Range("M14:BP14").Copy 
+0

謝謝,但這部分似乎工作正常。問題是,一旦它將此範圍複製到數據表中,它就會將整個數據表複製到存檔工作簿。陌生人仍然重命名它也沒有任何錯誤。當我單步執行代碼或手動運行代碼時,根本沒有問題。這只是通過按鈕啓動時的一個問題。 – RWGFloyd

+0

wrsphil的評論是正確的。這個問題仍然是錯誤的命名參考 –

相關問題