gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
using excel vba search column G4 to G80 in all sheets of a workbook. If value of cell in…
- User
using excel vba search column G4 to G80 in all sheets of a workbook. If value of cell in column G is greater than 1499 and corresponding value in column F is not equal to "Grand Total", then copy matches found in column G to column I in sheet named Totals and also copy the corresponding value in column F for the matches found in column G to column H in the sheet named Totals
- Assistant
Here is a sample code that should achieve what you are looking for:
codeSub CopyValuesToTotalsSheet() Dim wb As Workbook Dim ws As Worksheet Dim totalsWs As Worksheet Dim lastRow As Long Dim i As Long 'set workbook and worksheet variables Set wb = ThisWorkbook Set totalsWs = wb.Worksheets("Totals") 'loop through each worksheet in the workbook For Each ws In wb.Worksheets 'skip the Totals sheet If ws.Name <> "Totals" Then lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row 'get the last row in column G 'loop through each row in column G For i = 4 To lastRow If ws.Cells(i, "G").Value > 1499 And ws.Cells(i, "F").Value <> "Grand Total" Then totalsWs.Cells(totalsWs.Rows.Count, "I").End(xlUp).Offset(1).Value = ws.Cells(i, "G").Value 'copy value from column G to Totals sheet column I totalsWs.Cells(totalsWs.Rows.Count, "H").End(xlUp).Offset(1).Value = ws.Cells(i, "F").Value 'copy corresponding value from column F to Totals sheet column H End If Next i End If Next ws End Sub