How to write VBA to copy and paste special from one column to another column with 1 Criteria

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
124
Office Version
  1. 365
  2. 2016
Hi,
I'm struggling to write a VBA for criteria "S" or "V" in Column ("C:C"). If value is True? Move Column ("H:H") to Column ("I:I") as paste special. From "Sheet1" or "ESS Inv Data Entry" sheet.
Can someone help me write this?
 

Attachments

  • 1 Criteria to paste special.PNG
    1 Criteria to paste special.PNG
    22.4 KB · Views: 10

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Run this macro on the sheet you want it to work on, that is, if you want it to work on the Sheet1 sheet, select the Sheet1 sheet and run the macro.

VBA Code:
Sub moveValue()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then Range("H" & i).Cut Range("I" & i)
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Run this macro on the sheet you want it to work on, that is, if you want it to work on the Sheet1 sheet, select the Sheet1 sheet and run the macro.

VBA Code:
Sub moveValue()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then Range("H" & i).Cut Range("I" & i)
  Next
  Application.ScreenUpdating = True
End Sub
Hi Dante,
The macro:
* Removed Values in column "H". I still need the values in the column.
* didn't paste special to Column "I" and changes format.
 
Upvote 0
Move Column ("H:H") to Column ("I:I")
???



* Removed Values in column "H". I still need the values in the column.
* didn't paste special to Column "I" and changes format.
Try this:

VBA Code:
Sub moveValue()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then Range("I" & i).Value = Range("H" & i).Value
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
???




Try this:

VBA Code:
Sub moveValue()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then Range("I" & i).Value = Range("H" & i).Value
  Next
  Application.ScreenUpdating = True
End Sub
Hi Dante,
Thank you that worked but how do I have the same format as original e.g. Accounting. I'm not sure where to apply "PasteSpecial xlPasteValuesAndNumberFormats". Need to look like pic attached in Brown.
 

Attachments

  • 1 Criteria to paste special (2).PNG
    1 Criteria to paste special (2).PNG
    48.7 KB · Views: 4
Upvote 0
If you are going to use images you need to include the Row and Column references.
Is there any reason you don't apply the Number Formatting to the whole column or at least each whole section up front ?
Applying number formatting inside the loop on a row by row basis would be the easiest but a bad idea.

Something like this might work. This is mostly using @DanteAmor's solution.
VBA Code:
Sub moveValue_Mod()
  Dim i As Long
  Dim NumFmt As String
 
  NumFmt = ""
 
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
        Range("I" & i).Value = Range("H" & i).Value
        If NumFmt = "" Then NumFmt = Range("H" & i).NumberFormat
    End If
  Next
 
  Range("I2:I" & Range("H" & Rows.Count).End(3).Row).NumberFormat = NumFmt
 
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm not sure where to apply "PasteSpecial xlPasteValuesAndNumberFormats".



I show you 2 options:

Option 1:

VBA Code:
Sub moveValue_v1()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
      Range("H" & i).Copy
      Range("I" & i).PasteSpecial xlPasteFormulasAndNumberFormats
    End If
  Next
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub



Option 2:

VBA Code:
Sub moveValue_v2()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
      Range("H" & i).Copy Range("I" & i)
    End If
  Next
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
Solution
I show you 2 options:

Option 1:

VBA Code:
Sub moveValue_v1()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
      Range("H" & i).Copy
      Range("I" & i).PasteSpecial xlPasteFormulasAndNumberFormats
    End If
  Next
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub



Option 2:

VBA Code:
Sub moveValue_v2()
  Dim i As Long
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
      Range("H" & i).Copy Range("I" & i)
    End If
  Next
  Application.ScreenUpdating = True
End Sub

😇
Thanks Dante,
Works great.
 
Upvote 0
If you are going to use images you need to include the Row and Column references.
Is there any reason you don't apply the Number Formatting to the whole column or at least each whole section up front ?
Applying number formatting inside the loop on a row by row basis would be the easiest but a bad idea.

Something like this might work. This is mostly using @DanteAmor's solution.
VBA Code:
Sub moveValue_Mod()
  Dim i As Long
  Dim NumFmt As String
 
  NumFmt = ""
 
  Application.ScreenUpdating = False
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value = "S" Or Range("C" & i).Value = "V" Then
        Range("I" & i).Value = Range("H" & i).Value
        If NumFmt = "" Then NumFmt = Range("H" & i).NumberFormat
    End If
  Next
 
  Range("I2:I" & Range("H" & Rows.Count).End(3).Row).NumberFormat = NumFmt
 
  Application.ScreenUpdating = True
End Sub
Hi Alex,
Sorry but this stops at "End If".
 
Upvote 0

Forum statistics

Threads
1,226,470
Messages
6,191,207
Members
453,648
Latest member
graduateguardian

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