Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

vba - Insert rows based on two or more conditions in Excel

I'm currently working for a company that collects data from public ilumination systems around my country. We use a lot of Excel Sheets to organize data and, because of that, we end up using a bunch of formulas to speed up the process. However, I'm new to VBA and I need some help with a code that I was trying to create.

This is what I'm trying to make

Each row has an ID, and each ID has a sequence of multiple types of lamps (sodium vapor) and power in watts (I need to copy this sequence from another sheet). I need to pick this values and put in the main table, if there are 2 lamps that are equal in both power and type, I need to put the type+power in the "Lamp" column, and the number of times it appears in the row (2) in the "Quantity" column. BUT, if, in the same row, there are lamps with different powers or types, the other type of lamp needs to be inserted below the first row with the same ID. For example:

This is what should happen

I need to do this manually. I want to know if theres a way to make a VBA code that will check the number of repetitions of a specific value and return THE value and THE NUMBER OF REPETITIONS both in the "Quantity" column and in "Lamp" column, and IF THERE ARE DIFFERENT TYPES OF LAMPS will insert the necessary number of rows below the first one, containing the same data but the "Quantity" and "Lamp" values, these two should be the values of the second type of lamp and etc. Can you guys help me?

I'm sorry, english is not my main language and I'm still learning it, and I'm also bad at explananing so if you didn't get what I need I can clarify if necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...