Help with VBA modification

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
71
Office Version
  1. 2021
Platform
  1. Windows
Hello.

I need some help to modify this VBA... Right now this VBA only works in "Sheet1". I would like it to work in all 8 sheets in my workbook at once... So in "Sheet1, Sheet2, Sheet3, Sheet4, Sheet5, Sheet6, Sheet7, Sheet8"

Is this possible?

Here is VBA i use
VBA Code:
Option Explicit
Sub Delete_White_Cells_And_Shift_Up()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change sheet name to suit ***
    Dim r As Range
    Set r = ws.Range("L1627:VQX1627").CurrentRegion
    
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("L1627:VQX1627").Resize(LRow, LCol).Value = b
    MsgBox Timer - t
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Your normal approach would be
Code:
For each ws In thisWorkbook.Worksheets

    '-----> do all your stuff here

Next ws
 
Upvote 1
Assuming your sheet names are "Sheet1" to "Sheet8" as you say, then perhaps
VBA Code:
Sub Delete_White_Cells_And_Shift_Up()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Dim r As Range
    Dim a, b
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long, Indx As Long

    For Indx = 1 To 8
        Set ws = Worksheets("Sheet" & Indx)                 '<-- *** Change sheet name to suit ***
        Set r = ws.Range("L1627:VQX1627").CurrentRegion

        a = r

        ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

        LRow = r.Rows.Count
        LCol = r.Columns.Count
        k = 1
        For j = 1 To LCol
            For i = 1 To LRow
                If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
                    b(k, j) = a(i, j)
                    k = k + 1
                End If
            Next i
            k = 1
        Next j
        ws.Range("L1627:VQX1627").Resize(LRow, LCol).Value = b
        b = Nothing
    Next Indx
    MsgBox Timer - t
End Sub
 
Upvote 1
Assuming your sheet names are "Sheet1" to "Sheet8" as you say, then perhaps
VBA Code:
Sub Delete_White_Cells_And_Shift_Up()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Dim r As Range
    Dim a, b
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long, Indx As Long

    For Indx = 1 To 8
        Set ws = Worksheets("Sheet" & Indx)                 '<-- *** Change sheet name to suit ***
        Set r = ws.Range("L1627:VQX1627").CurrentRegion

        a = r

        ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

        LRow = r.Rows.Count
        LCol = r.Columns.Count
        k = 1
        For j = 1 To LCol
            For i = 1 To LRow
                If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
                    b(k, j) = a(i, j)
                    k = k + 1
                End If
            Next i
            k = 1
        Next j
        ws.Range("L1627:VQX1627").Resize(LRow, LCol).Value = b
        b = Nothing
    Next Indx
    MsgBox Timer - t
End Sub
Yes my sheets are named Sheet1 to Sheet8 but it doesnt work. I get this error on screenshot.
 

Attachments

  • 1.png
    1.png
    61.8 KB · Views: 15
Upvote 0
At which line of code?

(and on which worksheet, 1 to 8)
 
Upvote 0
Try changing this line
VBA Code:
b = Nothing
to this
VBA Code:
Set b = Nothing
 
Upvote 1
Seems like the code that @jolivanes gave you is the most straight forward and simple answer. Have you tried it?

VBA Code:
Option Explicit
Sub Delete_White_Cells_And_Shift_Up()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    For each ws In thisWorkbook.Worksheets
    Dim r As Range
    Set r = ws.Range("L1627:VQX1627").CurrentRegion
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("L1627:VQX1627").Resize(LRow, LCol).Value = b
    Next ws
    MsgBox Timer - t
End Sub
 
Upvote 1
Solution
All you have to do to find the line is press the "Debug" button on that message
I didnt know that sorry...
Try changing this line
VBA Code:
b = Nothing
to this
VBA Code:
Set b = Nothing
It works great now after this fix thanks.
Seems like the code that @jolivanes gave you is the most straight forward and simple answer. Have you tried it?

VBA Code:
Option Explicit
Sub Delete_White_Cells_And_Shift_Up()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    For each ws In thisWorkbook.Worksheets
    Dim r As Range
    Set r = ws.Range("L1627:VQX1627").CurrentRegion
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("L1627:VQX1627").Resize(LRow, LCol).Value = b
    Next ws
    MsgBox Timer - t
End Sub
This also works i just didnt know where to put this line exactly.

Thanks for your help guys you are great... :)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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