Solved

vba find the last empty column

Posted on 2016-08-18
10
88 Views
Last Modified: 2016-08-19
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

0
Comment
Question by:Euro5
  • 4
  • 3
  • 3
10 Comments
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41761326
can you provide an example workbook
0
 

Author Comment

by:Euro5
ID: 41761552
The columns vary at every run.
TEST.xlsx
0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41761994
I'll take a look later
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41762001
Does this work for you?

Sub August()
Dim lngLastRowD As LongPtr
Dim c As Long, lr As LongPtr

With Application
   .Calculation = xlCalculationManual
   .EnableEvents = False
   .ScreenUpdating = False
End With

Sheets("Shipment Data").Activate

With Sheets("Shipment Data")
   lngLastRowD = Sheets("Shipping").Range("A800000").End(xlUp).Row
   c = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
   .Range(.Cells(2, c), .Cells(lngLastRowD, c)).Formula = "=IFERROR(trim(A2)*1,A2)"
   .Range(.Cells(2, c), .Cells(lngLastRowD, c)).Value = .Range(.Cells(2, c), .Cells(lngLastRowD, c)).Value
   
   lngLastRowD = Sheets("Shipment Data").Range("A800000").End(xlUp).Row
   .Range(.Cells(2, c), .Cells(lngLastRowD, c)).Copy
   .Range("E2:E" & lngLastRowD).PasteSpecial xlPasteValues
End With

With Application
   .Calculation = xlCalculationAutomatic
   .EnableEvents = True
   .ScreenUpdating = True
End With
End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41762027
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41762739
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

0
 

Author Closing Comment

by:Euro5
ID: 41762983
Wonderful, thank you!!
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41762985
You're welcome. Glad to help.
0
 

Author Comment

by:Euro5
ID: 41763003
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!!
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41763350
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now