分配给button时,我的VBA行为会有所不同

我build立了质量检查表,需要将结果作为一行数据存储在名为“数据”的单独表格中,并将完整检查表的存档版本保存在单独的工作簿中。 我是VBA的新手,但已经设法得到我需要的工作。 当我将macros指定给包含在我的检查表上的button时,我的问题就出现了。 如果我按下button,它将复制错误的表单,并且基本上不会执行手动运行macros时的操作。 任何人都可以提出什么build议

谢谢

我的代码如下:

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 

我认为你的问题是由于你正在引用错误的工作表。 当你使用不同的纸张时,确保你总是完全符合条件。

我会开始与下

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

然后你可以改变所有的范围ws.range("A1")

这样他们将始终引用正确的工作表上的范围。

我将首先检查你的代码,并确保对范围的每一个引用都在工作表上引用一个工作表和一个范围。

希望能帮助到你!

你的问题是这行代码

 Range("M14:BP14").Copy 

你需要明确说明你所指的是什么表。 像ie

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