Я успешно написал код VBA для суммирования и форматирования большого набора данных на листе. Сценарий считается успешным, когда макрос запускается на следующем выбранном мной листе. Получив задание применить сценарий ко всем листам в книге, измененный сценарий завершает обобщение для каждого листа, но форматирует только первый. Мы пытались устранить неполадки в моем классе данных, но безуспешно. Это изображение того, как он должен выглядеть.
Мой сценарий для всей книги:
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
Я уверен, что есть другие, гораздо лучшие способы сделать это, но как новичок, это мой салат из кода, и я был бы признателен за любую помощь, почему он не форматирует остальные три листа.
Пользователь Excel для Mac, хотя я также запускал его через Parallels.
1 ответ
Set colJ = Range("J2", Range("J2").End(xlDown)) 'from J2 to the last cell entry
Здесь вы получаете диапазон для активного листа.
Изменить на:
Set colJ = ws.Range("J2", ws.Range("J2").End(xlDown))
Похожие вопросы
Новые вопросы
excel
Только для вопросов по программированию для объектов или файлов Excel или для разработки сложных формул. Вы можете объединить тег Excel с VBA, VSTO, C #, VB.NET, PowerShell, OLE-автоматизацией и другими тегами и вопросами, связанными с программированием, если это применимо. Общая помощь по MS Excel для функций одного листа доступна в Super User.