简体   繁体   中英

VBA script to format cells within a column range only formats the first sheet in the workbook

I have successfully scripted VBA code for summarizing and formatting a large set of data within a sheet. The script is successful when the macro is run on the next sheet I select. When tasked to apply the script across all sheets in the workbook, the modified script completes the summarizations for each sheet, but only formats the first. We tried to troubleshoot in my data class, but to no avail. This is an image of what it is supposed to look like.

在此处输入图像描述

My script for the whole workbook:

Sub tickerdata_all_ws()

    
    'define variables
        dim ws as Worksheet
        Dim ticker As String
        Dim stock_vol As Long
        Dim yrclose As Double
        Dim yrchange As Double
        Dim yrvar As Double
        Dim i As Long
        Dim sumrow As Integer
        Dim lastrow As Long

        lastrow = ActiveSheet.UsedRange.Rows.Count
    for each ws in Worksheet
        'create the column headers
        ws.Range("H1").Value = "Ticker"
        ws.Range("J1").Value = "Yearly Change"
        ws.Range("K1").Value = "Percent Change"
        ws.Range("L1").Value = "Total Stock Volume"

        'which row our summaries will be placed for above columns
        sumrow = 2

            'the loop checks each iteration until the last row
            
        For i = 2 To lastrow
                
                'we need to capture the price of the ticker if it is the first of its year
            Dim firstprice As Boolean
                
            If firstprice = False Then 'false is the default boolean value, so this statement is true
                    
                Dim yropen As Double
                        
                    yropen = ws.Cells(i, 3).Value
                        
                    firstprice = True 'we have captured the opening price of the year for the ticker
                    
            End If
                    
                    'now we can check if we are in the same ticker value
            If ws.Cells(i + 1, 1).Value <> ws.Cells(i, 1).Value Then
                    
                    'this should happen when the cell values are finally different / capture all the values
                    ticker = ws.Cells(i, 1).Value
                    
                    stock_vol = ws.Cells(i, 7).Value
                    
                    yrclose = ws.Cells(i, 6).Value
                    
                    yrchange = yrclose - yropen
                    
                If yropen <> 0 Then 'this prevents dividing by zero which will result in overflow error 6
                    
                            yrvar = (yrclose - yropen) / yrclose
                    
                Else
                        
                            yrvar = 0
                            yrchange = 0
                        
                End If
                    

                'insert values into the summary
                ws.Cells(sumrow, 9).Value = ticker
                ws.Cells(sumrow, 10).Value = yrchange
                ws.Cells(sumrow, 11).Value = yrvar
                ws.Cells(sumrow, 12).Value = stock_vol
                sumrow = sumrow + 1 'sets the stage for the next set of data into row 3

                stock_vol = 0 'resets vol for the next ticker
                    
                firstprice = False 'allows the next 'first' open price of the loop to be captured
                
            End If

        
        Next i  'finish i iteration of the loop
            


        ws.Range("K:K").NumberFormat = "0.0%" 'aesthetic preference


        'format columns colors
        Dim colJ As Range
        Dim Cell as Range

        Set colJ = Range("J2", Range("J2").End(xlDown)) 'from J2 to the last cell entry
            
        For Each Cell In colJ
                
            If Cell.Value > 0 Then
                    Cell.Interior.ColorIndex = 50
                    Cell.Font.ColorIndex = 2
            ElseIf Cell.Value < 0 Then
                    Cell.Interior.ColorIndex = 30
                    Cell.Font.ColorIndex = 2
            Else
                    Cell.Interior.ColorIndex = xlNone 'this really serves no purpose
            End If
                
        Next
    next ws
End Sub

I am sure there are other, much better ways to accomplish this, but as a novice, this is my code salad, and I'd appreciate any help as to why it is not formatting the other three sheets.

Excel for Mac user, though I've run it via Parallels as well.

Set colJ = Range("J2", Range("J2").End(xlDown)) 'from J2 to the last cell entry

here you get range for active sheet.

Change to:

Set colJ = ws.Range("J2", ws.Range("J2").End(xlDown))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM