首先,您可能想要编辑您的问题,因为这些问题非常不同:
What is your maiden named
What is your maiden name?
我对你的代码做了一些修改。这些评论应该可以帮助您了解正在发生的事情。使用这种方法,您可以提出问题,而无需选择或显示包含所有答案的工作表。
我已将您的硬编码行替换为一个变量,该变量设置为ws
对象 A 列中的第一个空行。您可以设置ws
为您的工作表被调用的任何内容。现在您可以根据需要多次运行它,它总是将新答案附加到新行。
' use this statement at the top of all modules to require variable declaration
Option Explicit
Sub dform()
' declare your variables
Dim wb As Workbook
Dim ws As Worksheet
Dim firstEmptyRow As Long
Dim mName As String
Dim x As Long
' you need the "set" keyword for object variables
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
' the best way to get the last row is to go up from the bottom of the sheet
' add 1 to get the first empty row
firstEmptyRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row + 1
mName = InputBox("What is your maiden named", "Maiden Name")
' always avoid selecting or activating in VBA code
ws.Range("A" & firstEmptyRow).Value = mName
x = MsgBox("Are you still married?", 4)
If x = 6 Then ws.Range("G" & firstEmptyRow).Value = "Yes"
If x = 7 Then ws.Range("G" & firstEmptyRow).Value = "No"
Exit Sub
End Sub