ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dim & Copy & Paste (https://www.excelbanter.com/excel-programming/393259-dim-copy-paste.html)

Josh O.

Dim & Copy & Paste
 
I have the below macro to find cells in column 14 and paste them to column
"A", same row, if there is data in that cell.

I need to be able to do the same thing for another column in the same
worksheet (ie - find values in 16th column and paste to same row column "I"),
but I get an error if I try to use similar code (just changing the column
index and destination column).

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 14) < "" Then
Application.CutCopyMode = False
Cells(i, 14).Copy
Range("a" & Cells(i, 14).Row).Select
ActiveSheet.Paste
End If
Next


Wigi

Dim & Copy & Paste
 
Hellowa Josh

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 14) < "" Then Cells(i, 14).Copy Range("a" & i)
Next

'and

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 16).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 16) < "" Then Cells(i, 16).Copy Range("I" & i)
Next



--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"Josh O." wrote:

I have the below macro to find cells in column 14 and paste them to column
"A", same row, if there is data in that cell.

I need to be able to do the same thing for another column in the same
worksheet (ie - find values in 16th column and paste to same row column "I"),
but I get an error if I try to use similar code (just changing the column
index and destination column).

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 14) < "" Then
Application.CutCopyMode = False
Cells(i, 14).Copy
Range("a" & Cells(i, 14).Row).Select
ActiveSheet.Paste
End If
Next


Josh O.

Dim & Copy & Paste
 
Wigi,
With both Dim statements I get the following error:

"Compile Error: Duplicate declaration in current scope"

"Wigi" wrote:

Hellowa Josh

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 14) < "" Then Cells(i, 14).Copy Range("a" & i)
Next

'and

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 16).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 16) < "" Then Cells(i, 16).Copy Range("I" & i)
Next



--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"Josh O." wrote:

I have the below macro to find cells in column 14 and paste them to column
"A", same row, if there is data in that cell.

I need to be able to do the same thing for another column in the same
worksheet (ie - find values in 16th column and paste to same row column "I"),
but I get an error if I try to use similar code (just changing the column
index and destination column).

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 14).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 14) < "" Then
Application.CutCopyMode = False
Cells(i, 14).Copy
Range("a" & Cells(i, 14).Row).Select
ActiveSheet.Paste
End If
Next



All times are GMT +1. The time now is 03:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com