Copy part of a cell which is a varible in both length and content using VBA

WardenBerret

New Member
Joined
Mar 2, 2016
Messages
9
Hello!

this will be a tough one... :confused:

So I have workbook1 (wbThis) and workbook2 (wbTarget) and in wbThis' sheet I have some cells which are filled. These cells have the format of:

From A6
P2123: Begin procedure
A7
P1234: Code
A8
P4456-6: Document
|
|
|
|
|
|
|
V
A27

It continues like that. Now I have 2 issues.

1.
I want to copy the PXXXxX: (small x is like an arbitrary summation of - or _ or > etc etc) codes to wbTarget. This code varies as you can see but there will always be a "P" in the beginning and a ":" at the end. Regarding this, I have tried with the code:
Code:
Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim rowRng As Integer
Dim targetRowRng As Integer

For rowRng 6 To 27
   For targetRowRng 14 To 35 
      If Left((A:rowRng).Value,1) = "P" Then
          wbThis.Sheets("Sheet1").Range(A:rowRng).Copy
          wbTarget.Sheets("Sheet1").Range(E:targetRowRng).PasteSpecial
      End If

However, as you may have noticed, I have not wrote the code where I want it to end on ":" and copy everything inbetween (incl "P" and ":").. I don't know how to code this and I would like your help. And also, a few comments whether the code is efficient will be highly appreciated.

2.
Now there is no way to that all cells of wbThis' range of A6 -> A27 will be filled everytime there's a new document and because I don't want unnecessary copy/pasting I want the script to stop the copy/pasting of PXXXxX: if there is no P. (if there is no P there is no PXXXxX and then the cell will be empty and therefore redundant)
I guess you can code this using else to the If statement above:
Code:
Else Pass
(i'm still learning vba so I'm not too familiar with statements)

These are my problems, and i would love some guidance.

Thanks.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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