dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,373
- Office Version
- 365
- 2016
- Platform
- Windows
I have some vba code that copies rows from a table to another table on another sheet.
The headers for columns D, F and G on the destination table keep being replaced with column1, column 2 and column 3, instead of being left the as they are.
I can't see where it is doing it in the code. Could someone please help me?
The headers for columns D, F and G on the destination table keep being replaced with column1, column 2 and column 3, instead of being left the as they are.
I can't see where it is doing it in the code. Could someone please help me?
Code:
[FONT="]Sub cmdSendNP()[/FONT]
[FONT="] Application.ScreenUpdating = False[/FONT]
[FONT="] Application.EnableEvents = False[/FONT]
[FONT="] Dim desWS As Worksheet, srcWS As Worksheet[/FONT]
[FONT="] Set srcWS = ThisWorkbook.Sheets("CSS_quote_sheet")[/FONT]
[FONT="] Set desWS = ThisWorkbook.Sheets("Costing_tool")[/FONT]
[FONT="] Dim lastRow1 As Long, lastRow2 As Long[/FONT]
[FONT="] Dim i As Long, x As Long, header As Range[/FONT]
[FONT="] lastRow1 = srcWS.Range("B" & srcWS.Rows.Count).End(xlUp).Row[/FONT]
[FONT="] lastRow2 = desWS.Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row[/FONT]
[FONT="] With srcWS.Range("A:A,B:B,G:G")[/FONT]
[FONT="] If lastRow2 < 5 Then[/FONT]
[FONT="] lastRow2 = 5[/FONT]
[FONT="] For i = 1 To .Areas.Count[/FONT]
[FONT="] x = .Areas(i).Column[/FONT]
[FONT="] Set header = desWS.Rows(4).Find(.Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)[/FONT]
[FONT="] If Not header Is Nothing Then[/FONT]
[FONT="] srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy[/FONT]
[FONT="] desWS.Cells(lastRow2, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues[/FONT]
[FONT="] End If[/FONT]
[FONT="] Next i[/FONT]
[FONT="] With desWS[/FONT]
[FONT="] If .Range("A" & .Rows.Count).End(xlUp).Row > 5 Then[/FONT]
[FONT="] .ListObjects.Item("tblCosting").ListRows.Add[/FONT]
[FONT="] .ListObjects.Item("tblCosting").DataBodyRange.Columns(1).NumberFormat = "dd/mm/yyyy"[/FONT]
[FONT="] End If[/FONT]
[FONT="] .Range("D" & lastRow2 & ":D" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("B5")[/FONT]
[FONT="] .Range("F" & lastRow2 & ":F" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("B7")[/FONT]
[FONT="] .Range("G" & lastRow2 & ":G" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("B6")[/FONT]
[FONT="] End With[/FONT]
[FONT="] Else[/FONT]
[FONT="] lastRow2 = desWS.Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row[/FONT]
[FONT="] desWS.ListObjects.Item("tblCosting").ListRows.Add[/FONT]
[FONT="] For i = 1 To .Areas.Count[/FONT]
[FONT="] x = .Areas(i).Column[/FONT]
[FONT="] Set header = desWS.Rows(4).Find(.Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)[/FONT]
[FONT="] If Not header Is Nothing Then[/FONT]
[FONT="] srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy[/FONT]
[FONT="] desWS.Cells(lastRow2 + 1, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues[/FONT]
[FONT="] End If[/FONT]
[FONT="] Next i[/FONT]
[FONT="] With desWS[/FONT]
[FONT="] .Range("D" & lastRow2 + 1 & ":D" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B5")[/FONT]
[FONT="] .Range("F" & lastRow2 + 1 & ":F" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B7")[/FONT]
[FONT="] .Range("G" & lastRow2 + 1 & ":G" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B6")[/FONT]
[FONT="] End With[/FONT]
[FONT="] End If[/FONT]
[FONT="] End With[/FONT]
[FONT="] desWS.ListObjects("tblCosting").Sort.SortFields.Clear[/FONT]
[FONT="] desWS.ListObjects("tblCosting").Sort.SortFields. _[/FONT]
[FONT="] Add Key:=desWS.Cells(, 1), SortOn:=xlSortOnValues, Order:= _[/FONT]
[FONT="] xlAscending, DataOption:=xlSortNormal[/FONT]
[FONT="] With desWS.ListObjects("tblCosting").Sort[/FONT]
[FONT="] .header = xlYes[/FONT]
[FONT="] .MatchCase = False[/FONT]
[FONT="] .Orientation = xlTopToBottom[/FONT]
[FONT="] .SortMethod = xlPinYin[/FONT]
[FONT="] .Apply[/FONT]
[FONT="] End With[/FONT]
[FONT="] [/FONT]
[FONT="] Call AddNameNP[/FONT]
[FONT="] [/FONT]
[FONT="] With Application[/FONT]
[FONT="] .CutCopyMode = False[/FONT]
[FONT="] .EnableEvents = True[/FONT]
[FONT="] .ScreenUpdating = True[/FONT]
[FONT="] End With[/FONT]
[FONT="] Dim oLst As ListObject[/FONT]
[FONT="] Dim lr As Long, rng As Range[/FONT]
[FONT="] lr = desWS.Cells(Rows.Count, "A").End(xlUp).Row[/FONT]
[FONT="] For i = lr To 4 Step -1[/FONT]
[FONT="] Set rng = desWS.Cells(i, 1)[/FONT]
[FONT="] If WorksheetFunction.CountBlank(rng) = 1 Then[/FONT]
[FONT="] desWS.Rows(i).Delete[/FONT]
[FONT="] End If[/FONT]
[FONT="] Next i[/FONT]
[FONT="]End Sub[/FONT]