Copy and paste macro can’t remove formatting.

Geosphere

New Member
Joined
Jul 16, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,


I’m very new to excel macro's

What I am trying to do is take Info from a BOM with Reference, Value and Part numbers from sheet1 and move the data into sheet2 that I can then use to print a sticker.

when I run my macro it opens a search box, I search for a unique part value then it will copy and paste 3 cells from the same row to a sheet2 A1,A2,A3 and then move them in a way, so I am able to print them to a sticker.
I have found some random bits of code scattered around the web that dose 90% of what I want but I can’t seem the last few bits to work, and I hope someone could help me out.

I would like to remove the formatting when it copies the data, I have tried some of the paste special options, but they never work.

I guess my only other way I could make it work is so it formats sheet when I has finished the copy?

If anyone could point me in the right direction it would really help me out.



Thanks


VBA Code:
Sub customcopy()

Dim strsearch As String
Dim lastline As Integer
Dim tocopy As Integer

ActiveWorkbook.Worksheets("Sheet1").Select
strsearch = CStr(InputBox("enter the string to search for"))
lastline = Range("A65536").End(xlUp).Row
j = 1

For i = 1 To lastline
    For Each c In Range("A" & i & ":Z" & i)
        If c.Text = strsearch Then
            tocopy = 1
        End If
    Next c
    If tocopy = 1 Then
           Range(Cells(i, "A"), Cells(i, "C")).Copy Destination:=Sheets("Sheet2").Cells(j, "A")
        
          
        j = j + 1
    End If
tocopy = 0
Next i

ActiveWorkbook.Worksheets("Sheet2").Select
 Range("A1").Cut Destination:=Range("B3")
 Range("B1").Cut Destination:=Range("B4")
 Range("C1").Cut Destination:=Range("B5")
 


End Sub
 

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,)
Hello Geosphere

Copy/pasting of the cell values only will work. Change this line:

VBA Code:
Range(Cells(i, "A"), Cells(i, "C")).Copy Destination:=Sheets("Sheet2").Cells(j, "A")

to
VBA Code:
Range(Cells(i, "A"), Cells(i, "C")).Copy
Sheets("Sheet2").Cells(j, "A").PasteSpecial xlValues

It needs to be on two lines as shown.
It will copy /paste cell values only, nothing else.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
You can simply set the destination value to the source value thus reducing the number of steps.

Try the following:

VBA Code:
Sub customcopy_alt()

Dim strSearch As String
Dim lstLine As Integer, i As Integer
Dim srcSh As Worksheet, dstSh As Worksheet
Dim Found As Range

Set srcSh = Worksheets("Sheet1")
Set dstSh = Worksheets("Sheet2")

strSearch = CStr(InputBox("enter the string to search for"))
lstLine = Range("A" & Rows.Count).End(xlUp).Row

j = 1

For i = 1 To lstLine
    Set Found = srcSh.Range("A" & i).Resize(, 26).Find(strSearch)
    If Not Found Is Nothing Then
        dstSh.Range("A" & j).Resize(, 3).Value = srcSh.Range("A" & i).Resize(, 3).Value
        j = j + 1
    End If
Next i

'========================================
' not sure what the purpose of this section is!!?
'========================================

'ActiveWorkbook.Worksheets("Sheet2").Select
' Range("A1").Cut Destination:=Range("B3")
' Range("B1").Cut Destination:=Range("B4")
' Range("C1").Cut Destination:=Range("B5")
 
End Sub
 
Upvote 0
Thanks Dave your code strips the formatting perfect, but it only seems to work if I run the macro when in Sheet1.


The code at the bottom moves it into separated rows as below
Locations:R1, R3, R4, R5, R6, R8, R10, R14, R15, R16
Value:10K 1% 0805
MF Part number:WCR0805-10KFI
 
Upvote 0
Hi @Geosphere , sorry this variable needs to be set with explicit sheet reference:

VBA Code:
lstLine = srcSh.Range("A" & Rows.Count).End(xlUp).Row

What would be much better is to use the sheet codenames, thereby removing the need to declare and set the variables. Preferable use relevant names, but for demonstration purposes I've stuck with srcSh and dstSh.

1658093359819.png



VBA Code:
Sub customcopy_alt()

Dim strSearch As String
Dim lstLine As Integer, i As Integer
Dim Found As Range

strSearch = CStr(InputBox("enter the string to search for"))
lstLine = srcSh.Range("A" & Rows.Count).End(xlUp).Row

j = 1

For i = 1 To lstLine
    Set Found = srcSh.Range("A" & i).Resize(, 26).Find(strSearch)
    If Not Found Is Nothing Then
        dstSh.Range("A" & j).Resize(, 3).Value = srcSh.Range("A" & i).Resize(, 3).Value
        j = j + 1
    End If
Next i

'========================================
' not sure what the purpose of this section is!!?
'========================================

'With dstSh
' .Range("A1").Cut Destination:=.Range("B3")
' .Range("B1").Cut Destination:=.Range("B4")
' .Range("C1").Cut Destination:=.Range("B5")
'End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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