Running Excel using VBA from another program - excel

I am using vba in a CAD program to export data, sort the data, and add data. The following macro is exactly what I want excel to do. However I believe I am limited to having the CAD program tell Excel what to do through VBA. This macro copyies a formula and pastes it to all the populated cells below it in the column.
MACRO CODE:
Range("B1").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
iLogic Version of Code:
oBook.WorkSheets(1).Name = "Order List"
oBook.WorkSheets(2).Name = "Cut List"
wSheet1 = oBook.WorkSheets("Order List")
wSheet2 = oBook.WorkSheets("Cut List")
wSheet2.Activate
wSheet2.Range("B1").Select
wSheet2.Selection.Copy
wSheet2.Range(Selection, Selection.End(xlDown)).Select
wSheet2.Selection.Paste
Unfortunately I seem to be missing something to translate between Inventor and Excel, but I don't know enough to even know if that's the issue.
Any advice is very much appreciated as I am still very new to VBA.

Ok. I had some code that I had copied off of a forum but didn't understand the functions going on. I believe this is what you are referring to "Application"?
wSheet2.Columns("G:G").select()
oExcelApp.Selection.cut()
wSheet2.Columns("B:B").Select()
oExcelApp.Selection.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)
Although I don't understand what the
"(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)" refers to. I understand the part about the direction and where it inserts just not whats before it.

I do agree with #DisGruntledDraftsman about not using Select and Activate. However, if you are just trying to get some working code that gets the job done, some simple tweaks can be done. Of course this isn't ideal but, it should work though.
row_Count = wSheet2.Range("B1048576").End(xlUp).Row
wSheet2.Activate
wSheet2.Range("B1").Select
Selection.AutoFill Destination:=wSheet2.Range("B1:B" & row_Count), Type:=xlFillDefault

Related

vba inserting/copying range of rows next cell formulas collapse

i am using basic vba code to insert Range(C3:E3").Insert xlShiftDown rows. I discovered that, formulas which are related to these cells, and are in different columns also collapse with inserting range.row (not entire.row).
I wonder if there is any workaround with vba code(i mean using insert with different mode) or it is part of excel game we need to figure out to solve our problem without inserting rows.
Appreciate any guidance or opinion why it could be so in excel ahead
Can you try cut or paste?
Range("C3:E3").Select
Selection.Cut
Range("C10").Select
Selection.Insert Shift:=xlDown
Range("C3:E3").Select
Selection.Cut
Range("C10").Select
ActiveSheet.Paste

Need to convert VBA code to Excel online Scripting

Good morning all,
Can someone help me by converting this simple VBA code to Excel Script code?
What the script does is it selects and entire row from the active cell and pastes it on the first row to the Template sheet.
Sub Copy()
ActiveCell.EntireRow.Select
Selection.Copy
Sheets("Template").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Instead of all the copying and pasting, why not simply use this piece of code (obviously you need to run this while having a worksheet open, different than the "Template" one):
Worksheets("Template").Range("1:1").Value = ActiveSheet.Range("1:1").Value
More information on this can be found in this other StackOverflow post.

Excel: Copy Row to New Tab if Cell Contains Value

I'm a complete novice when it comes to Excel, I've Googled it and it's come up with some responses that I don't understand in the slightest. Most of which are things I need to do in Visual Basic?
Essentially what I want is to set up several tabs for individual users, that generate from the main tab. All of which are set up already.
So IF Row R on Main Sheet has NO (Persons Initials)
Copy this row to the Nick tab (There will be 20+ rows for each user, not sure if that changes anything)
Any help would be incredible and massively appreciated!
Thanks,
Nick
You can probably get a start by recording a macro. Here's a link on how to do that: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-macro-HP010342374.aspx#BMrecordmacro. After you click record, just start doing what you want done manually. That will show you some of the syntax. In this case, it will probably be easiest for you to format your summary data as a table, filter it by each person's initials and then copy the results to the individual tabs. Your recorded code is probably going to look something like this:
Sub CopyNO()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Range("Table1[#All]").Select
Selection.Copy
Sheets("NO").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
To clean up the code you record, you'll want to try to change anything that says ActiveSheet to something like Sheets("SheetName") and, ideally, get rid of any .Select commands by instead using whatever is actually done with the selection. Here you might end up with something like:
Sub CopyNO()
Sheets("Summary").ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Worksheets("Summary").Range("Table1").Copy _
Destination:=Worksheets("NO").Range("A5")
End Sub

Excel VBA That command cannot be used on multiple selections

I have got this code:
Worksheets("SOURCE_DATA_HIDDEN").Activate
Sheets("SOURCE_DATA_HIDDEN").Select
Columns("B").Copy
Sheets("RESOURCE_DEMAND").Select
Columns("C").Select
ActiveSheet.Paste
I use it to copy and paste a column from one sheet to another. The code used to work last time i checked and it somehow broke today.
It comes up with the following error:
Run time error 1004: That command cannot be used on multiple selections.
I really cant figure out what is going on. I haven't made any amendments to the code.
try to clear the clipboard Application.CutCopyMode = False
You can simplify that a lot more. If its just the values you are wanting too, change to PasteSpecial. Try this:
ThisWorkbook.Sheets("SOURCE_DATA_HIDDEN").Columns("B").Copy
ThisWorkbook.Sheets("RESOURCE_DEMAND").Range("C1").PasteSpecial Paste:=xlValues
Application.CutCopyMode = False
Eliminates all the selecting and activating, which is generally a habit you dont want to keep!

Run Time Error '1004': Select method of Range Class failed VBA 2003

I am trying to copy a column from one sheet to another. The code I am using is a recorded macro and it works fine until I connect it to a button. When I do so, it gives a
Run Time Error '1004': Select method of Range Class failed
Here is the code and I can see nothing wrong with it. When I hit debug it highlights the second line.
Sheets("Count").Select
Columns("C:C").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("b1").Select
ActiveSheet.Paste
Sheets("Count").Select
Sheets("Count").Columns("A:A").Select
Columns("A:A").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("A1").Select
ActiveSheet.Paste
I have no clue what the problem is. Please help
You should always avoid using .Select They are a major cause of errors. You may want to see How to avoid using Select in Excel VBA
Sub Sample()
Sheets("Count").Columns("C:C").Copy _
Sheets("Add Invintory").Columns("B:B")
Sheets("Count").Columns("A:A").Copy _
Sheets("Add Invintory").Columns("A:A")
End Sub
I think the issue is that you have written the code in another sheet's code module. If I'm in Sheet1, and write e.g.
Sheets("Sheet2").Select
Columns("A:A").Select
...then Excel assumes you are referring to the Columns on Sheet1 as it treats the current sheet as a default. Therefore, you've told Excel "select Sheet 2" then "select a column on Sheet 1"...which it can't do so it gives you an error message. The best solution would be not to use 'Select'...but you will still see in Siddharth's code that he has had to refer to sheet addresses explicitly
Your original code would have worked if placed in the ThisWorkbook module. Locations for entering code are explained towards the end of this Excel help video
When you are putting vba code into the "view sheet code" .. There definitely helps to use Application.Run ... to run macro..
I had problem i directly input macro to sheet code.. for selection in another sheet it claimed runtime error 1004.. So i created macro separately and then i put Application.Run my macro from sheet code.
Works out perfectly ;)
This Application.Run also helps when you have too big macro that excel claim it cant be so big. You can easily divide to several parts and then just run applications one by one.. ;)

Resources