ItalianPlatinum
Well-known Member
- Joined
- Mar 23, 2017
- Messages
- 857
- Office Version
- 365
- 2019
- Platform
- Windows
Hello - Looking for help to modify my code to be able to define a list I choose to drive what the final export file will be. My current code in layman's terms opens an external file, formats it, inserts it into the workbook where the code is, then copies it over to another tab as the defined format it should be in then finally export that sheet to csv.
The variable I am looking for is to be able to input in a range in like column W to only see those types. in my workbook the first part exists. in the external file it only has the unique tag. the data set is huge so i tried to reduce it to simple terms below. i include my orginal code if it helps:
The variable I am looking for is to be able to input in a range in like column W to only see those types. in my workbook the first part exists. in the external file it only has the unique tag. the data set is huge so i tried to reduce it to simple terms below. i include my orginal code if it helps:
Book2 | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | |||
1 | Type | Name | Category | Submissions | Unique Tag | IGNORE | IGNORE | Price | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | IGNORE | SKU | |||||
2 | AA | 1 | ABC1 | 10 | XXXXXX | ||||||||||||||||||||
3 | AA | 2 | BBB1 | 20 | VVVVV | ||||||||||||||||||||
4 | AA | 3 | CCC1 | 10.5 | WWWWW | ||||||||||||||||||||
5 | AA | 4 | DDD1 | 22 | AAAAA | ||||||||||||||||||||
6 | BB | 1 | ACD1 | 23 | PPPPP | ||||||||||||||||||||
7 | BB | 2 | ADD1 | 45 | RRRSSS | ||||||||||||||||||||
8 | BB | 3 | AFF1 | 95 | TYTYT | ||||||||||||||||||||
9 | BB | 4 | AGG1 | 78 | LLLLL | ||||||||||||||||||||
10 | CC | 1 | LLL1 | 45 | UUUUU | ||||||||||||||||||||
11 | CC | 2 | LLL2 | 65 | MMMMM | ||||||||||||||||||||
12 | CC | 3 | LLL3 | 37 | BBBBB | ||||||||||||||||||||
13 | CC | 4 | YYY4 | 19 | DDDDDD | For Export | |||||||||||||||||||
14 | DD | 2 | UUU8 | 82 | EEEEEE | AA | |||||||||||||||||||
15 | DD | 3 | PPP0 | 34 | QQQQQ | CC | |||||||||||||||||||
16 | DD | 4 | MNM1 | 82 | SSSSSS | EE | |||||||||||||||||||
17 | EE | 1 | AFD1 | 71 | BBBBBB | ||||||||||||||||||||
18 | EE | 2 | DF21 | 17 | RRSARA | ||||||||||||||||||||
19 | |||||||||||||||||||||||||
20 | Upload Final file Example | ||||||||||||||||||||||||
21 | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | |||
22 | XX | ABC1 | XX | 10 | XX | 10 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XXXXXX | |||
23 | XX | BBB1 | XX | 20 | XX | 20 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | VVVVV | |||
24 | XX | CCC1 | XX | 10.5 | XX | 10.5 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | WWWWW | |||
25 | XX | DDD1 | XX | 22 | XX | 22 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | AAAAA | |||
26 | XX | ACD1 | XX | 23 | XX | 23 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | PPPPP | |||
27 | XX | ADD1 | XX | 45 | XX | 45 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | RRRSSS | |||
28 | XX | AFF1 | XX | 95 | XX | 95 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | TYTYT | |||
29 | XX | AGG1 | XX | 78 | XX | 78 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | LLLLL | |||
30 | XX | LLL1 | XX | 45 | XX | 45 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | UUUUU | |||
31 | XX | LLL2 | XX | 65 | XX | 65 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | MMMMM | |||
32 | XX | LLL3 | XX | 37 | XX | 37 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | BBBBB | |||
33 | XX | YYY4 | XX | 19 | XX | 19 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | DDDDDD | |||
34 | XX | UUU8 | XX | 82 | XX | 82 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | EEEEEE | |||
35 | XX | PPP0 | XX | 34 | XX | 34 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | QQQQQ | |||
36 | XX | MNM1 | XX | 82 | XX | 82 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | SSSSSS | |||
37 | XX | AFD1 | XX | 71 | XX | 71 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | BBBBBB | |||
38 | XX | DF21 | XX | 17 | XX | 17 | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | XX | RRSARA | |||
Compare |
VBA Code:
Option Explicit
Sub CN()
Dim sPath As String, sPartial As String, sFName As String
Dim rws As Long
Application.ScreenUpdating = False
'Clear CV Sheet
With Sheets("CV")
.Cells.ClearContents
End With
'Clear CN Upload Sheet
With Sheets("CN")
.Range("A2:V" & rows.count).ClearContents
End With
sPath = "XXX" ' <<<<< change accordingly
sPartial = "ZZZ" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
sFName = Dir(sPath & sPartial)
If Len(sFName) > 0 Then
Workbooks.OpenText sPath & sFName
With Sheets("ZZZ")
.Range("A:Z").Copy
End With
Workbooks("NC").Sheets("CV").Range("A1").PasteSpecial
Workbooks("NC").Sheets("CV").Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
CommaDelimited2
Workbooks(sFName).Close SaveChanges:=False
CN_2
Application.ScreenUpdating = True
Else
MsgBox "File not found.", vbExclamation
End If
Workbooks("NC").Sheets("COMPARE").Activate
End Sub
Sub CommaDelimited2()
Columns("A:A").Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(3, 1), Array(20, 1), Array(26, 1), Array(27, 1), _
Array(40, 1), Array(41, 1), Array(54, 1), Array(55, 1), Array(62, 1), Array(76, 1), Array( _
90, 1), Array(103, 1), Array(113, 1), Array(125, 1), Array(136, 1), Array(149, 1), Array( _
152, 1)), TrailingMinusNumbers:=True
End Sub
Sub CN_2()
Dim Sheet As Worksheet
Dim FoundRange As Range
Dim LastRow As Long
Dim rws As Long
With Workbooks("NC").Sheets("CV")
rws = .Range("B2:B2").End(xlDown).row - 1
Workbooks("NC").Sheets("CN").Range("B2").Resize(rws, 1).Value = .Range("B2").Resize(rws).Value
End With
With Workbooks("NC").Sheets("CV")
rws = .Range("E2:E2").End(xlDown).row - 1
Workbooks("NC").Sheets("CN").Range("F2").Resize(rws, 1).Value = .Range("E2").Resize(rws).Value
End With
With Workbooks("NC").Sheets("CV")
rws = .Range("G2:G2").End(xlDown).row - 1
Workbooks("NC").Sheets("CN").Range("H2").Resize(rws, 1).Value = .Range("G2").Resize(rws).Value
End With
With Workbooks("NC").Sheets("CV")
rws = .Range("R2:R2").End(xlDown).row - 1
Workbooks("NC").Sheets("CN").Range("V2").Resize(rws, 1).Value = .Range("R2").Resize(rws).Value
End With
Set Sheet = Worksheets("CN")
LastRow = Sheet.Cells(Sheet.rows.count, 2).End(xlUp).row
If LastRow < 2 Then Exit Sub
On Error Resume Next
Set FoundRange = Sheet.Range("B2:B" & LastRow).SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If FoundRange Is Nothing Then Exit Sub
UpdateColumnValues FoundRange, "A", "MU", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "C", "F", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "D", "F", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "E", "R", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "J", "NA", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "M", "NA", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "N", "#", LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "T", Format(Now, "MM/DD/YY"), LastRow, 2, Sheet, True
UpdateColumnValues FoundRange, "U", "US", LastRow, 2, Sheet, True
Sheets("CN").Copy
GetNameAndSaveAsCSV2
End Sub
Sub GetNameAndSaveAsCSV2()
Dim oWb As Workbook
Dim sMyFile As String
Dim sSavedFile As String
sMyFile = "AAA" & "BBB" & Format(Now, "MMDDYY") & ".csv" ' <<< change as required
Set oWb = ActiveWorkbook ' <<< change as required
' return with drive:\folder\filename.ext of saved file
sSavedFile = FileSaveAs(oWb, sMyFile)
Set oWb = Nothing
End Sub