Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
变量正在从单元格中删除数据-循环帮助;VBA_Vba_Excel - Fatal编程技术网

变量正在从单元格中删除数据-循环帮助;VBA

变量正在从单元格中删除数据-循环帮助;VBA,vba,excel,Vba,Excel,我有两张工作表: a) 包含所有夹具组件的夹具列表 b) 包含具有该设备的存储的存储列表 c) 我需要的是-带有夹具组件的夹具列表,它还列出了具有夹具的商店 我需要创建一个包含两个工作表的第三个工作表。目前我有一个查找fixture名称的find函数;然后根据需要的信息创建变量,并将变量放在第三页上 但是当我设置原始变量信息时,变量名消失了,因此下一个循环,fixture名不会在搜索中被捕获 存储列表很大,这导致宏运行非常缓慢;因此,任何有助于提高效率的帮助都将不胜感激 我附上了我的代码,因为我

我有两张工作表:

a) 包含所有夹具组件的夹具列表
b) 包含具有该设备的存储的存储列表
c) 我需要的是-带有夹具组件的夹具列表,它还列出了具有夹具的商店

我需要创建一个包含两个工作表的第三个工作表。目前我有一个查找fixture名称的find函数;然后根据需要的信息创建变量,并将变量放在第三页上

但是当我设置原始变量信息时,变量名消失了,因此下一个循环,fixture名不会在搜索中被捕获

存储列表很大,这导致宏运行非常缓慢;因此,任何有助于提高效率的帮助都将不胜感激

我附上了我的代码,因为我没有很好地解释这一点

Sub FindTest()


Dim S1 As Excel.Worksheet
Dim S2 As Excel.Worksheet
Dim S3 As Excel.Worksheet
Dim h As Long
Dim i As Long
Dim j As Long
Dim x As Long
Dim l As Long
Dim aCell As Range
Dim bCell As Range
Dim oRange As Range
Dim TitleVar As String
Dim ItemNumber As String
Dim ItemDesc As String
Dim ShipTo As String
Dim StoreNumber As String
Dim UPC As String
Dim Chain As String
Dim DivRange As Range
Dim Match As String
Dim FixtureType As String
Dim FindFixtureName As Range
Dim FindItemNumber As Range
Dim FindUPC As Range
Dim FindItemDesc As Range
Dim lastRow As Integer
Dim rng As Range
Dim wb As Workbook
Dim rng1 As Range



Set S1 = Sheets("Titles")
Set S2 = Sheets("Fixtures")
Set S3 = Sheets("Import")
Set wb = ActiveWorkbook
Set rng1 = S1.Cells.Find("*", S1.[a1], xlFormulas, , , xlPrevious)


Set oRange = S1.Columns(4)

h = 2
j = 2
i = 2
K = 2
l = 2
m = 1

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
S2.Activate
S2.Columns("B:B").Select

With Selection
    Selection.NumberFormat = "General"
    .Value = .Value
End With


TitleVar = S1.Cells(K, 4)
Chain = S2.Cells(h, 1)
ShipTo = S2.Cells(h, 2)
StoreNumber = S2.Cells(h, 4)
UPC = Format(S1.Cells(K, 7), "###########")
lastRow = S1.Range("D" & Rows.Count).End(xlUp).Row
StrSearch = UCase(S2.Cells(h, 6))
FixtureType = S2.Cells(h, 8)
ItemNumber = S1.Range("D" & i).Offset(0, 2)
Match = ShipTo & ItemNumber



'************** Test Worksheet


LastShipTo = S2.Range("B" & Rows.Count).End(xlUp).Row - 1

Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Test"

Range("A1") = "Chain"
Range("B1") = "Match"
Range("C1") = "Ship To Number"
Range("D1") = "Store #"
Range("E1") = "Item Number"
Range("F1") = "Item Description"
Range("G1") = "UPC"
Range("H1") = "Fixture"
Range("I1") = "Fixture Type"
Range("j1") = "Division"
Range("k1") = "Total"

  Range("A1:Q1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 6299648
    .TintAndShade = 0
    .PatternTintAndShade = 0


End With
With Selection.Font
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
End With

Range("A1:K1").HorizontalAlignment = xlCenter



Set FindFixtureName = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

 Do While FindFixtureName Is Nothing
 If FindFixtureName Is Nothing Then
    h = h + 1
    StrSearch = UCase(S2.Cells(h, 6))
    Set FindFixtureName = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
  End If
Loop


Set FindItemNumber = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Offset(0, 
2) Set FindUPC = S1.Range("D:D").Find(What:=StrSearch, After:=S1.Cells(1, 
4), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, 
SearchDirection:=xlNext, MatchCase:=False).Offset(0, 3)
Set FindItemDesc = S1.Range("D:D").Find(What:=StrSearch, After:=S1.Cells(1, 
4), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, 
SearchDirection:=xlNext, MatchCase:=False).Offset(0, 4)

Do Until StrSearch = ""


          Do Until FindFixtureName <> StrSearch

           Match = ShipTo & FindItemNumber
           Sheets("Test").Cells(j, 1) = Chain
           Sheets("Test").Cells(j, 2) = Match
           Sheets("Test").Cells(j, 3) = ShipTo
           Sheets("Test").Cells(j, 4) = StoreNumber
           Sheets("Test").Cells(j, 5) = FindItemNumber
           Sheets("Test").Cells(j, 6) = FindItemDesc
           Sheets("Test").Cells(j, 7) = FindUPC
           Sheets("Test").Cells(j, 8) = StrSearch
           Sheets("Test").Cells(j, 9) = FixtureType


           j = j + 1
           l = l + 1


           FindFixtureName = FindFixtureName.Offset(m, 0)
           FindItemNumber = FindItemNumber.Offset(m, 0)
           FindUPC = FindUPC.Offset(m, 0)
           FindItemDesc = FindItemDesc.Offset(m, 0)
           m = m + 1

           Loop



 TitleVar = S1.Cells(K, 4)


h = h + 1
 l = 1
 ShipTo = S2.Cells(h, 2)
 StrSearch = UCase(S2.Cells(h, 6))
 Match = ShipTo & ItemNumber
 StoreNumber = S2.Cells(h, 4)

 FindFixtureName = vbaNullString

Set FindFixtureName = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

Do While FindFixtureName Is Nothing
If FindFixtureName Is Nothing Then
    h = h + 1
    StrSearch = UCase(S2.Cells(h, 6))
    Set FindFixtureName = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
End If
Loop


Set FindItemNumber = S1.Range("D:D").Find(What:=StrSearch, 
After:=S1.Cells(1, 4), LookIn:=xlValues, LookAt:=xlWhole, 
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Offset(0, 
2)
Set FindUPC = S1.Range("D:D").Find(What:=StrSearch, After:=S1.Cells(1, 4), 
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, 
SearchDirection:=xlNext, MatchCase:=False).Offset(0, 3)
Set FindItemDesc = S1.Range("D:D").Find(What:=StrSearch, After:=S1.Cells(1, 
4), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, 
SearchDirection:=xlNext, MatchCase:=False).Offset(0, 4)

m = 1

Loop




lastRow = Sheets("Test").Range("A" & Rows.Count).End(xlUp).Row


With ActiveWorkbook.Worksheets("DIV")
lR = Sheets("Div").Range("A" & .Rows.Count).End(xlUp).Row
Sheets("Test").Range("J2: J" & lastRow).Formula = 
"=IfError(VLOOKUP(C2,Div!$A$2:$G$" & lR & ",2,0),"""")"

End With


With ActiveWorkbook.Worksheets("Test")
RR = Sheets("Import").Range("A" & .Rows.Count).End(xlUp).Row
Sheets("Test").Range("K2:K" & lastRow).Formula = 
"=IfError(VLOOKUP(B2,Import!$B$2:$J$" & RR & ",8,0),"""")"

Sheets("Test").Range("L1") = "0 Total"
Sheets("Test").Range("L2:L" & lastRow).Formula = "=IF(K2="""",""YES"","""")"

    Sheets("Test").Range("M1") = "1 Total"
Sheets("Test").Range("M2:M" & lastRow).Formula = "=IF(K2=1,""YES"","""")"

    Sheets("Test").Range("N1") = "2 Total"
Sheets("Test").Range("N2:N" & lastRow).Formula = "=IF(K2=2,""YES"","""")"

    Sheets("Test").Range("O1") = "3+ Total"
Sheets("Test").Range("O2:O" & lastRow).Formula = "=IF(K2>=3,""YES"","""")"

    Sheets("Test").Range("P1") = "Dup Store Match"
Sheets("Test").Range("P2:P" & lastRow).Formula = "=D2&"" ""&H2"

    Sheets("Test").Range("Q1") = "Dup Store Count"
Sheets("Test").Range("Q2:Q" & lastRow).Formula = "=IF(P3=P2,""DUP"","""")"

End With


'******  End of Find




Sheets("Test").Cells.EntireColumn.AutoFit


Sheets("Test").Activate

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic



MsgBox "Done"

End Sub
Sub-FindTest()
将S1作为Excel.工作表
将S2作为Excel.工作表
将S3作为Excel.工作表
暗h一样长
我想我会坚持多久
Dim j尽可能长
暗x等长
我和你一样长
Dim aCell As系列
暗bCell As范围
暗橙色作为射程
暗标题为字符串
将ItemNumber设置为字符串
Dim ItemDesc As字符串
将ShipTo变暗为字符串
将存储编号设置为字符串
作为字符串的Dim UPC
暗链如弦
Dim DivRange As范围
暗匹配为字符串
Dim FixtureType作为字符串
Dim FINDFIXT重命名为范围
Dim FindItemNumber作为范围
Dim FindUPC As范围
Dim FINDETIMDESC As范围
将最后一行设置为整数
变暗rng As范围
将wb设置为工作簿
变暗rng1 As范围
集合S1=图纸(“标题”)
设置S2=板材(“固定装置”)
设置S3=图纸(“导入”)
设置wb=ActiveWorkbook
设置rng1=S1.Cells.Find(“*”,S1[a1],xlFormulas,,xlPrevious)
设置橙色=S1。列(4)
h=2
j=2
i=2
K=2
l=2
m=1
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
S2.激活
S2.列(“B:B”)。选择
有选择
Selection.NumberFormat=“常规”
.Value=.Value
以
滴定度=S1。细胞(K,4)
链=S2。单元(h,1)
ShipTo=S2。单元(h,2)
StoreNumber=S2.单元(h,4)
UPC=格式(S1.单元格(K,7),“,”
lastRow=S1.Range(“D”和Rows.Count).End(xlUp).Row
StrSearch=UCase(S2.细胞(h,6))
FixtureType=S2。单元(h,8)
ItemNumber=S1.范围(“D”和i).偏移量(0,2)
匹配=发货人和项目编号(&ItemNumber)
'*************测试工作表
LastShipTo=S2.Range(“B”和Rows.Count)。End(xlUp)。行-1
Worksheets.Add(之后:=工作表(Worksheets.Count)).Name=“测试”
范围(“A1”)=“链”
范围(“B1”)=“匹配”
范围(“C1”)=“发货编号”
范围(“D1”)=“存储”
范围(“E1”)=“项目编号”
范围(“F1”)=“项目说明”
范围(“G1”)=“UPC”
范围(“H1”)=“夹具”
范围(“I1”)=“夹具类型”
范围(“j1”)=“除法”
范围(“k1”)=“总计”
范围(“A1:Q1”)。选择
有选择。内饰
.Pattern=xlSolid
.PatternColorIndex=xlAutomatic
.颜色=6299648
.TintAndShade=0
.PatternTintAndShade=0
以
使用Selection.Font
.ThemeColor=xlThemeColorDark1
.TintAndShade=0
以
范围(“A1:K1”)。水平对齐=xlCenter
设置findTextRename=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)
FindFractRename什么都不是时执行
如果FindTextRename什么都不是,那么
h=h+1
StrSearch=UCase(S2.细胞(h,6))
设置findTextRename=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)
如果结束
环
设置FindItemNumber=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)。偏移量(0,
2) 设置FindUPC=S1.Range(“D:D”).Find(What:=strearch,After:=S1.Cells(1,
4) ,LookIn:=xlValues,LookAt:=xlWhole,SearchOrder:=xlByRows,
SearchDirection:=xlNext,MatchCase:=False)。偏移量(0,3)
设置FindItemDesc=S1.Range(“D:D”).Find(What:=strearch,After:=S1.Cells(1,
4) ,LookIn:=xlValues,LookAt:=xlWhole,SearchOrder:=xlByRows,
SearchDirection:=xlNext,MatchCase:=False)。偏移量(0,4)
直到StrSearch=“”为止
直到找到TextRename StrSearch为止
Match=ShipTo&FindItemNumber
薄板(“试验”)。单元(j,1)=链
表(“测试”)。单元格(j,2)=匹配
表(“试验”)。单元(j,3)=装运至
表(“测试”)。单元(j,4)=存储编号
表(“测试”)。单元格(j,5)=FindItemNumber
表(“测试”)。单元格(j,6)=FindItemDesc
表(“测试”)。单元格(j,7)=FindUPC
表(“测试”)。单元格(j,8)=strearch
薄板(“试验”)。单元(j,9)=固定类型
j=j+1
l=l+1
FindTextRename=FindTextRename.Offset(m,0)
FindItemNumber=FindItemNumber.Offset(m,0)
FindUPC=FindUPC.偏移量(m,0)
FindItemDesc=FindItemDesc.Offset(m,0)
m=m+1
环
滴定度=S1。细胞(K,4)
h=h+1
l=1
ShipTo=S2。单元(h,2)
StrSearch=UCase(S2.细胞(h,6))
匹配=发货人和项目编号(&ItemNumber)
StoreNumber=S2.单元(h,4)
FindTextRename=vbaNullString
设置findTextRename=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)
FindFractRename什么都不是时执行
如果FindTextRename什么都不是,那么
h=h+1
StrSearch=UCase(S2.细胞(h,6))
设置findTextRename=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)
如果结束
环
设置FindItemNumber=S1.Range(“D:D”).Find(What:=strearch,
之后:=S1.单元格(1,4),LookIn:=xlValues,LookAt:=xlother,
SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)。偏移量(0,
2)
设置FindUPC=S1。范围(“D:D”)。查找(什么
 FindFixtureName = FindFixtureName.Offset(m, 0)
 FindFixtureName.Value = FindFixtureName.Offset(m, 0).Value
 Set FindFixtureName = FindFixtureName.Offset(m, 0)
FindFixtureName = vbaNullString