0

I am trying to automate a sheet I will get twice a month. Basically, it is sorted by a category, which is a singular letter. I have it able to sort the first table of data into separate sheets based on the category. However, I have a separate table of data I want to also sort into the already made sheets based on the same categories but into a new column, preferably with a blank column in between. Below is the code I found somewhere and I was going to just try and edit it to add the second table of data to a different column.

For clarification I get a table of data from two different programs. The first table the below works and creates new sheets within the workbook and lists all data with that same category in it. I want the second table to go into the sheets as well but in column C instead of column A.

Example data:
Table 1:

A 122
S 134
D 122

Table 2:

A 134
S 154
D 879

Code:

Sub Copy_Data()
 Dim r As Range, LastRow As Long, ws As Worksheet
 Dim LastRow1 As Long, MyColumn As String
 Dim LastColumn As Long
 Dim src As Worksheet
 'Change this column Letter to the one with the Co ID in
 MyColumn = "A"
 'Change the worksheet name to the one with the data on
 Set src = Sheets("CalAmp")
 LastRow = src.Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row
 For Each r In src.Range(MyColumn & "2:" & MyColumn & LastRow)
         On Error Resume Next
         Set ws = Sheets(CStr(r.Value))
         On Error GoTo 0
         If ws Is Nothing Then
             Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = CStr(r.Value)
             'This row adds a header from the soyrce sheet
             'remove the ' if you want to do that
             'src.Rows(1).Copy ActiveSheet.Range("A1")
             LastRow1 = Sheets(CStr(r.Value)).Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row
             src.Rows(r.Row).Copy Sheets(CStr(r.Value)).Cells(LastRow1 + 1, 1)
             Set ws = Nothing
         Else
              ' Find the last row in the target sheet for the Co ID sheet
            LastRow1 = Sheets(CStr(r.Value)).Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row
        
            ' Find the next available column in the target sheet
            LastColumn = Sheets(CStr(r.Value)).Cells(1, Sheets(CStr(r.Value)).Columns.Count).End(xlToLeft).Column + 1
        
            ' Reset the worksheet variable for the next iteration
            Set ws = Nothing
         End If
    
 Next r
 End Sub

5
  • What exactly are you copying? The code copies entire rows, which can only be copied to column A. What does "... into a new column..." mean? Are you copying only the values from column A so that they could be copied, e.g., to column C ("... preferably with a blank column in between...")? Please clarify by editing your post. Commented Jan 16 at 14:26
  • "based on the same categories but into a new column" do you mean the category is not in ColA but a different column? And "with a blank row in between" ? Commented Jan 16 at 15:53
  • I edited my post not sure if it makes more sense now. Basically I want the second set of data to go into column C of the previously created sheets. Commented Jan 16 at 17:59
  • A screenshot of a destination sheet after the first loop and then another after the second set of data is added would be helpful here.- Commented Jan 16 at 18:14
  • Or is your source data really only two columns? Commented Jan 16 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.