Link to home
Start Free TrialLog in
Avatar of Euro5
Euro5Flag for United States of America

asked on

vba find the last empty column

I need to use the following code, but it puts the formula in column H and I need the formula to be in the first blank column.
I need to them copy that value to Column A and remove that formula from the other column.
Can anyone help?


Sub August()

Dim lngLastRowD As LongPtr
Dim c As Long, lr As LongPtr

Application.ScreenUpdating = False
Sheets("Shipment Data").Activate
Application.Calculation = xlCalculationManual

With Sheets("Shipment Data")
lngLastRowD = Sheets("Shipping").Range("A800000").End(xlUp).Row
.Range("H2:H" & lngLastRowD).Formula = "=IFERROR(trim(A2)*1,A2)"
.Range("H2:H" & lngLastRowD).Copy
.Range("H2:H" & lngLastRowD).PasteSpecial xlValues

lngLastRowD = Sheets("Shipment Data").Range("A800000").End(xlUp).Row
.Range("E2:E" & lngLastRowD).Formula = "=H2"
.Range("E2:E" & lngLastRowD).Copy
.Range("E2:E" & lngLastRowD).PasteSpecial xlValues


End With
Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = False
End Sub

Open in new window

Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

can you provide an example workbook
Avatar of Euro5

ASKER

The columns vary at every run.
TEST.xlsx
I'll take a look later
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Because your data is set out in a tabular format you can simplify the code using CurrentRegion, i.e a Range object that represents the current region. The current region is a range bounded by any combination of blank rows and blank columns. You code seems to be referring to two sheets, is that what you intended?

So

Option Explicit
Sub August()

Dim lngLastRowD As LongPtr
Dim rRng As Range
Dim c As Long, lr As LongPtr
''/// you don't need to activate
'Sheets("Shipment Data").Activate

With Sheets("Shipment Data")
''/// this gets the row from a different sheet, is this correct?
Set rRng = Sheets("Shipping").Cells(1, 1).CurrentRegion
lngLastRowD = rRng.Rows.Count
''/// this works on Shipment Data?
.Range("H2:H" & lngLastRowD).Formula = "=IFERROR(trim(A2)*1,A2)"
.Range("H2:H" & lngLastRowD).Copy
.Range("H2:H" & lngLastRowD).PasteSpecial xlValues
lngLastRowD = .Cells(1, 1).CurrentRegion
''/// this works on Shipment Data?
.Range("E2:E" & lngLastRowD).Formula = "=H2"
.Range("E2:E" & lngLastRowD).Copy
.Range("E2:E" & lngLastRowD).PasteSpecial xlValues
End With

End Sub

Open in new window

I keep trying to interpret what you are doing from the code, but it doesn't make much sense. Can you explain what you want to do and attach an example workbook that represents the workbook that you are using, the current example does not if you have two sheets ShipmentData and Shipping

Option Explicit

Sub August()

    Dim lngLastRowD As LongPtr
    Dim c As Long, lr As LongPtr
    Dim rRng As Range

    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual

        With Sheets("Shipment Data")
            Set rRng = Range("A1").CurrentRegion
            c = rRng.Column.Column.Count + 1
            lr = rRng.Rows.Count
            ''///I think this is a mistake, if not explain what you use it for
            'lngLastRowD = Sheets("Shipping").Range("A800000").End(xlUp).Row
            .Cells(lr, c).Formula = "=IFERROR(trim(A2)*1,A2)"
            .Cells(lr, c).Copy
            .Cells(lr, c).PasteSpecial xlValues


            'lngLastRowD = Sheets("Shipment Data").Range("A800000").End(xlUp).Row
            ''/// what is this for?
            ''/// it appears to enter the formula in the whole range
            .Range("E2:E" & lngLastRowD).Formula = "=H2"
            .Range("E2:E" & lngLastRowD).Copy
            .Range("E2:E" & lngLastRowD).PasteSpecial xlValues


        End With
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = False
    End With
End Sub

Open in new window

Avatar of Euro5

ASKER

Wonderful, thank you!!
You're welcome. Glad to help.
Avatar of Euro5

ASKER

Subodh Tiwari (Neeraj)
Can we delete all the values in the column with the formula?
It has to be dynamic, I will never know what the last column is.
Thanks!!
Like this...
Dim lc As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
'since it is the last column with data, you can simply delete it like this
Columns(lc).Delete

'OR use this to clear the column contents
'Columns(lc).ClearContents

Open in new window