Updating VBA Code Help

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello,

Can someone please help with updating an old code.
What the code is trying to do is take values in column B and copy/paste them on a new sheet.
Column B can have multiple lines with different information, but the values in column B will always be the same.

Code:
Dim LastCol As Integer, i As Long, iStart As Long, iEnd As Long
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    With ActiveSheet
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    
    iStart = 2
    For i = 2 To lastrow
        If .Range("B" & i).Value <> .Range("B" & i + 1).Value Then
            iEnd = i
            Sheets.Add after:=Sheets(Sheets.Count)
            Set ws = ActiveSheet
            On Error Resume Next
            ws.Name = .Range("B" & iStart).Value
            On Error GoTo 0
            ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
            .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
            iStart = iEnd + 1
        End If
    Next i
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    

    
For Each ws In Sheets
    
    Sheets("Retro").Select
    Range("A1:R1").Copy
    
    If ws.Visible Then ws.Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Columns("A:R").AutoFit
    ActiveSheet.Range("A1").Select
    
Next


EDIT: What it seems to be having an issue with is pulling data from Column A rather than B to copy/paste and create new sheets.
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Trial this...
Code:
With ActiveSheet
    lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    
    iStart = 2
    For i = 2 To lastrow
        If .Range("B" & i).Value <> .Range("B" & i + 1).Value Then
            iEnd = i
            Sheets.Add after:=Sheets(Sheets.Count)
            Set ws = Sheets(Sheets.Count) 'ActiveSheet
            On Error Resume Next
            ws.Name = .Range("B" & iStart).Value
            On Error GoTo 0
            ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
            .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
            iStart = iEnd + 1
        End If
    Next i
    End With
HTH. Dave
 
Upvote 0
Hi Dave,

This seems to work better however, there are still more sheets being created than necessary.
For example column B could have the same value in the beginning and also at the end. I think your updated code puts them into multiple sheets rather than on the same sheet because they have the same value.
 
Upvote 0
Well I never said that your original code logic did what U wanted it to do because I don't know what that is. Your original request was that the code was pulling data from "A" instead of "B"... you never mentioned that more sheets were being created than necessary or that column B could have the same value in the beginning and also at the end... which I don't understand. I'm guessing that this line of code is wrong and causing more sheets to be made than U need...
Code:
If .Range("B" & i).Value <> .Range("B" & i + 1).Value Then
It says that if a value in the active sheet B is not the same as the next row value in B then create a new sheet... I don't get it or why this would be a condition? Maybe clearly state what U want your outcome to be and what the criteria are for B. Dave
 
Upvote 0
Hi Dave,

I apologize for not stating my full intentions on the code however, I have found out how to fix my original code.
Thank you for helping, have a great day.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top