Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-运行时错误';1004';_Vba_Excel - Fatal编程技术网

VBA-运行时错误';1004';

VBA-运行时错误';1004';,vba,excel,Vba,Excel,有点背景,这是我第一次在excel中使用VBA编程, 我正试图创建一个表格来填写电子表格 我当前收到错误消息:“运行时错误'1004':对象'\u Global'的方法'Worksheets'失败 我在网上搜索过,并尝试过各种解决方案,但我认为基本上归结为我自己缺乏理解 Private Sub CommandButton1_Click() 'When pressing save, save values in spreedsheet locations ActiveSheet.range("c8

有点背景,这是我第一次在excel中使用VBA编程, 我正试图创建一个表格来填写电子表格

我当前收到错误消息:“运行时错误'1004':对象'\u Global'的方法'Worksheets'失败

我在网上搜索过,并尝试过各种解决方案,但我认为基本上归结为我自己缺乏理解

Private Sub CommandButton1_Click()
'When pressing save, save values in spreedsheet locations
ActiveSheet.range("c8").Value = ContactName.Value
ActiveSheet.range("b19").Value = ModelNumber.Value
ActiveSheet.range("d19").Value = SerialNumber.Value
ActiveSheet.range("g19").Value = IncidentNumber.Value
ActiveSheet.range("j19").Value = Description.Value
ActiveSheet.range("c7").Value = PortLocation.Value

'save file
ActiveWorkbook.SaveAs Filename:= _
  "D:\Users\611281\Downloads\Zebra\EmailMeToZebra.xlsx", FileFormat:=xlOpenXMLWorkbook, ReadOnlyRecommended:=False, CreateBackup:=False

End 'after pressing save, close down sheet.

End Sub


Private Sub UserForm_Initialize()

Me.PortLocation.List = Worksheets("Data lookup_ports").range("e3:e200").Value

Dim MyTempWkBk As Workbook
Dim MyCurrentWin As Window

Set MyCurrentWin = ActiveWindow
Set MyTempWkBk = Workbooks.Open("D:\Users\611281\Downloads\Zebra\GUI.xlsm")
MyCurrentWin.Activate      'Allows only a VERY brief flash of the opened workbook
MyTempWkBk.Windows.Visible = False 'Only necessary if you also need to prevent
                                    'the user from manually accessing the opened
                                    'workbook before it is closed.

'Operate on the new workbook, which is not visible to the user, then close it...

End Sub

Private Sub UserForm_Terminate()
    End 'when pressing x, close down window, do not save.
End Sub
我得到了代码上的错误:

Me.PortLocation.List = Worksheets("Data lookup_ports").range("e3:e200").Value

这只是我试图从电子表格区域填充列表框的情况。是否尝试命名工作簿?这是假设出现错误的行引用的是此工作簿中的单元格区域。此外,请确保检查所引用工作表的名称是否正确拼写(如果拼写正确,还要检查空白。)此外,工作表是否隐藏?如果是,则可能需要在调用工作表之前添加此项

wb.sheets("Data lookup_ports").Visible = True  
可以同时尝试下面的编辑

Private Sub UserForm_Initialize()

Dim MyTempWkBk As Workbook
Dim MyCurrentWin As Window
Dim WB as Workbook
   Set WB = ThisWorkBook
wb.sheets("Data lookup_ports").Visible = True 

Me.PortLocation.List = WB.Sheets("Data lookup_ports").Range("E3:E200").Value



Set MyCurrentWin = ActiveWindow
Set MyTempWkBk = Workbooks.Open("D:\Users\611281\Downloads\Zebra\GUI.xlsm")
MyCurrentWin.Activate   
MyTempWkBk.Windows.Visible = False 'Only necessary if you also need to prevent
                                'the user from manually accessing the opened
                                'workbook before it is closed.

'Operate on the new workbook, which is not visible to the user, then close it...

End Sub

Private Sub UserForm_Terminate()
    End 'when pressing x, close down window, do not save.
End Sub

工作表(“数据查找端口”)
应该是
工作表(“数据查找端口”)
还是
工作表(“数据查找端口”)
相反?错误表明工作表名称不正确。这解决了问题,您认为工作表不可见的想法是正确的,解决了问题。尽管现在我遇到了一个与CurrentWin无关的问题。激活它表示需要对象,但至少需要它的进度!再次感谢。您想做什么如何处理currentwin.activate?如果这是您的目标,只需调用工作表即可激活。目前,我尝试打开VBA脚本而不是电子表格的尝试失败。以前工作过,但后来已中断。正如我所说,VBA没有问题。是的,我应该可以接受,更大的问题是您解决的问题。