How do I reference range of columns for my copy/paste VBA code?

Jeevz_87

New Member
Joined
Sep 21, 2021
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi All,


I've been busy creating a code that, depending on whether the values in a column are greater/less than 0, the code will copy those values to the bottom of the table and paste them into a corresponding table.

The problem Im having is that Ive had to repeat this code for each column and am unsure on how to reference a range of columns for the copy and paste portion of my code.

I want my code to follow this logic;

'If values in table columns Z:BI are greater than/less than zero, then select those values only , copy only those values and paste them in corresponding range BL:CU. Else if values equal zero, do nothing.'

My existing code Im using currently loops from row 12 down to the bottom of the table in a column before pasting, however this is abit slow and only focuses on one column at a time.

VBA Code:
Sub CopyValuesP1FY23()
    
    Dim WS As Worksheet
    Dim iLastRow As Long
    Dim i As Long
    
   
    Set ws = ActiveSheet
        
    
    Let iLastRow = ws.Cells(WS.Rows.Count, "Z").End(xlUp).Row
        
    
    For i = 12 To iLastRow
       
        
        If ws.Cells(i, 26).Value > 0 Then
            
           
            ws.Cells(i, 64).Value = ws.Cells(i, 26).Value
            
        
        ElseIf ws.Cells(i, 26).Value < 0 Then
            
            
            ws.Cells(i, 64).Value = ws.Cells(i, 26).Value
                
        End If
            
    Next i
        
End Sub

Is there a way to do what I'm proposing using the above example but incorporating the above ranges (i.e Z:BI Copy, BL:CU Paste) and maybe speeding up the process by using a less process intensive code?

Thanks,



Jeevz
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Jeevz,

how about

VBA Code:
Sub MrE1222730_161450F()
    
  Dim WS As Worksheet
  Dim rngCell As Range
  
  Application.ScreenUpdating = False
  Set WS = ActiveSheet
  
  For Each rngCell In WS.Range("Z12", WS.Range("BI" & WS.Cells(WS.Rows.Count, "Z").End(xlUp).Row))
    If rngCell.Value <> 0 Then rngCell.Offset(, 28).Value = rngCell.Value
  Next rngCell
        
  Set WS = Nothing
  Application.ScreenUpdating = True

End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,892
Messages
6,122,112
Members
449,066
Latest member
Andyg666

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