Simple Tweak Required for Excel VBA while Copy rows to different sheets

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
204
Office Version
  1. 2019
Platform
  1. Windows
Code:
Sub CopyRowDemo()
Dim Rw As Long, LstRwRaw As Long, NxtRwDest As Long
Application.ScreenUpdating = False
LstRwRaw = Sheets("Raw Data").Cells(Rows.Count, "B").End(xlUp).Row
For Rw = 3 To LstRwRaw
    With Sheets(Cells(Rw, "B").Value)
        If .Range("B6") = "" Then
            NxtRwDest = 6
        Else
            NxtRwDest = .Cells(Rows.Count, "B").End(xlUp).Row + 1
        End If
        Rows(Rw).Copy .Cells(NxtRwDest, "A")




        With .Cells(NxtRwDest + 1, "G")
            .Formula = "=Sum(G6:G" & NxtRwDest & ")"
            .Font.Bold = True
            .Copy .Offset(, 1).Resize(, 26)
        End With
        
    End With
Next Rw
Application.ScreenUpdating = True
End Sub

I am using the above code to copy rows from a sheet(Raw Data) to different sheets and after copying with the above code it sums the value from G:AG column.

I want some changes in it.
1. It copies entire column to different sheet from the Raw Data Sheet. I want it to be copied from column C to column AH
2. The above code sums from column G:AG, so I want to have a TEXT TOTAL in sum row in F column.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Possibly...
Code:
Sub CopyRowDemo()
    Dim Rw As Long
    Dim LstRwRaw As Long
    Dim NxtRwDest As Long
    
    Application.ScreenUpdating = False
    
    LstRwRaw = Sheets("Raw Data").Cells(Rows.Count, "B").End(xlUp).Row
    For Rw = 3 To LstRwRaw
        With Sheets(Cells(Rw, "B").Value)
            If .Range("B6") = "" Then
                NxtRwDest = 6
            Else
                NxtRwDest = .Cells(Rows.Count, "B").End(xlUp).Row + 1
            End If
            Range("C" & Rw & ":AG" & a).Copy .Cells(NxtRwDest, "A")
            
            With .Cells(NxtRwDest + 1, "G")
                .Offset(, -1).Value = "TOTAL"
                .Formula = "=Sum(G6:G" & NxtRwDest & ")"
                .Font.Bold = True
                .Copy .Offset(, 1).Resize(, 26)
            End With
        End With
    Next Rw
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Possibly...
Code:
Sub CopyRowDemo()
    Dim Rw As Long
    Dim LstRwRaw As Long
    Dim NxtRwDest As Long
    
    Application.ScreenUpdating = False
    
    LstRwRaw = Sheets("Raw Data").Cells(Rows.Count, "B").End(xlUp).Row
    For Rw = 3 To LstRwRaw
        With Sheets(Cells(Rw, "B").Value)
            If .Range("B6") = "" Then
                NxtRwDest = 6
            Else
                NxtRwDest = .Cells(Rows.Count, "B").End(xlUp).Row + 1
            End If
            Range("C" & Rw & ":AG" & a).Copy .Cells(NxtRwDest, "A")
            
            With .Cells(NxtRwDest + 1, "G")
                .Offset(, -1).Value = "TOTAL"
                .Formula = "=Sum(G6:G" & NxtRwDest & ")"
                .Font.Bold = True
                .Copy .Offset(, 1).Resize(, 26)
            End With
        End With
    Next Rw
    
    Application.ScreenUpdating = True
End Sub

Hello with the code I am getting Error 400
 
Upvote 0
Which line is highlighted with the error?

20%2Bjan%2Bcopy%2Bsheets%2B.JPG
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,043
Members
449,092
Latest member
ikke

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