我正在尝试编写一个宏,它会检查所有工作表名称是否符合某些条件(特别是在此处包含名称中的“TUBA”),如果符合,则将这些工作表上的范围导出为文本文件,工作表名称为文件名。我收到错误91:对象变量或未设置块变量,并在调试时突出显示If WS.name Like "TUBA*" Then
行。我怎样才能解决这个问题?有问题的代码如下。我以前使用几乎相同的代码但没有If
语句(在下面的第二个块中显示)成功,所以我假设我添加它的方式。如果我需要设置一个变量,哪一个有我错过了?
Sub ExportTubatoText()
Dim c As Range, r As Range
Dim output As String
Dim lngcount As Long
Dim WS As Worksheet
Dim Name As String
Dim strFolder As String
strFolder = GetFolder("L:TUBA\")
'\ dialog box opens in that folder as default
'strFolder = GetFolder("L:TUBA\")
If strFolder <> "" Then
MsgBox strFolder
End If
For Each sh In ThisWorkbook.Worksheets
'if worksheet has 'TUBA' in the title, then it is exported to text
If WS.Name Like "TUBA*" Then
output = ""
For Each r In sh.Range("F3:F200").Rows
For Each c In r.Cells
output = output & c.Value
Next c
output = output & vbNewLine
Next r
Name = sh.Name
Open strFolder & "\" & Name & ".txt" For Output As #1
Print #1, output
Close
End If
Next
End Sub
成功的代码:
For Each sh In ThisWorkbook.Worksheets
output = ""
For Each r In sh.Range("O2:O500").Rows
For Each c In r.Cells
output = output & c.Value
Next c
output = output & vbNewLine
Next r
Name = sh.Name
Open strFolder & "\" & Name & ".txt" For Output As #1
Print #1, output
Close
Next
答案 0 :(得分:6)
尝试更改
def prime_checker(prime):
limit = int(math.sqrt(prime))
x = 2
while x <= limit:
if prime % x != 0:
x += 1
if x == limit:
print("%d is prime" % prime)
return True
else:
print("%d Not a prime" % prime)
return False
prime_checker(199)
到
def prime_counting():
list_of_primes = []
for x in range(10):
if prime_checker(x) == True:
list_of_primes.append(x)
print(list_of_primes)
prime_counting()
或将您的For Each更改为If WS.Name Like "TUBA*" Then
答案 1 :(得分:1)
注意:这只是一个想法,而不是答案,因为@Rdster解释了为什么你的第一个代码不起作用。
如果您只使用一个列(就像您的两个代码一样),您可以替换这部分代码:
For Each r In sh.Range("F3:F200").Rows
For Each c In r.Cells
output = output & c.Value
Next c
output = output & vbNewLine
Next r
这一行:
output = Join(Application.Transpose(sh.Range("F3:F200").Value), vbNewLine)