How to specify range instead of Entire.Row

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello everyone....

I'm using this command below to insert a row.
But instead of copying the entire row, I only want to copy from A to Q only.
How should I change the command?


VBA Code:
Dim i As Integer
Dim j As Integer

For i = 1 To 10

j = InStr(1, Cells(i, 18), "Freight", vbTextCompare)

If j = 1 Then
Cells(i, 1).EntireRow.Copy

Cells(i + 1, 1).EntireRow.Insert
Application.CutCopyMode = False


Cells(i + 1, 7).Value = "Z001"
Cells(i + 1, 10).Value = Cells(i, 16)

i = i + 1

Else
End If

Next i

Thank you in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
First a few comments
When inserting or deleting rows it is usually best to work from the bottom up.
Integer variable values are converted to Long so you might as well declare them that way to start with.
There is no need for your 'Else' line of code since there no code lines in that section.

See how this goes with a copy of your data.
VBA Code:
Sub Test()
  Dim i As Long
  Dim j As Long
  
  For i = 10 To 1 Step -1
    j = InStr(1, Cells(i, 18), "Freight", vbTextCompare)
    If j = 1 Then
      Rows(i + 1).Insert
      Cells(i, 1).Resize(, 17).Copy Destination:=Cells(i + 1, 1)
      Cells(i + 1, 7).Value = "Z001"
      Cells(i + 1, 10).Value = Cells(i, 16).Value
    End If
  Next i
End Sub
 
Upvote 0
Solution
First a few comments
When inserting or deleting rows it is usually best to work from the bottom up.
Integer variable values are converted to Long so you might as well declare them that way to start with.
There is no need for your 'Else' line of code since there no code lines in that section.

See how this goes with a copy of your data.
VBA Code:
Sub Test()
  Dim i As Long
  Dim j As Long
 
  For i = 10 To 1 Step -1
    j = InStr(1, Cells(i, 18), "Freight", vbTextCompare)
    If j = 1 Then
      Rows(i + 1).Insert
      Cells(i, 1).Resize(, 17).Copy Destination:=Cells(i + 1, 1)
      Cells(i + 1, 7).Value = "Z001"
      Cells(i + 1, 10).Value = Cells(i, 16).Value
    End If
  Next i
End Sub
I see....
Thank you for your explanation. I tried the code and it works perfectly well.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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