VBA to insert rows based on cells column in another sheet (same workbook)

Bond00

Board Regular
Joined
Oct 11, 2017
Messages
142
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
So what I need is to detect if there is anything in Price Work Sheet in E10 to E113
Keep in mind there could be some blank cells mixed in. So it could look like this
E10-E17 in this example:
1725853168078.png


I need to be able to scan all the cells in this "Price Work Sheet" for any items in this column and on the 2nd Sheet "Finaloutput" Starting below B17 I need it to auto insert rows and copy the text from the other sheet into this sheet starting with B18 (in the B column) as it creates rows for each of them. (I want it to copy the empty space rows as well)
 
When i set variables for sheet tabs, if i want to not use the typed name and use the vba name is it same as the others?
Dim ws1 As Worksheet: Set ws1 = wb.Sheets(1)
Dim ws2 As Worksheet: Set ws2 = wb.Sheets("Finaloutput")

or does it need to be As a object?
Dim ws1 As Object: Set ws1 = wb.Sheets(Sheet1)
or Set ws1 = wb.Sheets1
The first option should be fine. "Worksheet" is a an object itself, so that makes it more clear what you are defining it to be, instead of just a general "Object", which could include other objects.

**Important part**
Also i forgot in my real doc, i had to use merged cells on the output sheet (ws2). So in my test doc i know you cant copy paste data unless the source and location match the same merged cell count, in this case its 6 cells across merged. So i made a copy of the data on ws1 to the far right side in some hidden columns with this data all lined up so its easy to copy 1 big range of 9 columns for the data.
My issue is i need to paste it as values only. and for the life of me i cant figure out how to do it i keep getting errors.
Run-time error '1004':
Application-defined or object-defined error
ws2.Range("A18").PasteSpecial just doesnt seem to work anyway i use it..
I just want to copy rng.Copy into ws2.Cells(18, 1).Select on down so i guess from "A18:J" & LRow+7
I tried just selecting A18 and pasting normal and that works fine but I need to paste values only.
Get rid of your merged cells! Most serious programmers will NEVER use them! They are probably the single worst feature of Excel, and cause all sorts of issues for things like VBA, sorting, and other functionality. If you are simply merging cells across individual rows, it is much better to use the "Center Across Selection" formatting option instead, which gives you the exact same visual effect as merged cells, but without all the issues merged cells cause. See: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis

For the pasting values only, i came across this: ws2.Range("A18:J" & LRow + 7).Value = rng.Value
So it didnt use the copy paste function and worked good, unless theres a better way let me know.
If that does what you want, that is fine. That method is usually preferrable than using Copy/Paste, if it does what you want.

Also 1 last issue i came across on this.
On Col A i need to count down but only where there is a value > 0 in column H.
how can i do that? is there some function I can use to do this in that column A?
Place this formula in cell A10 and copy down for all rows:
Excel Formula:
=IF(H10>0,COUNT($H$10:$H10),"")

Note: Just a General "rule of thumb" to help you get the best help as fast as possible.
- If you are posting a follow-up question that is directly dependent upon the previous question, post it in the same thread.
- If your follow-up question is NOT directly dependent upon the previous question, post it in a new thread. That way it will appear as a new unanswered question, will get a lot more looks (because it will appear in the "Unanswered threads" listing that many use to look for new unanswered questions to answers, and stands a better chance of getting an answer (and faster!). Also, the initial replier to your original question may not be available at the time, or may not know how to answer your new question.

So your last question above (the one asking for the formula), is really a new question and would typically have been better to post in a new thread (it is simple question and probably would have gotten a reply hours ago, long before I logged on for the day).
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Note: Just a General "rule of thumb" to help you get the best help as fast as possible.
- If you are posting a follow-up question that is directly dependent upon the previous question, post it in the same thread.
Alright sounds good, thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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