Find, Copy Partial String, and Paste in New Row Excel VBA

LWell

New Member
Joined
Jun 1, 2015
Messages
20
Hello,

I have a workbook with two sheets. One of them is updated monthly with a list of contracts and contract details. However, that sheet is generated automatically by an external program and is quite cluttered and difficult to navigate. My goal is to create a simplified list of every contract in the other sheet.

The issue is that the cluttered sheet displays the names of the contracts in Column B as "CONTRACT NAME: XXXX" whereas I really only want to copy and paste "XXXX". I would greatly appreciate any help in searching Column B for the contract name, copying just the "XXXX" portion, and pasting them subsequently in Column A of the other sheet, starting on Row 12. I hope that makes sense; thank you all very much for any help!

Best,
 
I got it! Thanks so much for your help! Just a notation issue that making it not want to cooperate.

What I used:

Code:
Sub Contract_Num()
  Dim lRowCount&

  lRowCount = Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row
  
  With Sheets("Sheet3").Range("A12").Resize(lRowCount)
    .Formula = "=IF(LEFT([COLOR=#ff0000]'[/COLOR]Sheet2[COLOR=#ff0000]'[/COLOR]!B1,8)=""CONTRACT"",MID([COLOR=#ff0000]'[/COLOR]Sheet2[COLOR=#ff0000]'[/COLOR]!B1,18,12),"""")"
    .Value = .Value
    .Columns("A").Sort key1:=Range("A12"), _
       order1:=xlAscending
  End With
  
End Sub
 
Last edited:
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Now, if I wanted to put this in a command button that is located on a different sheet (Sheet1 if you will), would I simply need to activate Sheet3 before the 1RowCount line?
 
Upvote 0
Now, if I wanted to put this in a command button that is located on a different sheet (Sheet1 if you will), would I simply need to activate Sheet3 before the 1RowCount line?

I am unsure about that.

Hate to just say just give it a try.

A Forms button should work, but I've have trouble with the sort when run from other sheets.

Is there a reason for the command button other than a Forms button or running from a shape on the sheet?

Howard
 
Upvote 0
Is there a reason for the command button other than a Forms button or running from a shape on the sheet?

The purpose of the button is ease of use. I will be handing off this project (once I am finished) to someone with very basic Excel skills. I want to make the process of running macros as simple as possible for them. So, no, it is not completely necessary but I believe it would make the process easier for them.

I will continue to look for an answer to this problem. Howard, thank you so much for your help on this.
 
Upvote 0
I got it to work how I wanted. For posterity purposes, the command button code is:

Code:
Private Sub CommandButton1_Click()
    Worksheets("Sheet3").Activate
    Call Contract_Num
End Sub

And the Contract_Num macro is:

Code:
Sub Contract_Num()
  Dim lRowCount&
  lRowCount = Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row
  With Sheets("Sheet3").Range("A12").Resize(lRowCount)
    .Formula = "=IF(LEFT('Sheet2'!B1,15)=""CONTRACT NUMBER"",MID('Sheet2'!B1,20,4),"""")"
    .Value = .Value
    .Columns("A").Sort key1:=Range("A12"), _
       order1:=xlAscending
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,408
Messages
6,124,727
Members
449,185
Latest member
ekrause77

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