我写了一个vba代码,弹出一个输入框然后打印一个范围。如果用户按下“取消”键,我想停止代码。按钮。截至目前,无论用户操作如何,它都会打印出来并浪费大量纸张。这是代码:
Sub Form()
ThisWorkbook.Sheets("FAQs").Activate
ActiveSheet.Range("A1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo1")
ThisWorkbook.Sheets("FAQs").Activate
ActiveSheet.Range("B1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo2")
ThisWorkbook.Sheets("FAQs").Activate
ActiveSheet.Range("C1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo3")
ThisWorkbook.Sheets("FAQs").Activate
ActiveSheet.Range("D1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo4")
ThisWorkbook.Sheets("FAQs").Activate
ActiveSheet.Range("E1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo5")
Columns("A:D").EntireColumn.Hidden = False
Range("A1048308:B1048359").PrintOut
Columns("A:D").EntireColumn.Hidden = True
ActiveSheet.Range("A1").Select
ThisWorkbook.Sheets("Spends Tracker").Activate
ActiveSheet.Range("A1").Select
End Sub
编辑:
这是我的新代码不会更新单元格A1048306 ,但仍继续使用vba并更新单元格B1048306,C1048306,D1048306,E1048306并打印范围。
Sub Form()
Dim strVale As String
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("FAQs")
strVale = InputBox("SrNo1")
If strVale = vbNullString Then
MsgBox ("User canceled!")
ThisWorkbook.Sheets("Spends Tracker").Activate
ActiveSheet.Range("A1").Select
Exit Sub
Else
'Here we can use the objects range to set the formula.
ws.Range("A1048306").FormulaR1C1 = strValue
End If
ws.Range("B1048306").FormulaR1C1 = InputBox("SrNo2")
ws.Range("C1048306").FormulaR1C1 = InputBox("SrNo3")
ws.Range("D1048306").FormulaR1C1 = InputBox("SrNo4")
ws.Range("E1048306").FormulaR1C1 = InputBox("SrNo5")
ws.Columns("A:D").EntireColumn.Hidden = False
ws.Range("A1048308:B1048359").PrintOut
ws.Columns("A:D").EntireColumn.Hidden = True
ActiveSheet.Range("A1").Select
ThisWorkbook.Sheets("Spends Tracker").Activate
ActiveSheet.Range("A1").Select
End Sub
答案 0 :(得分:1)
评估返回值,而不是将其放入单元格公式中。
更改
ActiveCell.FormulaR1C1 = InputBox("SrNo1")
这样的事情
Dim strVale as string
strVale = InputBox("SrNo1")
If strVale = vbNullString Then
MsgBox ("User canceled!")
Exit Sub
Else
ActiveCell.FormulaR1C1 = strValue
End If
将其应用于每个InputBox。
编辑:第二个问题
我改变了你的代码。试一试。
选择和激活会导致事情变得混乱。看这里。 How to avoid using Select in Excel VBA macros
最好避免它们。
我取出了所有ThisWorkbook.Sheets("FAQs").Activate
并宣布了该表的对象。然后你可以使用这个对象。
Option Explicit
Sub Form()
Dim strValue As String
'Here we are declaring and setting the object = to your FAQs worksheet
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("FAQs")
strValue = InputBox("SrNo1")
If strValue = vbNullString Then
MsgBox ("User canceled!")
ThisWorkbook.Sheets("Spends Tracker").Activate
ActiveSheet.Range("A1").Select
Exit Sub
Else
'Here we can use the objects range to set the formula.
ws.Range("A1048306").FormulaR1C1 = strValue
End If
ActiveSheet.Range("B1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo2")
ActiveSheet.Range("C1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo3")
ActiveSheet.Range("D1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo4")
ActiveSheet.Range("E1048306").Select
ActiveCell.FormulaR1C1 = InputBox("SrNo5")
Columns("A:D").EntireColumn.Hidden = False
Range("A1048308:B1048359").PrintOut
Columns("A:D").EntireColumn.Hidden = True
ActiveSheet.Range("A1").Select
ThisWorkbook.Sheets("Spends Tracker").Activate
ActiveSheet.Range("A1").Select
End Sub
主要变化是
ActiveCell.FormulaR1C1 = strValue
成了
ws.Range("A1048306").FormulaR1C1 = strValue
这样您就不必担心活动或选定的单元格是什么。
答案 1 :(得分:0)
这是你的代码,收紧了。我建议阅读avoiding using .Select
,这有助于提高可读性和速度。
Sub Form()
Dim srOkCancel$
Dim i&
With ThisWorkbook.Sheets("FAQs")
For i = 1 To 5
srOkCancel = InputBox("SrNo" & i, vbOKCancel)
If srOkCancel = vbNullString Then Exit Sub
.Range(.Cells(1048306, .Columns(i)), .Cells(1048306, .Columns(i))).FormulaR1C1 = srOkCancel
Next i
.Columns("A:D").EntireColumn.Hidden = False
.Range("A1048308:B1048359").PrintOut
.Columns("A:D").EntireColumn.Hidden = True
.Range("A1").Select
End With
ThisWorkbook.Sheets("Spends Tracker").Range("A1").Select
End Sub
唯一的问题是,我不知道你期望作为一个输入 - 它会是一个公式吗?如果你使用" = r1c1 + 1"那么这不会真的有效。作为输入字符串。请告诉我您希望输入的数据类型,我会修复它。