Insert rows into worksheet from another workbook?
Yes, you can make a VBA macro for this.
I'd create a button, and in the click event, first make sure workbook_b is open, then use the following code to copy the rows where custid is in workbook_a, but not in workbook_b (note that in the end you just end up with a copy workbook_a in workbook_b, so it'd be a lot simpler to just copy the whole data range over).
Dim i As Integer, workingCol1 As Integer, workingCol2 As Integer
Dim workingRange1 As Range, workingRange2 As Range
workingCol1 = WorksheetFunction.Match("custid", Sheets("Sheet1").UsedRange.Rows(1), 0)
Set workingRange1 = Sheets("Sheet1").Range("AllCustomers").Columns(workingCol1)
workingCol2 = WorksheetFunction.Match("custid", Sheets("Sheet2").UsedRange.Rows(1), 0)
Set workingRange2 = Sheets("Sheet2").Range("CriticalCustomers").Columns(workingCol2)
For i = 2 To workingRange1.Rows.Count
If Not IsError(Application.Match(workingRange1.Cells(i, 1), workingRange2, 0)) Then
workingRange1.Rows(i).EntireRow.Copy
Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Offset(1, 0).EntireRow.PasteSpecial (xlPasteValues)
End If
Next i
If you really only need one cell copied over, then you could do it with a worksheet-function, but it'd be a little complicated.
Related videos on Youtube
samprat
Updated on September 18, 2022Comments
-
samprat over 1 year
Cards.h
class Card { public: // Card suits struct Suit { // Suits in order enum Enum { Clubs, Diamonds, Hearts, Spades, }; }; // Card rank struct Rank { // Ranks with aces low enum Enum { Ace, Two, King, .... ... }; }; // constructors //get & set functions //predicate friend bool compareCardsSuit(const Card & cardlhs, const Card & cardrhs) { return cardlhs.GetSuit() == cardrhs.GetSuit(); } friend bool operator==(Card const& lhs, Card const& rhs) // THis func is used for some other purpose { // We only care about rank during equality return lhs.m_rank == rhs.m_rank; }
hands.h
class Hand { public: static int const Size = 5; // The type of hand we have struct Type { // Type of hand in accending order enum Enum { HighCard,// Five cards which do not form any of the combinations below Flush, // Five cards of the same suit bla, bla.. }; }; // Object creation // other functiosn private: mutable std::array<Card, Size> m_cards; Type::Enum m_type; // Hand calculations Type::Enum Evaluate();
hands.cpp
Hand::Type::Enum Hand::Evaluate() { std::equal(m_cards.begin(), m_cards.end(), compareCardsSuit); // got error { return Hand::Type::Flush; } // Return our hand return Hand::Type::HighCard; }
All I want is to check that if member data of m_cards has identical suit then return flush..
I am getting error as shwon below
Error 3 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion)
Error 2 error C2171: '++' : illegal on operands of type 'bool (__cdecl *)(const Card &,const Card &)'
-
rdrgrtz almost 13 yearsThanks @Lance, I will probably need an answer since I have no idea how to do it. I was actually looking to do it with formulas, to make the workbook more "portable".
-
rdrgrtz almost 13 yearsThanks @lance, I wasn't aware that VBA resides "in" the workbook.
-
Lance Roberts almost 13 yearsOk, I'm ready to work on this but don't know which cell you want to copy and where you want it to end up. Throw me the relevant column numbers and names on each sheet and I'll whip something up (I hope).
-
Mr Lister almost 9 yearsI'm afraid you will have to write the
==
operator yourself. Your source doesn't contain the++
, so I'm not sure how to handle that error. -
samprat almost 9 years@Mr Lister. Thanks The issue is I have already written equality operator for ranks. this operator is used for some different purpose. Now how can I write again same operator for suit that takes same parameter as of rank. the compiler will not allow me.. Also what s wrong in my code?
-
Mr Lister almost 9 yearsHm? Why won't the compiler allow you to write a similatr operator for a different class? Can you post a minimal, complete and verifiable example in the question that we can check?
-
David G almost 9 yearsDon't use
std::equal
, usestd::find
and compare it withm_cards.end()
to see if it found the element (i.eif (find(...) != m_cards.end()) { ok }
). -
samprat almost 9 years@ 0X499602D2 , by std::find how would I make sure that all objects of array has same suit for example all objects have clubs ?
-
David G almost 9 years@samprat
std::all_of(m_cards.begin(), m_cards.end(), [] (const Card& c) { return c.GetSuit() == Clubs; })
.
-
-
rdrgrtz almost 13 yearsNifty script @lance, but like you said, if the result is a workbook copy, then not much use. Also, the fact that it's VBA limits the use to one machine, and I want the function to work no matter where I take the Excel file (unless I'm mistaken and VBA is embedded in the Excel file itself).
-
Lance Roberts almost 13 years@rdrgrtz,I don't think you get what I'm saying. What you specified was what wrote, if you don't want it to be just a copy then specify something different. The VBA is in the file, just like any worksheet-function is in the file, so therefore I assume now you only want a menu-driven solution. Please verify and clarify your question if you really didn't want to end up with a copy.
-
rdrgrtz almost 13 yearsThanks for the clarify @lance, I did get what you were saying, I just wasn't aware that VBA resided "in" the file. Your solution, as per required, is correct; I guess I just want a conditional copy of just one cell, and not the entire workbook. I'll mark this as answered, sorry for the upset.
-
Lance Roberts almost 13 years@rdrgrtz, I'll get back to you with a one-cell copy.