运行时错误9使用" With Sheet"

时间:2017-12-13 19:14:58

标签: excel-vba vba excel

我提出了一个解决方案,应该绕过我的原始问题(在VBA代码下面找到,所以如果不需要额外的信息,我可以保持这个简短和甜蜜)但是我得到了运行时错误9。我在两个基本工作簿上测试了这段代码并且工作正常。 LastRow行是调试时突出显示的行。我试图在单个文件名中包围文件名,因为该名称有空格但没有解决错误。

TemplateSyntaxError at /blog/
Could not parse the remainder: '-m-d"' from 'post.date|date:Y-m-d"'
Request Method: GET
Request URL:    http://127.0.0.1:8000/blog/
Django Version: 2.0
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: '-m-d"' from 'post.date|date:Y-m-d"'
Exception Location: C:\Python34\lib\site-packages\django\template\base.py in __init__, line 668
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.3
Python Path:    
['C:\\Users\\User\\Desktop\\pythonsite\\mysite',
 'C:\\windows\\SYSTEM32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Python34\\lib\\site-packages']
Server time:    Wed, 13 Dec 2017 19:13:00 +0000
Error during template rendering

In template C:\Users\User\Desktop\pythonsite\mysite\blog\templates\blog\blog.html, error at line 5
Could not parse the remainder: '-m-d"' from 'post.date|date:Y-m-d"'
1   {% extends "aboutme/header.html" %}
2   
3   {%block content %}
4       {% for post in object_list %}
5           <h5>{{post.date|date:Y-m-d"}}<a href="/blog/{{post.id}}">{{post.title}}</a></h5>
6       {% end for %}
7   {% endblock %}

作为背景:我的主要工作簿有几个宏,必须打开并保持全屏,没有公式栏。我为工作簿激活/停用添加了一个宏,因此如果用户打开另一个Excel实例,它将正常工作。我的主要书中的一个宏会拉出另一个工作簿,触发我需要的宏,但是在某些时候它会被激活/取消激活宏捕获,从来没有实际完成没有激活/去激活宏的宏工作。所以我决定尝试添加这个宏,因为我认为这将是一个很好的解决方法。

2 个答案:

答案 0 :(得分:1)

目前你的行

LastRow = Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR").Cells(Worksheets("GSR").Rows.Count, "A").End(xlUp).Row

相当于

LastRow = Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR").Cells(ActiveWorkbook.Worksheets("GSR").Rows.Count, "A").End(xlUp).Row

所以,如果没有&#34; GSR&#34;在活动工作簿中的工作表,它将提供超出范围的&#34;下标&#34;错误。

这条线实际上应该说

LastRow = Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR").Cells(Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR").Rows.Count, "A").End(xlUp).Row

但这有点笨拙,所以如果你把它移到With区块内会更好:

With Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    '...

答案 1 :(得分:0)

lastRow来电也写在With block中,以便根据需要完全限定所有对象。

Dim LastRow As Long
With Workbooks("Reservation Activity Dashboard (RAD) CP.xlsm").Worksheets("GSR")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With