Code to Resize cells with formula in the first cell

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello experts

I have this data in my sheet where column E is derived from a code. There is a formula in cell F7 which is dragged down to 3000 rows.

The formula is =VLOOKUP(E6,$A$2:$A$3000,1,0). If the cell in column E19 is blank, then I want the cell F19 to be blank. I tried this formula =IFERROR(VLOOKUP(E6,$A$2:$A$3000,1,0),"")

The problem is when the next line of code selects the cells with an error, it selects all the cells with error, even the cells which are blank in column E. Is it possible to write a code to insert the formula in cell F6 and resize the cells with the cells with value in the column E.?
Book1
ABCDEFG
1
2January
3February
4March
5April
6MayMercury#N/A
7JuneFebruaryFebruary
8JulyMarchMarch
9AugustAprilApril
10SeptemberMayMay
11OctoberJuneJune
12NovemberJulyJuly
13DecemberAugustAugust
14SundaySeptemberSeptember
15MondayOctoberOctober
16TuesdayNovemberNovember
17WednesdayDecemberDecember
18SundaySunday
19#N/A
20#N/A
21#N/A
22#N/A
23#N/A
24#N/A
25#N/A
List of Ledgers
Cell Formulas
RangeFormula
F6:F25F6=VLOOKUP(E6,$A$2:$A$4922,1,0)
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
you could try this which will show blank rather than N/A if "E" cells are blank

Excel Formula:
=IFERROR(If(E6="","",VLOOKUP(E6,$A$2:$A$3000,1,0)),"")
 
Upvote 0
@RAJESH1960, if this thread is based on your recent previous threads, I have reworked the previous code into the following code:

VBA Code:
Sub MoveDataToDifferentSheetsV3()
'
    Application.ScreenUpdating = False                                                              ' Turn ScreenUpdating off
'
    Dim StartTime                           As Single
    StartTime = Timer                                                                               ' Start the stop watch
'
    Dim DictionaryRow                       As Long
    Dim FormulaNumber                       As Long
    Dim List_of_LedgersColumnA_LastRow      As Long, ParticularsLastRow                     As Long, SourceLastRow  As Long
    Dim SourceHeaderColumnNumber            As Long, SourceHeaderRow                        As Long
    Dim cell                                As Range
    Dim CodeCompletionTime                  As Single
    Dim SourceLastColumnLetter              As String
    Dim VlookupStartAddress                 As String
    Dim DataDictionary                      As Variant
    Dim FormulaArray                        As Variant, List_of_LedgersFormulaResultsArray  As Variant, PasteDataParticularsDataArray   As Variant
    Dim SourceWS                            As Worksheet
'
    Set SourceWS = Sheets("PasteData")                                                              ' <--- Set this to the source sheet
    VlookupStartAddress = "$A$6"                                                                    ' <--- Set this to the proper start address
'
    Sheets("MasterData").Range("B2", Sheets("MasterData").Range("B2").End(xlDown)).ClearContents    ' Clear old data from Sheets("MasterData")
    Sheets("List of Ledgers").Columns("E:F").ClearContents                                          ' Clear old data from Sheets("List of Ledgers")
'
    With SourceWS
        SourceLastColumnLetter = Split(Cells(1, (.Cells.Find("*", , xlFormulas, , _
                xlByColumns, xlPrevious).Column)).Address, "$")(1)                                  '   Get last column letter used in the source sheet
        SourceLastRow = .Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row - 1              '   Get the last source row of data minus the total row
'
        With .Range("A1:" & SourceLastColumnLetter & SourceLastRow)                                 '   Look through the source sheet for the header row
            Set cell = .Find("Particulars", LookIn:=xlValues)                                       '       Find the header called 'Particulars'
'
            If Not cell Is Nothing Then                                                             '       If 'Particulars' is found then ...
                SourceHeaderRow = cell.Row                                                          '           Save the row # into SourceHeaderRow
                SourceHeaderColumnNumber = cell.Column                                              '           Save the Column # into SourceHeaderColumn
            End If
        End With
'
        ParticularsLastRow = .Cells(SourceHeaderRow + 1, SourceHeaderColumnNumber).End(xlDown).Row  ' Get last Row of consecutive data in 'Particulars' column
        If ParticularsLastRow = SourceLastRow Then ParticularsLastRow = ParticularsLastRow - 1      ' If no blanks found in column before the total line then subtract 1
'
        PasteDataParticularsDataArray = .Range(.Cells(SourceHeaderRow + 1, SourceHeaderColumnNumber), _
            .Cells(ParticularsLastRow, SourceHeaderColumnNumber))                               ' Save Data to be pasted into 2D 1 Based PasteDataParticularsDataArray
    End With
'
    With Sheets("List of Ledgers")
        List_of_LedgersColumnA_LastRow = .Range("A" & Rows.Count).End(xlUp).Row                         ' Get last row used in Sheets("List of Ledgers") column A
'
        .Range("E6").Resize(UBound(PasteDataParticularsDataArray, 1)) = PasteDataParticularsDataArray   'Display PasteDataParticularsDataArray to Sheets("List of Ledgers")
'
        ReDim FormulaArray(1 To UBound(PasteDataParticularsDataArray, 1))                               ' Set the number of rows for the FormulaArray
'
        For FormulaNumber = 1 To UBound(PasteDataParticularsDataArray, 1)                               ' Loop to put formulas into FormulaArray
            FormulaArray(FormulaNumber) = "=VLOOKUP(E" & 5 + FormulaNumber & "," & _
                    VlookupStartAddress & ":$A$" & List_of_LedgersColumnA_LastRow & ",1,0)"             '   Save Formula into FormulaArray
        Next                                                                                            ' Loop back
'
        .Range("F6").Resize(UBound(FormulaArray, 1)) = FormulaArray                                     'Display FormulaArray to Sheets("List of Ledgers")
'
        List_of_LedgersFormulaResultsArray = .Range("F6:F" & Range("F6").End(xlDown).Row)       ' Load formula column Results from Sheets("List of Ledgers") to array
'
        DataDictionary = .Range("E6", .Cells(Rows.Count, "F").End(xlUp))                            '   Create DataDictionary
    End With
'
    With CreateObject("Scripting.Dictionary")
        For DictionaryRow = 1 To UBound(DataDictionary)                                             '   Loop through each row of DataDictionary
            If Not .Exists(DataDictionary(DictionaryRow, 1)) And _
                    IsError(List_of_LedgersFormulaResultsArray(DictionaryRow, 1)) Then              '       If uniue value found &  ...
'                                                                                                   '           Error found in column to the right then ...
                .Add DataDictionary(DictionaryRow, 1), Array(DataDictionary(DictionaryRow, 2))      '           add unique value to DataDictionary
            End If
        Next                                                                                        '   Loop back
'
        If .Count > 0 Then                                                                          '   If dictionary count > 0 then ...
            Sheets("MasterData").Range("B2").Resize(.Count) = Application.Transpose(.keys)          '       Display unique values on Sheets("MasterData")
        Else
            CodeCompletionTime = Timer - StartTime                                                  '       Stop the stop watch
            MsgBox "All Ledgers Available."                                                         '       Display message to user
            GoTo Continue                                                                           '       Jump to Continue:
        End If
    End With
'
    CodeCompletionTime = Timer - StartTime                                                          ' Stop the stop watch
'
Continue:
    Application.ScreenUpdating = True                                                               ' Turn ScreenUpdating back on
'
    CodeCompletionTime = Format(CodeCompletionTime, ".#####")                                       ' Prevent scientific notation results
    Debug.Print "Time to complete MoveDataToDifferentSheets = " & CodeCompletionTime & " seconds."  ' Display the time elapsed to the user (Ctrl-G)
'
    Application.Speech.Speak "This code completed in, , , " & CodeCompletionTime & " seconds."      ' Provide audio result
End Sub

Let us know if I have messed up your intentions.
 
Upvote 0
It is just amazing. But for one thing. The sheet MasterData is including the grand total in the list. Can you please remove that from the list.
Query Code to get Ledger names from another sheet.xlsm
B
1PARTY LEDGER NAME
2Mercury
3Thursday
4Friday
5Saturday
6Grand Total
MasterData
 
Upvote 0
And also in column E of List of Ledgers sheet.
 
Upvote 0
you could try this which will show blank rather than N/A if "E" cells are blank

Excel Formula:
=IFERROR(If(E6="","",VLOOKUP(E6,$A$2:$A$3000,1,0)),"")
gordsky. Thanks. But I want the error to show so that the adjoining cells of the error cells need to be copied to the next sheet. If it shows blank, it will select all the blank cells of the 3000 rows in the adjoining cells.
 
Upvote 0
It is just amazing. But for one thing. The sheet MasterData is including the grand total in the list. Can you please remove that from the list.

My mistake, I forgot that my original check for lastrow already had 1 subtracted from it, so when I did a comparison to it, the comparison fails. It is fixed now.

The comparison line should have been:
VBA Code:
        If ParticularsLastRow = SourceLastRow + 1 Then ParticularsLastRow = ParticularsLastRow - 1    ' If no blanks found in column before the total line then subtract 1

VBA Code:
Sub MoveDataToDifferentSheetsV3a()
'
    Application.ScreenUpdating = False                                                              ' Turn ScreenUpdating off
'
    Dim StartTime                           As Single
    StartTime = Timer                                                                               ' Start the stop watch
'
    Dim DictionaryRow                       As Long
    Dim FormulaNumber                       As Long
    Dim List_of_LedgersColumnA_LastRow      As Long, ParticularsLastRow                     As Long, SourceLastRow  As Long
    Dim SourceHeaderColumnNumber            As Long, SourceHeaderRow                        As Long
    Dim cell                                As Range
    Dim CodeCompletionTime                  As Single
    Dim SourceLastColumnLetter              As String
    Dim VlookupStartAddress                 As String
    Dim DataDictionary                      As Variant
    Dim FormulaArray                        As Variant, List_of_LedgersFormulaResultsArray  As Variant, PasteDataParticularsDataArray   As Variant
    Dim SourceWS                            As Worksheet
'
    Set SourceWS = Sheets("PasteData")                                                              ' <--- Set this to the source sheet
    VlookupStartAddress = "$A$6"                                                                    ' <--- Set this to the proper start address
'
    Sheets("MasterData").Range("B2", Sheets("MasterData").Range("B2").End(xlDown)).ClearContents    ' Clear old data from Sheets("MasterData")
    Sheets("List of Ledgers").Columns("E:F").ClearContents                                          ' Clear old data from Sheets("List of Ledgers")
'
    With SourceWS
        SourceLastColumnLetter = Split(Cells(1, (.Cells.Find("*", , xlFormulas, , _
                xlByColumns, xlPrevious).Column)).Address, "$")(1)                                  '   Get last column letter used in the source sheet
        SourceLastRow = .Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row - 1              '   Get the last source row of data minus the total row
'
        With .Range("A1:" & SourceLastColumnLetter & SourceLastRow)                                 '   Look through the source sheet for the header row
            Set cell = .Find("Particulars", LookIn:=xlValues)                                       '       Find the header called 'Particulars'
'
            If Not cell Is Nothing Then                                                             '       If 'Particulars' is found then ...
                SourceHeaderRow = cell.Row                                                          '           Save the row # into SourceHeaderRow
                SourceHeaderColumnNumber = cell.Column                                              '           Save the Column # into SourceHeaderColumn
            End If
        End With
'
        ParticularsLastRow = .Cells(SourceHeaderRow + 1, SourceHeaderColumnNumber).End(xlDown).Row  ' Get last Row of consecutive data in 'Particulars' column
        If ParticularsLastRow = SourceLastRow + 1 Then ParticularsLastRow = ParticularsLastRow - 1    ' If no blanks found in column before the total line then subtract 1
'
        PasteDataParticularsDataArray = .Range(.Cells(SourceHeaderRow + 1, SourceHeaderColumnNumber), _
            .Cells(ParticularsLastRow, SourceHeaderColumnNumber))                               ' Save Data to be pasted into 2D 1 Based PasteDataParticularsDataArray
    End With
'
    With Sheets("List of Ledgers")
        List_of_LedgersColumnA_LastRow = .Range("A" & Rows.Count).End(xlUp).Row                         ' Get last row used in Sheets("List of Ledgers") column A
'
        .Range("E6").Resize(UBound(PasteDataParticularsDataArray, 1)) = PasteDataParticularsDataArray   'Display PasteDataParticularsDataArray to Sheets("List of Ledgers")
'
        ReDim FormulaArray(1 To UBound(PasteDataParticularsDataArray, 1))                               ' Set the number of rows for the FormulaArray
'
        For FormulaNumber = 1 To UBound(PasteDataParticularsDataArray, 1)                               ' Loop to put formulas into FormulaArray
            FormulaArray(FormulaNumber) = "=VLOOKUP(E" & 5 + FormulaNumber & "," & _
                    VlookupStartAddress & ":$A$" & List_of_LedgersColumnA_LastRow & ",1,0)"             '   Save Formula into FormulaArray
        Next                                                                                            ' Loop back
'
        .Range("F6").Resize(UBound(FormulaArray, 1)) = FormulaArray                                     'Display FormulaArray to Sheets("List of Ledgers")
'
        List_of_LedgersFormulaResultsArray = .Range("F6:F" & Range("F6").End(xlDown).Row)       ' Load formula column Results from Sheets("List of Ledgers") to array
'
        DataDictionary = .Range("E6", .Cells(Rows.Count, "F").End(xlUp))                            '   Create DataDictionary
    End With
'
    With CreateObject("Scripting.Dictionary")
        For DictionaryRow = 1 To UBound(DataDictionary)                                             '   Loop through each row of DataDictionary
            If Not .Exists(DataDictionary(DictionaryRow, 1)) And _
                    IsError(List_of_LedgersFormulaResultsArray(DictionaryRow, 1)) Then              '       If uniue value found &  ...
'                                                                                                   '           Error found in column to the right then ...
                .Add DataDictionary(DictionaryRow, 1), Array(DataDictionary(DictionaryRow, 2))      '           add unique value to DataDictionary
            End If
        Next                                                                                        '   Loop back
'
        If .Count > 0 Then                                                                          '   If dictionary count > 0 then ...
            Sheets("MasterData").Range("B2").Resize(.Count) = Application.Transpose(.keys)          '       Display unique values on Sheets("MasterData")
        Else
            CodeCompletionTime = Timer - StartTime                                                  '       Stop the stop watch
            MsgBox "All Ledgers Available."                                                         '       Display message to user
            GoTo Continue                                                                           '       Jump to Continue:
        End If
    End With
'
    CodeCompletionTime = Timer - StartTime                                                          ' Stop the stop watch
'
Continue:
    Application.ScreenUpdating = True                                                               ' Turn ScreenUpdating back on
'
    CodeCompletionTime = Format(CodeCompletionTime, ".#####")                                       ' Prevent scientific notation results
    Debug.Print "Time to complete MoveDataToDifferentSheets = " & CodeCompletionTime & " seconds."  ' Display the time elapsed to the user (Ctrl-G)
'
    Application.Speech.Speak "This code completed in, , , " & CodeCompletionTime & " seconds."      ' Provide audio result
End Sub
 
Upvote 0
Solution
I too compared the codes. But I couldn't find any difference. Where did you edit the code?
 
Upvote 0
I just edited the post to show the line that needed correcting.
 
Upvote 0
It is working perfect now. Thanks JohnnyL.?
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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