VBA: Referring to rows as ranges

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I am trying to loop through all cells in a row (row 7 starting at column E to last used column) and copying the value in the cell below (row 8) if cell in row 7 does not equal zero.

I am not sure if I've written the code correctly for this. Would appreciate any advice.

Code:
Sub SummarizeData()    Dim LastCol As Integer
    
    Rows("2:" & Rows.Count).ClearContents
    Worksheets("Volume Allocation").Activate
    
    With ActiveSheet
        LastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
    End With
    
    For i = 7 To LastCol
        If Worksheet.Cells(7, i).Value <> 0 Then
             ActiveSheet.Cells.Range("8:i").Value.Copy Destination:=Sheets("Journal").Range("A1")
             PasteSpecial Paste:=xlPasteValues, Transpose:=True
        End If
    Next i
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try the code below on a copy of your workbook.

Please note that I can't tell by either your code or description what sheet the line in red is referring to.

Please also note that the code hasn't been tested.

Code:
Sub SummarizeData()
    Dim LastCol As Long, x As Long, i As Long
    Application.ScreenUpdating = False
    [COLOR="#FF0000"]Rows("2:" & Rows.Count).ClearContents[/COLOR]
    
    With Worksheets("Volume Allocation")
    
        LastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
        x = 1

        For i = 5 To LastCol
            If .Cells(7, i).Value <> 0 Then
                Sheets("Journal").Cells(x, "A").Value = .Cells(8, i).Value
                x = x + 1
            End If
        Next i
        
    End With
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Not much different from above.
Code:
Sub Maybe()
Dim i As Long, sh2 As Worksheet
Set sh2 = Worksheets("Journal")
    For i = 5 To Cells(7, Columns.Count).End(xlToLeft).Column
        If Cells(7, i).Value <> 0 Then Cells(7, i).Offset(1).Copy sh2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    Next i
End Sub
 
Upvote 0
Thank you both!! A cheeky follow-up question: how can I change the fill color of only the cells being copied when this macro is run (i.e. the copied cells in "Journal")
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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