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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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