VBA Question of adding 2 Columns at the end of data set and filling Columns down with formulas

jgome21

New Member
Joined
Nov 26, 2017
Messages
13
Hi All - I am a newbie at writing VBA. I have a set of data ranging thru columns A thru O. I am trying to add to new columns at the end titled GCP(Column P) and PLCC (Column Q).

Then I want to fill the two columns with these formulas.
ColumnP:=IF(ISERR(SEARCH("GCP",$D2,1))," ","GCP")
ColumnQ:=F2&P2

So far I have the coding below but isn't working properly. Can someone please assist? Thank you.

Sub CCFormat()


Columns("P:Q").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove 'or xlFormatFromRightOrBelow
Range("P2") = "=IF(ISERR(SEARCH("GCP",$D2,1))," ","GCP")": Range("P2:P" & LastRow).FillDown
Range("Q2") = "=F2&P2": Range("q2:P" & LastRow).FillDown
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
Hi jgome21,

Welcome to MrExcel!!

Try this while on the sheet in question:

Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    
    Application.ScreenUpdating = False
    
    On Error Resume Next 'In case there's no data on the tab
        lngLastRow = Range("A:O").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        If lngLastRow >= 2 Then
            Range("P2:P" & lngLastRow).Formula = "=IF(ISERR(SEARCH(""GCP"",$D2,1)),"" "",""GCP"")"
            Range("Q2:Q" & lngLastRow).Formula = "=F2&P2"
        End If
    On Error GoTo 0
    
    Application.ScreenUpdating = True
        
End Sub

Regards,

Robert
 

jgome21

New Member
Joined
Nov 26, 2017
Messages
13
Hi jgome21,

Welcome to MrExcel!!

Try this while on the sheet in question:

Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    
    Application.ScreenUpdating = False
    
    On Error Resume Next 'In case there's no data on the tab
        lngLastRow = Range("A:O").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        If lngLastRow >= 2 Then
            Range("P2:P" & lngLastRow).Formula = "=IF(ISERR(SEARCH(""GCP"",$D2,1)),"" "",""GCP"")"
            Range("Q2:Q" & lngLastRow).Formula = "=F2&P2"
        End If
    On Error GoTo 0
    
    Application.ScreenUpdating = True
        
End Sub

Regards,

Robert

Thank you very much. It worked.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,307
Messages
5,641,442
Members
417,209
Latest member
Agbarker

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
Top