thedeadzeds
Active Member
- Joined
- Aug 16, 2011
- Messages
- 442
- Office Version
- 365
- Platform
- Windows
Hi Guys,
I have the following code which works great. It essentially finds the column name from the data tab and copies the data to the New Tab. Is there a way to create a message if for some reason the column names are not found in the data tab? So if any of the column headers are not found, end the code with a message. FYI, this is just a snippet of the code, at some point i will need to create for about 15 column headers.
Many thanks
I have the following code which works great. It essentially finds the column name from the data tab and copies the data to the New Tab. Is there a way to create a message if for some reason the column names are not found in the data tab? So if any of the column headers are not found, end the code with a message. FYI, this is just a snippet of the code, at some point i will need to create for about 15 column headers.
Many thanks
VBA Code:
Sub New()
If MsgBox("Are you sure you want to run VBA?", vbYesNo) = vbNo Then Exit Sub
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Data") '<== Sheet that has raw data
Dim LRow As Long, Found As Range
Dim lastRow As String
Dim lastRow2 As String
lastRow = Sheets("New").Cells(Rows.Count, "A").End(xlUp).Row
lastRow2 = Sheets("New").Cells(Rows.Count, "B").End(xlUp).Row
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Dealership
Set Found = ws.Range("A1:KG1").Find("Dealership") '<== Header name to search for
If Not Found Is Nothing Then
LRow = ws.Cells(ws.Rows.Count, Found.Column).End(xlUp).Row
ws.Range(ws.Cells(2, Found.Column), ws.Cells(LRow, Found.Column)).Copy
Sheets("New").Range("A" & lastRow + 1).PasteSpecial xlPasteValues '<== Sheet to paste data
End If
ws.Activate
ActiveSheet.Select
' Date
Set Found = ws.Range("A1:KG1").Find("OrderDate") '<== Header name to search for
If Not Found Is Nothing Then
LRow = ws.Cells(ws.Rows.Count, Found.Column).End(xlUp).Row
ws.Range(ws.Cells(2, Found.Column), ws.Cells(LRow, Found.Column)).Copy
Sheets("New").Range("B" & lastRow + 1).PasteSpecial xlPasteValues '<== Sheet to paste data
End If
ws.Activate
ActiveSheet.Select
End Sub