Columns B, C, D, E, F, contain values and blank spaces. Some of the cells in the columns…
- User
Columns B, C, D, E, F, contain values and blank spaces. Some of the cells in the columns B, C, D, E, F, have a background colour. I want to caputre values into column H, all the cells in columns B, C, D, E, F, that contain values and do not have a background colour. Is there a way to do this.
- Assistant
Yes, it is possible to capture the values from columns B, C, D, E, F that do not have a background color and display them in column H.
You can use a loop to iterate through each cell in columns B, C, D, E, F, and check if they have a background color using the
Interior.Colorproperty. If a cell does not have a background color, you can copy its value to the corresponding cell in column H.Here is an example VBA code that accomplishes this:
Sub CaptureValuesWithoutBackground() Dim ws As Worksheet Dim lastRow As Long Dim cell As Range ' Set the worksheet reference Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your sheet name ' Find the last row in columns B to F lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Loop through each cell in columns B to F For Each cell In ws.Range("B1:F" & lastRow) ' Check if the cell has a background color If cell.Interior.Color = RGB(255, 255, 255) Then ' Replace RGB(255, 255, 255) with the color code of your background ' Copy the value to the corresponding cell in column H ws.Range("H" & cell.Row).Value = cell.Value End If Next cell End Sub - User
the vba code can not work because there are also blank cells in the B:F range
- Assistant
Apologies for the confusion. To account for blank cells in columns B to F, you can modify the code as follows:
Sub CaptureValuesWithoutBackground() Dim ws As Worksheet Dim lastRow As Long Dim cell As Range ' Set the worksheet reference Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your sheet name ' Find the last row in columns B to F lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Loop through each cell in columns B to F For Each cell In ws.Range("B1:F" & lastRow) ' Check if the cell has a background color and is not empty If cell.Interior.Color = RGB(255, 255, 255) And cell.Value <> "" Then ' Replace RGB(255, 255, 255) with the color code of your background ' Copy the value to the corresponding cell in column H ws.Range("H" & cell.Row).Value = cell.Value End If Next cell End SubThis updated code checks if a cell has a background color and is not empty before copying its value to column H.