如何修复使用column.insert在我的VBA宏中添加列引起的问题?

huangapple go评论71阅读模式
英文:

How can I fix the issues caused by using column.insert to add a column in my VBA macro?

问题

请帮助我理解如何更新这个VBA以更改输出文件,以包括一个额外的列并更新现有列标题以反映以下内容; “Add Delay Code”和“Remove Delay Code”? 我相信通过更新列G中的现有“Delay Code”字段为“Add Delay Code”并将列H、I和J向右移动,以便列H可以成为新的“Remove Delay Code”列,我可以实现这个需求。但是,我是一个完全不懂VBA的初学者,不知道从哪里开始。

Sub Macro2()
'
' Macro2 Macro
'
Dim startcell As String
Dim Lastrow As String
Dim Lastrow2 As String
Dim rowCount As Integer
Dim ws As String
Dim Nws As String
Dim Nwo As String
Dim Firstcell As Integer
Dim Lastcell As String
Dim Firstrow As Integer
Dim WO1 As String
Dim WO2 As String
Dim WOlastROW As String
Dim StartTime As Date, EndTime As Date
Dim TimeTaken As Double
'
'StartTime = Time
Application.EnableEvents = False
Application.ScreenUpdating = False
GetUserName = Application.UserName
Sheets("OUTLOOK DATA").Visible = True
Sheets("OUTLOOK DATA").Select
Range("A1").Select
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Lastcell = "V" & (Lastrow)
Selection.AutoFilter
ActiveSheet.Range("$A$1:" & (Lastcell)).AutoFilter Field:=1, Criteria1:= _
"=*Crew*", Operator:=xlAnd
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.ShowAllData
Range("A1").Select
Selection.AutoFilter
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:M").Select
Selection.Delete Shift:=xlToLeft
Columns("E:K").Select
Selection.Delete Shift:=xlToLeft
Columns("A:D").EntireColumn.AutoFit
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlFixedWidth, _
OtherChar:="-", FieldInfo:=Array(Array(0, 1), Array(9, 1)), _
TrailingMinusNumbers:=True
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Columns("F:F").Select
Application.CutCopyMode = False
Selection.Copy
Columns("A:A").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("F:F").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$E" & (Lastrow)), , xlYes).Name = _
"Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium2"
Columns("A:D").EntireColumn.AutoFit
Sheets.Add After:=ActiveSheet
Sheets("OUTLOOK DATA").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ws = ActiveSheet.Name
rowCount = 1
startcell = ("A1")
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range(startcell, ("A" + (Lastrow))).Select
ActiveSheet.Range("$A$1:A" & (Lastrow)).RemoveDuplicates Columns:=1, Header:=xlNo
ws = ActiveSheet.Name
rowCount = 1
startcell = ("A1")
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range(startcell, ("A" + (Lastrow))).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range(startcell, ("A" + (Lastrow))), , xlNo).Name = _
"Table2"
Range("Table2[[#All],[Column1]]").Select
ActiveSheet.ListObjects("Table2").TableStyle = "TableStyleLight1"
ActiveWorkbook.Worksheets(ws).ListObjects("Table2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets(ws).ListObjects("Table2").Sort.SortFields.Add _
Key:=Range("Table2[[#All],[Column1]]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets(ws).ListObjects("Table2").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B2").Select
ActiveCell.FormulaR1C1 = "=MATCH([@Column1],'OUTLOOK DATA'!C[-1],0)"
rowCount = 1
startcell = ("A2")
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
For Each i In Range(startcell, ("A" + (Lastrow)))
rowCount = rowCount + 1
Nwo = Cells((rowCount), 1).Value
Firstrow = Cells((rowCount), 2).Value
Secondrow = (Firstrow) + 1
Sheets("OUTLOOK DATA").Select
WO1 = Range("A" & (Firstrow))
WO2 = Range("A" & (Secondrow))
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
(Nwo)
Range("A" & (Firstrow)).Select
WOlastROW = Selection.End(xlDown).Row
If (WO1) = (WO2) Then
Range("C" & (WOlastROW)).Select
Selection.Copy
Range("C" & (Firstrow)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
ActiveSheet.ShowAllData
Sheets("Sheet1").Select
Next i
Sheets("OUTLOOK DATA").Select
Application.CutCopyMode = False
ActiveSheet.Range("Table1[#All]").RemoveDuplicates Columns:=1, Header:= _
xlYes
ActiveWorkbook.Worksheets("OUTLOOK DATA").ListObjects("Table1").Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("OUTLOOK DATA").ListObjects("Table1").Sort. _
SortFields.Add2 Key:=Range("Table1[[#All],[Subject]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
<details>
<summary>英文:</summary>
Can anyone help me understand how I can update this VBA to change the output file to include an additional column and update an existing column header to reflect the following; &quot;Add Delay Code&quot; and &quot;Remove Delay Code&quot;? I believe I can achieve the need by updating the existing &quot;Delay Code&quot; field in Column G to &quot;Add Delay Code&quot; and shift the Columns H, I and J to the right so that Column H can become the new &quot;Remove Delay Code&quot; column. However, I&#39;m a complete VBA n00b and I don&#39;t know where to start.
Sub Macro2()
&#39;
&#39; Macro2 Macro
&#39;
Dim startcell As String
Dim Lastrow As String
Dim Lastrow2 As String
Dim rowCount As Integer
Dim ws As String
Dim Nws As String
Dim Nwo As String
Dim Firstcell As Integer
Dim Lastcell As String
Dim Firstrow As Integer
Dim WO1 As String
Dim WO2 As String
Dim WOlastROW As String
Dim StartTime As Date, EndTime As Date
Dim TimeTaken As Double
&#39;
&#39;StartTime = Time
Application.EnableEvents = False
Application.ScreenUpdating = False
GetUserName = Application.UserName
Sheets(&quot;OUTLOOK DATA&quot;).Visible = True
Sheets(&quot;OUTLOOK DATA&quot;).Select
Range(&quot;A1&quot;).Select
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
Lastcell = &quot;V&quot; &amp; (Lastrow)
Selection.AutoFilter
ActiveSheet.Range(&quot;$A$1:&quot; &amp; (Lastcell)).AutoFilter Field:=1, Criteria1:= _
&quot;=*Crew*&quot;, Operator:=xlAnd
Rows(&quot;2:2&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.ShowAllData
Range(&quot;A1&quot;).Select
Selection.AutoFilter
Columns(&quot;C:C&quot;).Select
Selection.Delete Shift:=xlToLeft
Columns(&quot;D:M&quot;).Select
Selection.Delete Shift:=xlToLeft
Columns(&quot;E:K&quot;).Select
Selection.Delete Shift:=xlToLeft
Columns(&quot;A:D&quot;).EntireColumn.AutoFit
Columns(&quot;A:A&quot;).Select
Selection.TextToColumns Destination:=Range(&quot;F1&quot;), DataType:=xlFixedWidth, _
OtherChar:=&quot;-&quot;, FieldInfo:=Array(Array(0, 1), Array(9, 1)), _
TrailingMinusNumbers:=True
Columns(&quot;G:G&quot;).Select
Selection.Delete Shift:=xlToLeft
Columns(&quot;F:F&quot;).Select
Application.CutCopyMode = False
Selection.Copy
Columns(&quot;A:A&quot;).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns(&quot;F:F&quot;).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range(&quot;A1:E1&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
ActiveSheet.ListObjects.Add(xlSrcRange, Range(&quot;$A$1:$E&quot; &amp; (Lastrow)), , xlYes).Name = _
&quot;Table1&quot;
Range(&quot;Table1[#All]&quot;).Select
ActiveSheet.ListObjects(&quot;Table1&quot;).TableStyle = &quot;TableStyleMedium2&quot;
Columns(&quot;A:D&quot;).EntireColumn.AutoFit
Sheets.Add After:=ActiveSheet
Sheets(&quot;OUTLOOK DATA&quot;).Select
Range(&quot;A2&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets(&quot;Sheet1&quot;).Select
Range(&quot;A1&quot;).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ws = ActiveSheet.Name
rowCount = 1
startcell = (&quot;A1&quot;)
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
Range(startcell, (&quot;A&quot; + (Lastrow))).Select
ActiveSheet.Range(&quot;$A$1:A&quot; &amp; (Lastrow)).RemoveDuplicates Columns:=1, Header:=xlNo
ws = ActiveSheet.Name
rowCount = 1
startcell = (&quot;A1&quot;)
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
Range(startcell, (&quot;A&quot; + (Lastrow))).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range(startcell, (&quot;A&quot; + (Lastrow))), , xlNo).Name = _
&quot;Table2&quot;
Range(&quot;Table2[[#All],[Column1]]&quot;).Select
ActiveSheet.ListObjects(&quot;Table2&quot;).TableStyle = &quot;TableStyleLight1&quot;
ActiveWorkbook.Worksheets(ws).ListObjects(&quot;Table2&quot;).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(ws).ListObjects(&quot;Table2&quot;).Sort.SortFields.Add _
Key:=Range(&quot;Table2[[#All],[Column1]]&quot;), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets(ws).ListObjects(&quot;Table2&quot;).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range(&quot;B2&quot;).Select
ActiveCell.FormulaR1C1 = &quot;=MATCH([@Column1],&#39;OUTLOOK DATA&#39;!C[-1],0)&quot;
rowCount = 1
startcell = (&quot;A2&quot;)
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
For Each i In Range(startcell, (&quot;A&quot; + (Lastrow)))
rowCount = rowCount + 1
Nwo = Cells((rowCount), 1).Value
Firstrow = Cells((rowCount), 2).Value
Secondrow = (Firstrow) + 1
Sheets(&quot;OUTLOOK DATA&quot;).Select
WO1 = Range(&quot;A&quot; &amp; (Firstrow))
WO2 = Range(&quot;A&quot; &amp; (Secondrow))
ActiveSheet.ListObjects(&quot;Table1&quot;).Range.AutoFilter Field:=1, Criteria1:= _
(Nwo)
Range(&quot;A&quot; &amp; (Firstrow)).Select
WOlastROW = Selection.End(xlDown).Row
If (WO1) = (WO2) Then
Range(&quot;C&quot; &amp; (WOlastROW)).Select
Selection.Copy
Range(&quot;C&quot; &amp; (Firstrow)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
ActiveSheet.ShowAllData
Sheets(&quot;Sheet1&quot;).Select
Next i
Sheets(&quot;OUTLOOK DATA&quot;).Select
Application.CutCopyMode = False
ActiveSheet.Range(&quot;Table1[#All]&quot;).RemoveDuplicates Columns:=1, Header:= _
xlYes
ActiveWorkbook.Worksheets(&quot;OUTLOOK DATA&quot;).ListObjects(&quot;Table1&quot;).Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets(&quot;OUTLOOK DATA&quot;).ListObjects(&quot;Table1&quot;).Sort. _
SortFields.Add2 Key:=Range(&quot;Table1[[#All],[Subject]]&quot;), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets(&quot;OUTLOOK DATA&quot;).ListObjects(&quot;Table1&quot;). _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns(&quot;B:C&quot;).Select
Selection.NumberFormat = &quot;m/d/yy;@&quot;
Range(&quot;Table1[[#Headers],[Subject]]&quot;).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = &quot;Order&quot;
Range(&quot;Table1[[#Headers],[Start Date]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Start&quot;
Range(&quot;Table1[[#Headers],[End Date]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Finish&quot;
Range(&quot;Table1[[#Headers],[column1]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;SCH Grade&quot;
Range(&quot;E2&quot;).Select
ActiveCell.Formula2R1C1 = &quot;=IFS(ISNUMBER(SEARCH(&quot;&quot;Hard Date&quot;&quot;,[@Categories],1)),&quot;&quot;B&quot;&quot;,ISNUMBER(SEARCH(&quot;&quot;Firm Date&quot;&quot;,[@Categories],1)),&quot;&quot;C&quot;&quot;,ISNUMBER(SEARCH(&quot;&quot;Flex Date&quot;&quot;,[@Categories],1)),&quot;&quot;D&quot;&quot;)&quot;
Range(&quot;Table1[SCH Grade]&quot;).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Replace What:=&quot;#N/A&quot;, Replacement:=&quot;D&quot;, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Columns(&quot;D:D&quot;).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Sheets(&quot;SAP DATA&quot;).Select
Range(&quot;Activity[[#Headers],[Suboperation]]&quot;) = &quot;Start&quot;
Range(&quot;Activity[[#Headers],[MaintenancePlan]]&quot;) = &quot;Finish&quot;
Range(&quot;Activity[[#Headers],[SystemCondition]]&quot;) = &quot;Update User Status&quot;
Range(&quot;Activity[[#Headers],[Description]]&quot;) = &quot;SCH Grade Update&quot;
Range(&quot;Activity[[#Headers],[Op.short text]]&quot;) = &quot;Delay Code&quot;
Range(&quot;Activity[[#Headers],[Op.System Cond.]]&quot;) = &quot;Scheduler&quot;
Range(&quot;C2:G3&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Columns(&quot;E:G&quot;).NumberFormat = &quot;General&quot;
Columns(&quot;C:D&quot;).NumberFormat = &quot;m/d/yy;@&quot;
&#39;Range(&quot;C2&quot;).Select
Range(&quot;C2&quot;).FormulaR1C1 = &quot;=VLOOKUP([@Order],Table1,2,FALSE)&quot;
&#39;Columns(&quot;C:C&quot;).Select
&#39;Range(&quot;D2&quot;).Select
Range(&quot;D2&quot;).FormulaR1C1 = &quot;=VLOOKUP([@Order],Table1,3,FALSE)&quot;
&#39;Range(&quot;E2&quot;).Select
Range(&quot;E2&quot;).FormulaR1C1 = &quot;=IF(LEFT([@[User status]],3)=&quot;&quot;RSC&quot;&quot;,&quot;&quot;Yes&quot;&quot;,&quot;&quot;No&quot;&quot;)&quot;
&#39;Range(&quot;F2&quot;).Select
Range(&quot;F2&quot;).FormulaR1C1 = &quot;=VLOOKUP([@Order],Table1,4,FALSE)&quot;
Range(&quot;Activity[[#Headers],[Order]:[Scheduler]]&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns(&quot;I:K&quot;).Select
Selection.ClearContents
Selection.NumberFormat = &quot;General&quot;
Range(&quot;Activity[[#Headers],[Column1]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Delete&quot;
Range(&quot;I2&quot;).Select
ActiveCell.FormulaR1C1 = &quot;=IF([@Start]=[@[Earl.start date]],&quot;&quot;YES&quot;&quot;,&quot;&quot;&quot;&quot;)&quot;
Range(&quot;Activity[[#Headers],[Column2]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Check&quot;
Range(&quot;J2&quot;).Select
ActiveCell.FormulaR1C1 = &quot;=IF([@Finish]=[@EarliestEndDate],&quot;&quot;YES&quot;&quot;,&quot;&quot;&quot;&quot;)&quot;
Range(&quot;Activity[[#Headers],[Column3]]&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Goodbye&quot;
Range(&quot;K2&quot;).Select
ActiveCell.FormulaR1C1 = &quot;=COUNTIFS([@[Update User Status]],&quot;&quot;No&quot;&quot;,[@Delete],&quot;&quot;YES&quot;&quot;,[@Check],&quot;&quot;YES&quot;&quot;)&quot;
Sheets(&quot;Sheet1&quot;).Select
Range(&quot;AA1&quot;).Select
ActiveCell.FormulaR1C1 = &quot;=COUNTIF(Activity[Goodbye],1)&quot;
Sheets(&quot;SAP DATA&quot;).Select
If Sheets(&quot;Sheet1&quot;).Range(&quot;AA1&quot;) &gt; 0 Then
ActiveWorkbook.Worksheets(&quot;SAP DATA&quot;).ListObjects(&quot;Activity&quot;).Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets(&quot;SAP DATA&quot;).ListObjects(&quot;Activity&quot;).Sort.SortFields. _
Add2 Key:=Range(&quot;Activity[[#All],[Goodbye]]&quot;), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets(&quot;SAP DATA&quot;).ListObjects(&quot;Activity&quot;).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.ListObjects(&quot;Activity&quot;).Range.AutoFilter Field:=11, Criteria1:= _
&quot;1&quot;
Rows(&quot;2:2&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
ActiveSheet.ShowAllData
End If
Range(&quot;A2&quot;).Select
Columns(&quot;I:I&quot;).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range(&quot;Activity[Scheduler]&quot;) = GetUserName
Columns(&quot;A:H&quot;).EntireColumn.AutoFit
Range(&quot;J2&quot;).Select
ActiveCell.FormulaR1C1 = _
&quot;S:\LDC-LI\Central Scheduling Folder\Outlook Exports\Bot Intake Form&quot;
Range(&quot;J1&quot;).Select
ActiveCell.FormulaR1C1 = &quot;Copy data to file destination below&quot;
Range(&quot;J1:J2&quot;).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range(&quot;J1&quot;).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
Range(&quot;J1:J2&quot;).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns(&quot;J:J&quot;).Select
Selection.ColumnWidth = 75
Range(&quot;Activity[[#Headers],[Order]]&quot;).Select
Range(&quot;Activity[[#Headers],[Order]]&quot;).Select
Sheets(&quot;Macro Controls&quot;).Select
ActiveWindow.SelectedSheets.Delete
Sheets(&quot;Sheet1&quot;).Select
ActiveWindow.SelectedSheets.Delete
Sheets(&quot;OUTLOOK DATA&quot;).Select
ActiveWindow.SelectedSheets.Delete
Application.EnableEvents = True
Application.ScreenUpdating = True
&#39;EndTime = Time
SAVE
&#39;TimeTaken = (EndTime - StartTime) * 24 * 60 * 60
&#39;MsgBox TimeTaken
End Sub
Private Sub SAVE()
&#39;
&#39; Creating a new book in which to place schedules
&#39; Thanks Shawn for your Googling expertise 12-12-17
&#39; Tuesday is officially &quot;BREAKTHROUGH DAY&quot;
USID = Environ(&quot;UserName&quot;)
ActiveWorkbook.SaveAs Filename:=&quot;C:\Users\&quot; &amp; (USID) &amp; &quot;\Desktop\&quot; &amp; &quot;BOT DATA DROP&quot; &amp; &quot;.xlsx&quot;, FileFormat _
:=xlOpenXMLWorkbook, Password:=&quot;&quot;, WriteResPassword:=&quot;&quot;, ReadOnlyRecommended:= _
False, CreateBackup:=False
End Sub
Sub GetWorkorderNumbers()
Dim Lastrow As String
Dim NewLastrow As String
&#39;
Sheets(&quot;Outlook Data&quot;).Select
Columns(&quot;A:A&quot;).Select
Selection.TextToColumns Destination:=Range(&quot;AA1&quot;), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=True, OtherChar:= _
&quot;&amp;&quot;, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers _
:=True
Columns(&quot;B:B&quot;).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns(&quot;AB:AC&quot;).Select
Selection.Copy
Range(&quot;A1:B1&quot;).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(&quot;B1&quot;).Select
ActiveCell.FormulaR1C1 = &quot;2nd WO&quot;
Columns(&quot;AB:AB&quot;).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Lastrow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row + 1
Range(&quot;B2:W&quot; &amp; Lastrow).Select
Selection.Copy
Range(&quot;B&quot; &amp; Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
NewLastrow = Range(&quot;C&quot; &amp; Rows.Count).End(xlUp).Row
Range(&quot;B&quot; &amp; Lastrow &amp; &quot;:B&quot; &amp; NewLastrow).Select
Selection.Copy
Range(&quot;A&quot; &amp; Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns(&quot;B:B&quot;).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Sheets(&quot;Macro Controls&quot;).Select
Range(&quot;A:A&quot;).EntireColumn.Hidden = False
ActiveSheet.ListObjects(&quot;WOnumbers&quot;).Range.AutoFilter Field:=1, Criteria1:= _
&quot;&lt;&gt;1*&quot;, Operator:=xlOr, Criteria2:=&quot;=&quot;
Range(&quot;A2&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
ActiveSheet.ListObjects(&quot;WOnumbers&quot;).Range.AutoFilter Field:=1
Range(&quot;A2&quot;).Select
ActiveSheet.Range(&quot;WOnumbers[#All]&quot;).RemoveDuplicates Columns:=1, Header:= _
xlYes
Range(&quot;A2&quot;).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(&quot;WOnumbers[[#Headers],[Order Numbers]]&quot;).Select
Sheets(&quot;OUTLOOK DATA&quot;).Visible = False
Sheets(&quot;SAP DATA&quot;).Visible = True
End Sub
I&#39;ve tried to add the column with column.insert code, and it caused everything to go haywire. I am feeling like this code is too complex for a simple fix. This is also a &#39;part 2&#39; macros I have to another &#39;part 1&#39; macros that formats data from Microsoft Outlook to align with data that comes from SAP in &#39;part 2&#39;.
</details>
# 答案1
**得分**: 1
我不知道这是否是你所面对的问题(因为坦白说,我不打算尝试理解你的代码 - 这么长的代码块实在太多了 - 这样长的过程实际上不应该存在,通常你会尝试将不同的功能拆分成子程序和函数)。
但现在我认为可能存在的问题是,当插入时,你正在改变列的位置。如果你在A列之后插入1列,那么列B现在将成为列C,依此类推。这将破坏对列B或C(或任何大于A的其他列)的可能引用 - 这就是为什么在插入行和列时,通常从最大的开始,然后从那里往回工作的原因。例如,假设你想在A、D和F之后插入一列。那么你应该首先在F之后插入列,然后是D,最后是A。这样,后续的插入操作将不会受到其他插入操作引起的列的移动的影响。
<details>
<summary>英文:</summary>
I don&#39;t know if this is the problem you face (because frankly I am not gonna try understanding your code - this wall of code is just too much - a procedure this long shoudn&#39;t really exist, you&#39;d usually try and split different functionalities into subs and functions.)
But now to what I think could be the issue you are facing. When inserting you are changing what Columns are where. If you insert 1 Column after Column A; Column B will now be Column C and so on. This will then screw up possible references to Column B or C (or any other Column greater than A) - This is why, when inserting Rows and Columns you usually start with the biggest and work back from there. For example say you want to insert a column after A, D and F. Then you&#39;d start by inserting the Column after F, then D and at last A. This way subsequent insert operations will not be affected by the shifting of columns caused by other insert operations.
</details>

huangapple
  • 本文由 发表于 2023年6月1日 09:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定