不可否认,很多代码都是从其他帮助主题中复制粘贴的。
代码正在通过Sheets("程序")并查看H列中的值。根据该值,它将查找具有相应名称的另一个工作表并将整行放入它,或者如果纸张不存在,它将制作纸张并将行放入其中。
Option Explicit
Sub Fr33M4cro()
Dim sh33tname As String
Dim custNameColumn As String
Dim i As Long
Dim stRow As Long
Dim customer As String
Dim ws As Worksheet
Dim sheetExist As Boolean
Dim sh As Worksheet
Dim wsRow As Long
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wasVISIBLE As Boolean
Dim shNAMES As Range, Nm As Range, NmSTR As String
sh33tname = "programs"
custNameColumn = "H"
stRow = 3
Set wsTEMP = Sheets("Sheet3")
Set sh = Sheets(sh33tname)
Set ws = Sheets(Worksheets.Count)
For i = stRow To sh.Range("H" & Rows.Count).End(xlUp).Row
customer = sh.Range("H" & i).Value
For Each ws In ThisWorkbook.Sheets
If StrComp(ws.Name, customer, vbTextCompare) = 0 Then
sheetExist = True
Exit For
End If
Next
If sheetExist Then
CopyRow i, sh, ws, custNameColumn, wsRow
Else
wsTEMP.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = sh.Range(custNameColumn & i).Value
CopyRow i, sh, ws, custNameColumn, wsRow
End If
Reset sheetExist
Next i
End Sub
Private Sub CopyRow(i As Long, ByRef sh As Worksheet, ByRef ws As Worksheet, custNameColumn As String, wsRow As Long)
wsRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
sh.Rows(i & ":" & i).Copy
ws.Rows(wsRow & ":" & wsRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Private Sub Reset(ByRef x As Boolean)
x = False
End Sub
Private Sub InsertSheet(shName As String)
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = shName
End Sub
Function FixStringForSheetName(shSTR As String) As String
'replace each forbidden character with something acceptable
shSTR = Replace(shSTR, ":", "")
shSTR = Replace(shSTR, "?", "")
shSTR = Replace(shSTR, "*", "")
shSTR = Replace(shSTR, "/", "-")
shSTR = Replace(shSTR, "\", "-")
shSTR = Replace(shSTR, "[", "(")
shSTR = Replace(shSTR, "]", ")")
'sheet names can only be 31 characters
FixStringForSheetName = Trim(Left(shSTR, 31))
End Function
我遇到的问题是它会创建一个具有列H值的新工作表,但是然后"运行时错误91:对象变量或者没有在行中设置块变量&# 34 ;.如果我结束宏并重新启动它而不更改任何内容,它将填充上次生成的第一张表的所有行,为列H的下一个值创建一个新表,再次运行时运行错误91。 它指向这行代码。
wsRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
从我收集的内容来看,我在第一部分中已经宣布了WsRow As Long,但我从未将其设置为某种东西。因为我是一个新手,我不知道该怎么设置它,因为它不会干扰脚本的其余部分。
答案 0 :(得分:1)
创建新工作表时,您没有将其与ws工作表var关联。您使用ActiveSheet分配名称;使用ActiveSheet为CopyRow提供目标。
...
If sheetExist Then
CopyRow i, sh, ws, custNameColumn, wsRow
Else
wsTEMP.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = sh.Range(custNameColumn & i).Value
CopyRow i, sh, ActiveSheet, custNameColumn, wsRow
End If
...