Copy values only without specific column formats VBA

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
519
Office Version
  1. 2019
Platform
  1. Windows
Welcome. I have this code to filter worksheet 1 and copy certain columns to the target columns on worksheet 2. It works very well for me. The only drawback is that it copies formats. What I want is
Editing it, for example, makes it possible to copy values only without formatting, whether fonts or formulas.

VBA Code:
Sub test()
Dim WS As Worksheet: Set WS = Sheets("Sheet1")
Dim r  As Worksheet: Set r = Worksheets("Sheet2")

    Sup = MsgBox("Copy data", _
    vbCritical + vbYesNo, "Confirmation")
        
        If Sup = vbYes Then

Application.ScreenUpdating = False
With WS
 If .AutoFilterMode Then .AutoFilterMode = False

With WS.Range("A1:J1")
    .AutoFilter 3, "Paris"

lr = WS.Columns("A:G").Find(What:="*", _
     SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    
    Set Rng = WS.Range("A" & lr & ":G" & lr).SpecialCells(xlCellTypeVisible)
    If WorksheetFunction.Subtotal(3, WS.Columns(3)) > 1 Then
        r.Range("A2:G" & r.Rows.Count).ClearContents

With Rng

    rngA = split("A,B,C,D,E,F,G", ",")
    
    rngB = split("B,A,E,C,D,F,G", ",")

For i = LBound(rngA) To UBound(rngA)
        WS.Range(rngA(i) & "2:" & rngA(i) & lr).Copy r.Range(rngB(i) & "2")
    Next i
 End With
 
End If
  .AutoFilter
End With
End With

 End If
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Untested:
Try replacing this part:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
        WS.Range(rngA(i) & "2:" & rngA(i) & lr).Copy r.Range(rngB(i) & "2")
    Next i
 End With

with this:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
    With WS.Range(rngA(i) & "2:" & rngA(i) & lr)
        r.Range(rngB(i) & "2").Resize(.Rows.Count, Columns.Count).Value = .Value
    End With
Next i
 
Upvote 0
Untested:
Try replacing this part:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
        WS.Range(rngA(i) & "2:" & rngA(i) & lr).Copy r.Range(rngB(i) & "2")
    Next i
 End With

with this:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
    With WS.Range(rngA(i) & "2:" & rngA(i) & lr)
        r.Range(rngB(i) & "2").Resize(.Rows.Count, Columns.Count).Value = .Value
    End With
Next i
Unfortunately it doesn't work
 
Upvote 0
Unless I am mistaken you are only copying 1 column at a time so get rid of the column resize in @Akuini's suggestion:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
    With WS.Range(rngA(i) & "2:" & rngA(i) & lr)
        r.Range(rngB(i) & "2").Resize(.Rows.Count).Value = .Value
    End With
Next i
 
Upvote 0
Unless I am mistaken you are only copying 1 column at a time so get rid of the column resize in @Akuini's suggestion:
VBA Code:
For i = LBound(rngA) To UBound(rngA)
    With WS.Range(rngA(i) & "2:" & rngA(i) & lr)
        r.Range(rngB(i) & "2").Resize(.Rows.Count).Value = .Value
    End With
Next i
Unfortunately, things didn't work out that way.
 
Upvote 0
Neither "it doesn't work" or "things didn't work out that way." give us any idea of what is not working ie what is it doing and how is it different from what it should be doing, hence Akuini's request for more detail ?
 
Upvote 0
Neither "it doesn't work" or "things didn't work out that way." give us any idea of what is not working ie what is it doing and how is it different from what it should be doing, hence Akuini's request for more detail ?
I'm really sorry, I probably couldn't have explained the idea more clearly. Here is the file with my code. This works fine. What I want is to copy the values without formatting. thank you In advance.


 
Upvote 0
I have made some small changes, see how you go with this.
If you want to preformat your output sheet then change the .Clear back to .ClearContents.
If you have dates and numbers try xlPasteValuesAndNumberFormats instead of xlPasteValues and see if you like what it does.
PS: You had with statements inside of with statements most of which didn't seem to do anything.

VBA Code:
Sub test()
    Dim WS As Worksheet: Set WS = Sheets("Sheet1")
    Dim r  As Worksheet: Set r = Worksheets("Sheet2")

    Sup = MsgBox("Copy data", _
    vbCritical + vbYesNo, "Confirmation")
        
    If Sup = vbYes Then

        Application.ScreenUpdating = False
        With WS
            If .AutoFilterMode Then .AutoFilterMode = False
        End With
    
            With WS.Range("A1:G1")
                .AutoFilter 3, "Paris"
    
                lr = WS.Columns("A:G").Find(What:="*", _
                        SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
        
                Set Rng = WS.Range("A" & lr & ":G" & lr).SpecialCells(xlCellTypeVisible)
                If WorksheetFunction.Subtotal(3, WS.Columns(3)) > 1 Then
                    r.Range("A2:G" & r.Rows.Count).Clear                            ' Use ClearContents if you are pre-formatting the output sheet
                
                    rngA = Split("A,B,C,D,E,F,G", ",")
                    rngB = Split("B,A,E,C,D,F,G", ",")
                    
                    For i = LBound(rngA) To UBound(rngA)
                        WS.Range(rngA(i) & "2:" & rngA(i) & lr).Copy
                            r.Range(rngB(i) & "2").PasteSpecial Paste:=xlPasteValues ' Alternatively use xlPasteValuesAndNumberFormats
                    Next i
                End If
                .AutoFilter
            End With
            Application.CutCopyMode = False
            Application.ScreenUpdating = True
        End If

End Sub
 
Upvote 1
Solution
I have made some small changes, see how you go with this.
If you want to preformat your output sheet then change the .Clear back to .ClearContents.
If you have dates and numbers try xlPasteValuesAndNumberFormats instead of xlPasteValues and see if you like what it does.
PS: You had with statements inside of with statements most of which didn't seem to do anything.

VBA Code:
Sub test()
    Dim WS As Worksheet: Set WS = Sheets("Sheet1")
    Dim r  As Worksheet: Set r = Worksheets("Sheet2")

    Sup = MsgBox("Copy data", _
    vbCritical + vbYesNo, "Confirmation")
       
    If Sup = vbYes Then

        Application.ScreenUpdating = False
        With WS
            If .AutoFilterMode Then .AutoFilterMode = False
        End With
   
            With WS.Range("A1:G1")
                .AutoFilter 3, "Paris"
   
                lr = WS.Columns("A:G").Find(What:="*", _
                        SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
       
                Set Rng = WS.Range("A" & lr & ":G" & lr).SpecialCells(xlCellTypeVisible)
                If WorksheetFunction.Subtotal(3, WS.Columns(3)) > 1 Then
                    r.Range("A2:G" & r.Rows.Count).Clear                            ' Use ClearContents if you are pre-formatting the output sheet
               
                    rngA = Split("A,B,C,D,E,F,G", ",")
                    rngB = Split("B,A,E,C,D,F,G", ",")
                   
                    For i = LBound(rngA) To UBound(rngA)
                        WS.Range(rngA(i) & "2:" & rngA(i) & lr).Copy
                            r.Range(rngB(i) & "2").PasteSpecial Paste:=xlPasteValues ' Alternatively use xlPasteValuesAndNumberFormats
                    Next i
                End If
                .AutoFilter
            End With
            Application.CutCopyMode = False
            Application.ScreenUpdating = True
        End If

End Sub
Very cool thank you @Alex Blakenburg. Always find us the right solution
 
Upvote 0

Forum statistics

Threads
1,217,366
Messages
6,136,128
Members
449,993
Latest member
Sphere2215

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