VBA: copy one column to another for various sheets (Loop)

Tarek78

New Member
Joined
Feb 21, 2021
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I have tried to copy my sheet using XL2BB.

I have a workbook with multiple sheets ("Beginning", "A", "B", "C", "End").

For all the sheets from Beginning until the end (I might include some new sheets from one month to another), I want to copy the column B to Column C.

Is that something we can do using a loop?

Thanks



Book1
ABC
1ACCOUNTREVENUEREVENUE
2ABC10
3ABC12
4ABC12
5ABB11
6ADD11
Beginning
 

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)
Try this:
VBA Code:
Sub Copy_Range()
'Modified 6/25/2022  12:44:07 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To Sheets.Count
        Sheets(i).Columns(2).Copy Sheets(i).Columns(3)
    Next
    Application.CutCopyMode = False
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your help. This would work.

However, what if I want to only copy all the sheets between « Beginning » and « End »?
 
Upvote 0
Thanks for your help. This would work.

However, what if I want to only copy all the sheets between « Beginning » and « End »
Thanks for your help. This would work.

However, what if I want to only copy all the sheets between « Beginning » and « End »?
You mean like 1 to 8 or 8 to 12 or like 8 to 9 Is that what you may want?
 
Upvote 0
Hi,

Sorry if my last message wasn’t too clear.

Let’s say I have all theses sheets in June :

« Mapping », « Parameters », « Beginning », « A », « B », « C », « End », « project list »

I want to only copy everything between Beginning and End>> A, B and C

Let’s say in July I have the following sheets :

« Mapping », « Parameters », « Beginning », « A », « B », « C », « D » , « End », « project list », « Account List »

The result would be : A, B, C, D
 
Upvote 0
Try this:

You will get an Input Box asking to the first sheet number and the last sheet number
VBA Code:
Sub Copy_Range()
'Modified  6/25/2022  7:23:05 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
   Dim ans As Long
   Dim anss As Long
   Dim n As Long
   n = Sheets.Count
   
   ans = InputBox("Enter first sheet number", "The Minimum number you can enter is " & 1, "")
   If ans < 1 Then MsgBox "You Entered " & ans & vbNewLine & " This is not a Proper number": Exit Sub
    
    anss = InputBox("Enter last sheet number", "The Maximum number you can enter is " & n, "")
    If anss > n Then MsgBox "You Entered " & anss & vbNewLine & " This is not a Proper number": Exit Sub
    
    For i = 1 To n
        Select Case i
            Case ans To anss
                Sheets(i).Columns(2).Copy Sheets(i).Columns(3)
        End Select
    Next
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Lets try this one it has error checking:
VBA Code:
Sub Copy_Range()
'Modified  6/25/2022  7:56:25 AM  EDT
On Error GoTo M
Application.ScreenUpdating = False
Dim i As Long
   Dim ans As Long
   Dim anss As Long
   Dim n As Long
   n = Sheets.Count
   
   ans = InputBox("Enter first sheet number", "The Minimum number you can enter is " & 1, "")
   If ans < 1 Then MsgBox "You Entered " & ans & vbNewLine & " This is not a Proper number": Exit Sub
    
    anss = InputBox("Enter last sheet number", "The Maximum number you can enter is " & n, "")
    If anss > n Then MsgBox "You Entered " & anss & vbNewLine & " This is not a Proper number": Exit Sub
    
    For i = 1 To n
        Select Case i
            Case ans To anss
                Sheets(i).Columns(2).Copy Sheets(i).Columns(3)
        End Select
    Next
    
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "We had a Problem" & vbNewLine & "You may have entered a character when you should have entered a number" & vbNewLine & "Or some other problem"
End Sub
 
Upvote 0
Solution
Lets try this one it has error checking:
VBA Code:
Sub Copy_Range()
'Modified  6/25/2022  7:56:25 AM  EDT
On Error GoTo M
Application.ScreenUpdating = False
Dim i As Long
   Dim ans As Long
   Dim anss As Long
   Dim n As Long
   n = Sheets.Count
  
   ans = InputBox("Enter first sheet number", "The Minimum number you can enter is " & 1, "")
   If ans < 1 Then MsgBox "You Entered " & ans & vbNewLine & " This is not a Proper number": Exit Sub
   
    anss = InputBox("Enter last sheet number", "The Maximum number you can enter is " & n, "")
    If anss > n Then MsgBox "You Entered " & anss & vbNewLine & " This is not a Proper number": Exit Sub
   
    For i = 1 To n
        Select Case i
            Case ans To anss
                Sheets(i).Columns(2).Copy Sheets(i).Columns(3)
        End Select
    Next
   
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "We had a Problem" & vbNewLine & "You may have entered a character when you should have entered a number" & vbNewLine & "Or some other problem"
End Sub
I'm assuming this worked.
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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