为什么我的代码有运行时错误? [英] Why do I have a runtime error on my code?

查看:128
本文介绍了为什么我的代码有运行时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的代码时,我遇到运行时错误

When I'm running my code I have a runtime error on

Sheets2.UsedRange.Columns.AutoFit



有人可以告诉我为什么会这样吗?

这个ActiveX按钮单击的原因是选择工作表2中的所有单元格,自动调整列并选择特定的列来更改它的字体。

先谢谢。



我的尝试:




could someone tell me why please?
The reason of this ActiveX button click is to select all cells in sheet 2, autofit the columns and select particular columns to change the font of it.
Thanks in advance.

What I have tried:

Private Sub CommandButton3_Click()

    Worksheets(2).Activate
    Worksheets(2).Range("A1:AZ1000").Select
    Sheets2.UsedRange.Columns.AutoFit
    'Sheet2.Range("A1").Select
    Range("Table1[[#Headers],[ns1:Identification]]").Select
    Selection.AutoFilter
    Range("E:E,G:G,I:I,K:K").Select
    Range("Table1[[#Headers],[ns1:Identification7]]").Activate
    ActiveWindow.SmallScroll ToRight:=29
    Range("E:E,G:G,I:I,K:K,AG:AG,AI:AI").Select
    Range("Table1[[#Headers],[ns1:Identification20]]").Activate
    ActiveWindow.SmallScroll ToRight:=6
    Range("E:E,G:G,I:I,K:K,AG:AG,AI:AI,AM:AM").Select
    Range("Table1[[#Headers],[ns1:MeterIdentification]]").Activate
    Selection.NumberFormat = "0.00"
End Sub

推荐答案

您确定 Sheets2 是否存在?



我很确定你必须用以下代码替换: Sheet s 2 Sheet2 。你可以看到 s 是多余的。



我建议阅读这个提示/技巧: 使用VBA在Excel表格之间复制数据 [ ^ ]继续你的VBA之旅。我提供了一些错误(通常称为不良做法)以及解决问题的方法。
Are you sure that Sheets2 exists?

I'm pretty sure you have to replace: Sheets2 with: Sheet2. As you can see "s" is redundant.

I'd suggest to read this tip/trick: Copy Data Between Excel Sheets using VBA[^] before you start continue your VBA journey. I provided there few mistakes (commonly named as "bad practices") and the way to resolve them.


这篇关于为什么我的代码有运行时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆