dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
I have a spreadsheet for calculating services performed for clients. My supervisor wants to be able to create another spreadsheet that is used to record all the clients that have used the service. I have a button that copies from a sheet, NPSS_quote_sheet to another sheet, Costing_tool in the initial workbook so that additional information can be entered. This is the code that the button runs:
The client's name is stored in a merged cell of G7:H7 in the sheet NPSS_quote_sheet. When the code is run, the required information will be copied from NPSS_quote_sheet to Costing_tool. I need the code to also open a workbook called Client_list.xlsm, then check through the client list and see if the name is there and if it isn't, I need the name to be copied to the list. It needs to be sorted into alphabetical order at the end of the procedure, so it copies every row in NPSS_quote_sheet and then checks the name. Every row in NPSS_quote_sheet will be relating to the one client. In Client_list, the list of names will start in A4 and go down from there. I don't know if I should have it copy to a range or to a table in client_list.
Could someone help me with the code I need to add to the above and advise me on whether it should be copied to a range or a table please?
Thanks,
Dave
Code:
Sub cmdSend()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim desWS As Worksheet, srcWS As Worksheet
Set srcWS = ThisWorkbook.Sheets("NPSS_quote_sheet")
Set desWS = ThisWorkbook.Sheets("Costing_tool")
Dim lastRow1 As Long, lastRow2 As Long
Dim i As Long, x As Long, header As Range
lastRow1 = srcWS.Range("B" & srcWS.Rows.Count).End(xlUp).Row
lastRow2 = desWS.Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
With srcWS.Range("A:A,B:B,H:H")
If lastRow2 < 5 Then
lastRow2 = 5
For i = 1 To .Areas.Count
x = .Areas(i).Column
Set header = desWS.Rows(4).Find(.Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)
If Not header Is Nothing Then
srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy
desWS.Cells(lastRow2, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
With desWS
If .Range("A" & .Rows.Count).End(xlUp).Row > 5 Then
desWS.ListObjects.Item("tblCosting").ListRows.Add
End If
.Range("D" & lastRow2 & ":D" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("G7")
.Range("F" & lastRow2 & ":F" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("B7")
.Range("G" & lastRow2 & ":G" & .Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row) = srcWS.Range("B6")
End With
Else
lastRow2 = desWS.Range("A:A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
desWS.ListObjects.Item("tblCosting").ListRows.Add
For i = 1 To .Areas.Count
x = .Areas(i).Column
Set header = desWS.Rows(4).Find(.Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)
If Not header Is Nothing Then
srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy
desWS.Cells(lastRow2 + 1, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
With desWS
.Range("D" & lastRow2 + 1 & ":D" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("G7")
.Range("F" & lastRow2 + 1 & ":F" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B7")
.Range("G" & lastRow2 + 1 & ":G" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B6")
End With
End If
End With
desWS.ListObjects("tblCosting").Sort.SortFields.Clear
desWS.ListObjects("tblCosting").Sort.SortFields. _
Add Key:=desWS.Cells(, 1), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With desWS.ListObjects("tblCosting").Sort
.header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Application
.CutCopyMode = False
.EnableEvents = True
.ScreenUpdating = True
End With
Dim oLst As ListObject
Dim lr As Long, rng As Range
lr = desWS.Cells(Rows.Count, "A").End(xlUp).Row
For i = lr To 4 Step -1
Set rng = desWS.Cells(i, 1)
If WorksheetFunction.CountBlank(rng) = 1 Then
desWS.Rows(i).Delete
End If
Next i
End Sub
The client's name is stored in a merged cell of G7:H7 in the sheet NPSS_quote_sheet. When the code is run, the required information will be copied from NPSS_quote_sheet to Costing_tool. I need the code to also open a workbook called Client_list.xlsm, then check through the client list and see if the name is there and if it isn't, I need the name to be copied to the list. It needs to be sorted into alphabetical order at the end of the procedure, so it copies every row in NPSS_quote_sheet and then checks the name. Every row in NPSS_quote_sheet will be relating to the one client. In Client_list, the list of names will start in A4 and go down from there. I don't know if I should have it copy to a range or to a table in client_list.
Could someone help me with the code I need to add to the above and advise me on whether it should be copied to a range or a table please?
Thanks,
Dave