Cut/Paste Data by matching cell to sheet name

MongeredRue

New Member
Joined
Jan 14, 2019
Messages
3
I run a report on a weekly basis, via separate software, and I have to distribute the data from the report into the corresponding sheets. I'm trying to do this in VBA, because i'm sick of doing it by hand, but I'm getting stuck on the most critical part: cutting the data and pasting it to the appropriate sheet. Any help would be appreciated!!

Code:
Worksheets("Working Sheet").Activate

Dim ws As Worksheet
Dim LR As Long, LR2 As Long, x As Long, i As Long

Application.ScreenUpdating = False

With Sheets("Working Sheet")
    LR = .Range("B" & .Rows.Count).End(xlUp).Row
    x = .Cells(1, Columns.Count).End(xlToLeft).Column
End With

For Each ws In ActiveWorkbook.Sheets
    If ws.Name <> "Working Sheet" Then ws.Cells.ClearContents
Next ws


    For Each ws In ActiveWorkbook.Sheets
        With ws
            LR2 = .Range("B" & .Rows.Count).End(xlUp).Row + 1
            For i = 1 To LR
                If ws.Name = Sheets("Working Sheet").Range("B" & 1).Value Then
                .Rows(i).Cut Destination:=Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1)
                Exit For
            End If
            Next i
        End With
    Next ws


Application.ScreenUpdating = True
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to MrExcel.
As you haven't told us what is not working, this is a guess, but try changing the 1 to an i
Code:
If ws.Name = Sheets("Working Sheet").Range("B" & [COLOR=#ff0000]1[/COLOR]).Value Then
 
Upvote 0
Sorry, I should have been more specific.

This is the section that doesn't work.

Code:
 For Each ws In ActiveWorkbook.Sheets
        With ws
            LR2 = .Range("B" & .Rows.Count).End(xlUp).Row + 1
            For i = 1 To LR
                If ws.Name = Sheets("Working Sheet").Range("B" & 1).Value Then
                .Rows(i).Cut Destination:=Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1)
                Exit For
            End If
            Next i

The sheets clear like they're supposed to and the code runs all the way through without errors but the data doesn't get pulled from the main sheet ("Working Sheet") to any of the other sheets.

The above is a modification of the code below. The code below almost worked but would only pull the header line over to the new sheet instead of every instance of the cell
matching the sheet name. If you could help get either one to work, that'd be great. Is there any more info I can provide to assist in troubleshooting?

(I tried the 1 to i correction; no change)

Code:
Sub macro1()

Dim ws As Worksheet
Dim LR As Long, LR2 As Long, x As Long

Application.ScreenUpdating = False

With Sheets("Sheet1")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    x = .Cells(1, Columns.Count).End(xlToLeft).Column
End With

For Each ws In ActiveWorkbook.Sheets
    If ws.name <> "Sheet1" Then ws.Cells.ClearContents
Next ws

For i = 1 To LR
    For Each ws In ActiveWorkbook.Sheets
        With ws
            If .name = Sheets("Sheet1").Range("C" & i).Value Then
                LR2 = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                .Range("A" & LR2).Resize(1, x).Value = Sheets("Sheet1").Range("A1").Resize(1, x).Value
                Exit For
            End If
        End With
    Next ws
Next i

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try removing this line
Code:
 Exit For
 
Upvote 0
You cannot post a workbook to the site,but you can upload to a share site such as dropbox, onedrive, googledrive, mark for sharing & then post a link to the thread.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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