分配给按钮

时间:2017-07-21 14:06:25

标签: excel vba excel-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

2 个答案:

答案 0 :(得分:1)

我认为您的问题是由于您引用了错误的表单。当您使用不同的床单时,请确保您始终完全符合资格。

我会用

启动Subs
dim ws as worksheet
set ws = Worksheets("Sheetname")

然后您可以将所有范围更改为ws.range("A1")

这样他们总是会在正确的工作表上引用范围。

我首先介绍一下你的代码并确保对一个范围的每一个引用都引用了工作表和工作表上的一个范围。

希望它有所帮助!

答案 1 :(得分:0)

你的问题在于这行代码

Range("M14:BP14").Copy

您需要明确说明您所指的是哪张纸。比如即。

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