Need Help Deleting Empty Columns Based on Condition in Excel Spreadsheet by VBA code

Status
Not open for further replies.

Guna13

Board Regular
Joined
Nov 22, 2019
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I need assistance on how to efficiently delete blank and empty columns based on a condition in the second row, starting from "F" column to the last column. I have updated the entire project code information in columns F1 to the last column, totaling more than 150+ columns. In the second row, if a project is used, it will have a corresponding number proportion like 1 or 0.50, 0.30, etc., but the sum of these values should not exceed 1 (100%).

Can someone please guide me on how to find and delete the empty columns that do not meet this condition? this below code is running. but just taking time. ( because this is repeated process for more than 6000+ Employee. so my performance is down.

Thank you for your help.


VBA Code:
Sub DeleteEmptyProjectColumns_New()
    Dim ws As Worksheet
    Dim rng As Range
    Dim col As Range
    Dim lastColumn As Long

    Set ws = ThisWorkbook.Sheets("WS")
    lastColumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    Set rng = ws.Range(ws.Cells(1, 6), ws.Cells(1, lastColumn))

    For i = lastColumn To 6 Step -1
        If ws.Cells(2, i).Value = "" Or Not IsNumeric(ws.Cells(2, i).Value) Then
            ws.Columns(i).Delete
        End If
    Next i
End Sub

Emample screen shot.
1690875537954.png
 
I am having trouble getting my head around exactly what is required. It is obviously not a simple problem and may turn out to be too much time required for a free public forum like this.
I will have another look when I get a chance.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I am having trouble getting my head around exactly what is required. It is obviously not a simple problem and may turn out to be too much time required for a free public forum like this.
I will have another look when I get a chance.
This is my written code and just I have only two conditions here. I Dont know how make this with optimize the code...

Rich (BB code):
Sub CopyToJVSheet1()
    Dim wsLastRow As Long
    Dim wsLastCol As Long
    Dim jvLastRow As Long
    Dim ws As Worksheet
    Dim jvSheet As Worksheet
    Dim dataRange As Range
    Dim dim1Values As Variant
    Dim dim2Values As Variant
    Dim dim3Values As Variant
    Dim dim4Values As Variant
    Dim Dim1V As Variant
    Dim ledgerAccount As String
    Dim b1Value As String
    Dim Amt() As Variant, Lg() As Variant, DC() As Variant, CC() As Variant
    Dim amounts As Variant
    Dim firstDigit As Long
    Dim fs As Range
    Dim AM As Range

   
    Set ws = ThisWorkbook.Sheets("WS")
    Set jvSheet = ThisWorkbook.Sheets("JV")
    Set dimSheet = ThisWorkbook.Sheets("Dimension Summary")
    Set dimSummary = ThisWorkbook.Sheets("Dimension Summary")
   
    wsLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    wsLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
   
    If wsLastCol = 6 Then
        searchValue = ws.Range("F1").Value
        Set foundCell = dimSheet.Range("B:B").Find(what:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
       
        Dim dimValues() As Variant
        dimValues = foundCell.Offset(0, 1).Resize(1, 4).Value
       
        jvLastRow = jvSheet.Cells(jvSheet.Rows.Count, "I").End(xlUp).Row
        Dim startRow As Long: startRow = jvLastRow + 1
       
        ' Copying data in one shot to avoid loops
        jvSheet.Range("I" & startRow).Resize(wsLastRow - 1, 1).Value = ws.Range("E2:E" & wsLastRow).Value
        jvSheet.Range("H" & startRow).Resize(wsLastRow - 1, 1).Value = ws.Range("F2:F" & wsLastRow).Value
        jvSheet.Range("A" & startRow).Resize(wsLastRow - 1, 1).Value = ws.Range("D2:D" & wsLastRow).Value
        jvSheet.Range("J" & startRow).Resize(wsLastRow - 1, 1).Value = EmpID
       
        jvSheet.Range("D" & startRow).Resize(wsLastRow - 1, 1).Value = ws.Range("B1").Value
       
    For Each cell In jvSheet.Range("A" & jvLastRow + 1 & ":A" & jvSheet.Cells(jvSheet.Rows.Count, "I").End(xlUp).Row)
        firstDigit = Val(Left(cell.Value, 1))
        If firstDigit > 2 Then
            cell.Offset(0, 1).Formula = dimValues(1, 1) ' Apply the formula when first digit > 2
                cell.Offset(0, 2).Formula = dimValues(1, 2)
                cell.Offset(0, 4).Formula = dimValues(1, 3)
                cell.Offset(0, 5).Formula = dimValues(1, 4)
                cell.Offset(0, 9).Formula = EmpID
               
        Else
              cell.Offset(0, 1).Value = "" ' Leave the cell blank when first digit <= 2
            cell.Offset(0, 4).Formula = dimValues(1, 3)
            cell.Offset(0, 5).Formula = dimValues(1, 4)
            cell.Offset(0, 9).Formula = EmpID
            End If
    Next cell

Else
   
    jvLastRow = Sheets("JV").Cells(Rows.Count, 1).End(xlUp).Row
   
    b1Value = Sheets("WS").Cells(1, "B").Value
   
    For i = 2 To wsLastRow
           
        ledgerAccount = Sheets("WS").Cells(i, "D").Value
        Dim4V = dimSheet.Range("D2").Value
        Dim5V = dimSheet.Range("E2").Value
       
        If Left(ledgerAccount, 1) = "1" Or Left(ledgerAccount, 1) = "2" Then
            Sheets("JV").Cells(jvLastRow + 1, "D").Value = b1Value
            Sheets("JV").Cells(jvLastRow + 1, "A").Value = ledgerAccount
            Sheets("JV").Cells(jvLastRow + 1, "E").Value = Dim4V
            Sheets("JV").Cells(jvLastRow + 1, "F").Value = Dim5V
            Sheets("JV").Cells(jvLastRow + 1, "D").Value = b1Value
            Sheets("JV").Cells(jvLastRow + 1, "H").Value = Sheets("WS").Cells(i, "B").Value
            Sheets("JV").Cells(jvLastRow + 1, "I").Value = ws.Cells(i, "E").Value
            Sheets("JV").Cells(jvLastRow + 1, "J").Value = EmpID
           
            jvLastRow = jvLastRow + 1
        Else
           
            amounts = Sheets("WS").Range(Sheets("WS").Cells(i, "F"), Sheets("WS").Cells(i, wsLastCol)).Value
            Sheets("JV").Range("H" & jvLastRow + 1).Resize(UBound(amounts, 2)).Value = Application.Transpose(amounts)
            Proct = ws.Range(ws.Cells(1, "F"), ws.Cells(1, ws.Cells(1, Columns.Count).End(xlToLeft).Column)).Value


' Update values in column B based on VLOOKUP from "Dim Summary" sheet
Set dimSummary = ThisWorkbook.Sheets("Dimension Summary") ' Change "Dim Summary" to your actual worksheet name

For j = 1 To UBound(Proct, 2)
    Dim lookupValue As Variant
    lookupValue = Proct(1, j)
   
    Dim resultValue As Variant
   
    resultValue = Application.VLookup(lookupValue, dimSummary.Range("B:C"), 2, False)
    resultValue1 = Application.VLookup(lookupValue, dimSummary.Range("B:D"), 3, False)
    resultValue2 = Application.VLookup(lookupValue, dimSummary.Range("B:E"), 4, False)
    resultValue3 = Application.VLookup(lookupValue, dimSummary.Range("B:F"), 5, False)
   
    If Not IsError(resultValue) Then
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "B").Value = resultValue
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "C").Value = resultValue1
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "E").Value = resultValue2
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "F").Value = resultValue3
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "J").Value = EmpID
       
    Else
        ThisWorkbook.Sheets("JV").Cells(jvLastRow + j, "B").Value = "Not Found"
    End If
Next j
                   
            For j = 0 To UBound(amounts, 2)
                Sheets("JV").Cells(jvLastRow + j + 1, "A").Value = ledgerAccount
                Sheets("JV").Cells(jvLastRow + j + 1, "D").Value = b1Value
                Sheets("JV").Cells(jvLastRow + j + 1, "I").Value = ws.Cells(i, "E").Value
                Sheets("JV").Cells(jvLastRow + j + 1, "J").Value = EmpID
               
            Next j
            jvLastRow = jvLastRow + UBound(amounts, 2)
        End If
    Next i
End If
End Sub
 
Upvote 0
This is now a duplicate of: Help to Optimize Macro Code for Faster Performance

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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