简体   繁体   中英

Run Time Error 9 Subscript out of Range

I'm a bit of a noob to VB for Excel, but I'm hoping to get a lot better in the next few months. Tons of ways it can make my team's job easier. Can someone please help me figure out how to get this macro to run? I've already worked through a few issues but any expertise is welcome.

I'm trying to create a form that people can fill out with Census Data, and then I'll have formulas running off this data in another sheet.

I'm currently getting the highlighted error on the part "Sheets(strName).Select"

Sub InputSheet()

Dim sName As String
Dim sStatus As String
Dim sSalary As String
Dim sBonus As String
Dim sPartner As String
Dim sWorkState As String
Dim sBenefitsLevel As String

sName = InputBox("Input Employee Name:", "PROJECT INFORMATION")
sStatus = InputBox("Input FT or PT:", "PROJECT INFORMATION")
sSalary = InputBox("Input Salary:", "PROJECT INFORMATION")
sBonus = InputBox("Input Bonus:", "PROJECT INFORMATION")
sPartner = InputBox("Input Partner Status (Y/N):", "PROJECT INFORMATION")
sWorkState = InputBox("Input Work State:", "PROJECT INFORMATION")
sBenefitsLevel = InputBox("Input Benefits Level:", "PROJECT INFORMATION")


Dim MyNewSheet As Worksheet
Set MyNewSheet = Worksheets.Add(, Sheets(Sheets.Count))
Dim strName As String
strName = "Project Data "

Sheets(strName).Select
Cells(1, 1).Value = "Employee Name or Title"
Cells(1, 2).Value = "Employment Status"
Cells(1, 3).Value = "Salary"

Cells(1, 4).Value = "Bonus"
Cells(1, 5).Value = "Partner Status"
Cells(1, 6).Value = "Work State"
Cells(1, 7).Value = "Benefits Level"
Cells(2, 1).Value = sName
Cells(2, 2).Value = sStatus
Cells(2, 3).Value = sSalary
Cells(2, 4).Value = sBonus
Cells(2, 5).Value = sPartner
Cells(2, 6).Value = sWorkState
Cells(2, 7).Value = sBenefitsLevel
ThisWorkbook.Save

MsgBox "Process is Complete!"

End Sub

您在strName的末尾有一个空格-选项卡是“ Project Data”还是“ Project Data”?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM