2013-02-18 65 views
0

请允许我先解释该方案...是否可以自动填充输入框?

我们有一个报告电子表格,可以更新每月数据以进行多种不同的度量。

随着时间的推移,越来越多的宏已被添加,目前总数超过20

,使工作更容易一些我已经添加了另一个宏,它会弹出一个用户表单

电话每个其他宏一个接一个,并显示一个进度条来指示完成了多少个任务(宏)。

第8个宏呼吁提示加上一个输入框为其月份被更新 - 这将永远是在所有8

SO同月,我想要做的就是添加一个全球性的输入框中作为用户窗体的第一件事,然后在其他宏中引用此输入(已删除其各自的提示)。

说实话,我绝对不知道如何做到这一点,但已经尝试了以下(全部一起)。

在工作簿

Public Monthglobal As Variant 

在在用户窗体子,其通过一个调用宏之一的开始的用户窗体代码

Function GetMonth() 
Monthglobal = InputBox("Please enter the 3 letter abbreviation for the Month which your are updating (e.g. Jan, Feb...)", "Month") 
If strName = vbNullString Then Exit Function 
End Function 

开始

GetMonth 

在每一个8个宏(包含在模块1)

'Searches for correct column for month and pastes data 

Selection.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlFormulas, _ 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False).Activate 
ActiveCell.Offset(1, 0).Activate 
ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

'Searches for correct column for month and pastes data 

结果

运行时错误 '91': 对象变量或未设置块变量

的错误返回与搜索(对于变量)节中强调:

Selection.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlFormulas, _ 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False).Activate 

我希望这就是足以令它的意义,如果有人需要看到更多的代码为个人宏请给我一个呼喊,但我认为错误信息清楚地表明问题在哪里......只是不是我的经验不足的人!

由于提前,

+0

你能解释'GetMonth()'中的If strName = vbNullString'部分吗? – barrowc 2013-02-18 14:12:13

回答

0

我会做这样的事情:

Public Monthglobal As String 

Function GetMonth() 
    Monthglobal = InputBox("Please enter the 3 letter abbreviation for the Month which your are updating (e.g. Jan, Feb...)", "Month") 
End Function 

搜索位 - 这是更好地定义编程,通过搜索什么范围,而不是使用“选择”。 3个字母的月份是整个单元格的内容还是其中的一部分?

Dim rngFind as Range, rngFound as Range 

Set rngFind = Range("A:A") ' Set this to whatever 'Selection' is currently 

'Now set the rngFound variable to be the eventual 'ActiveCell' from your macro above (found cell, offset(1,0)) 
Set rngFound = rngFind.Find(What:=Monthglobal, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,MatchCase:=False).Offset(1, 0) 

'Now paste without activating anything: 
rngFind.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False