Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:34:12+00:00 2026-06-17T21:34:12+00:00

Below is the code which I am using to copy cells from one sheet

  • 0

Below is the code which I am using to copy cells from one sheet and paste in to another.

Sheets("codes").Select
Range("A5:A100").Select
Selection.Copy
Sheets("Sheet2").Select
Range("B28").Select
ActiveSheet.Paste

The problem with this is some cells in this range are blank but I do not want them to be copied to Sheet2. I have got some idea from here but this method is too long. Is there a way I can iterate on the selection and check if the value is non-empty and paste. This way I can also paste some other text (eg #NA) in the blank cells.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:34:13+00:00Added an answer on June 17, 2026 at 9:34 pm

    Looks like you may be making some common rookie mistakes here (it’s okay we all did it).


    VBA example with line-by-line explanations

    TIP: Try not to use “Select” or “Copy”. Why use select when all you have to do is reference the cells themselves? For example, instead of using

    Sheets("codes").Select
    Range("A5:A100").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("B28").Select
    ActiveSheet.Paste
    

    Just use

    dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
    set myBook = Excel.ActiveWorkbook
    set mySheet = myBook.Sheets("codes")
    set myOtherSheet = myBook.Sheets("Sheet2")
    
    dim i as integer, j as integer 'Define a couple integer variables for counting
    
    j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
    for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
       if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
          myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
          j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
       end if
    next i 'This triggers the end of the loop and moves on to the next value of "i".
    

    I did the same thing all the time when I first started, and it never works out right. “Select” causes errors left and right. Use my code, read the comments, and you’ll be fine. A quick WARNING: I don’t have Excel on this computer so I couldn’t test the code. If it doesn’t work for some reason, leave me a comment and tomorrow I’ll fix it at work.

    The above code will omit blank cells completely when copying the data over to your second sheet. If you want to input a certain text for blank cells instead (like “N/A”), then you can use the following:

     dim mySheet as Worksheet, myOtherSheet as Worksheet, myBook as Workbook 'Define your workbooks and worksheets as variables
     set myBook = Excel.ActiveWorkbook
     set mySheet = myBook.Sheets("codes")
     set myOtherSheet = myBook.Sheets("Sheet2")
    
     dim i as integer, j as integer 'Define a couple integer variables for counting
    
     j = 28 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
     for i = 5 to 100 'This is the beginning the the loop which will repeat from 5 to 100 . . .
        if mySheet.Cells(i,1).value <> "" then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
           myOtherSheet.Cells(j,2).value = mySheet.Cells(i,1).value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
        else 'If the cell is blank, then . . .
           myOtherSheet.Cells(j,2).value = "N/A" ' . . . place the text "N/A" into the cell in row "j" in Sheet2.
        end if 'NOTICE we moved the "end if" statement up a line, so that it closes the "if" statement before the "j = j + 1" statement. _
          This is because now we want to add one to the "j" variable (i.e., move to the next available row in Sheet2) regardless of whether the cell in the "codes" sheet is blank or not.
           j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
     next i 'This triggers the end of the loop and moves on to the next value of "i".
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding copying cells from one sheet to another using VBA.
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I am using the below code to block the taskbar which is working perfectly.
I am using the below code to receive the message via serial port which
I am trying to copy a range of cells for which I don't know
I made a program in which I want to copy a file from one
[Edit: see final code below] I'm using the code below to randomly select 5
I am using blow code for join in nhibernate, which is working fine. But
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.