Please help me with the code to insert a column in excel using vba. Here is what I am doing -
Sheet1.Range("A:A").EntireColumn.insert
That code works fine for me.
Sheet1.Range("A:A").Insert works also. Range("A:A") is already referencing an EntireColumn.
There are a few things to check if it's not working for you:
You're referencing an object called Sheet1. Is that definitely the codename of the worksheet you want to change? Make sure you understand the difference between sheet name and codename. You could try referencing it by the sheet's name instead: Worksheets("Tabname").Range("A:A")...
Is the worksheet protected? That would give you an error if it is.
Is there any data in the right-most column of the spreadsheet? That would also cause an error as Excel doesn't know what to do with it. If you're not 100% sure, select the entire right-most column of the sheet and hit delete to remove anything that might cause an issue.
Lastly, can you insert a column manually? i.e. select left most column, [right-click] and [Insert]?
I think you got the Sheets property wrong, this works for me :
Sheets(1).Range("A:A").EntireColumn.Insert
You should clearly mention what you are trying to do; what type of problem you are facing.
However, you should try out the below code. hope this will help
Sheet1.Range("A:A").EntireColumn.Insert Shift:=xlToRight
while inserting a new row or column make sure to refer to the entire row or column. Check if there are any marge cells at the right of column A. That can possibly be causing the problem.
Sub TryMe()
Columns(1).Insert
End Sub
Also, this is a very generic question. If you can Google something and figure it out in a fraction of the time that it takes to craft a question and post it on SO, just Google it. If your question is very unique, and after hitting multiple dead ends using Google, then ask the SO community. I found the 3 links below using a Google search that took around 1 second.
https://www.automateexcel.com/vba/insert-row-column/
https://excelchamps.com/vba/insert-column/
https://www.educba.com/vba-insert-column/
Related
Something has changed in my VBA that is not allowing me to complete certain routines. I have listed a very simple example below. If anyone has experienced this I would be really appreciate any support in resolving this issue.
Simple example of issue:
When I use the following code it works fine.
Sheets("Sheet1").Select
Range("B3").Select
When I combine them I get a "1004" error
Sheets("Sheet1").Range("B3").Select
I checked the reference/document library and nothing appears to have changed in here. It has to be something simple but I just can't put my finger on it.
If you absolutely must do it in a single line of code then swap the Select for an Application.GoTo which accepts both worksheet and cell range.
application.goto range("Sheet1!B3")
However, it is almost never necessary (and most often counter-productive) to use the Range .Select method to reference a cell or cells to work on. See How to avoid using Select in Excel VBA macros for methods on getting away from relying on select and activate to accomplish your goals.
You already have your answer:
first Select the worksheet
the Select a range on that worksheet
Your code will work if you happen to be on Sheet1 when it is run, but will fail if you are not on Sheet1. In my opinion VBA is kind of dumb with regard to this issue.
I'm creating an Excel file from data in an Access database.
All I'm currently trying to do is offset a selection I've made in Excel from Access, and I keep receiving errors.
ExcelWS.Range(Range("H6"), Range("H6").End(xlDown)).Offset(0, -1).Select
ExcelWS is an Excel. Worksheet object.
My goal is to select from G6 to the end of the workbook. However, not every cell in column G is populated, so a simple End(xlDown) from cell G6 only takes me part way through the file. That being said, all cells in column H are populated, so I'm able to do an End(xlDown) from cell H6. From there, I want to move my selection one to the left, giving me the desired range in column G selected.
The above code is a product of about 2hrs of googling, and trying tons of different combinations of similar code. However, I'm still receiving an error.
Run-time error '1004':
Method 'Range' of object '_Global' failed
The odd thing is that 1/10 times, I'll receive the desire results. The rest of the time I receive the above error.
I feel as though my issue has something to do with running that line of code from Access, however that's currently my only way of completing this task as I have other things that rely on being able to do it from Access.
Any help would be appreciated, and if there's any more info I can give I'll try my best.
Thanks.
To run Select the sheet shoudl be active/ in -focus.
So you ned to set foucus to sheet.
ExcelWS.Activate
ExcelWS.Range(Range("H6"), Range("H6").End(xlDown)).Offset(0, -1).Select
Simalarly, to make the seet in-focus, your workbook needs to be active/ infocus.
Try to avoid Select as much as possible. Also fully qualify the names.
Dim rngTest as Range
Set rngTest= ExcelWS.Range(ExcelWS.Range("H6"), ExcelWS.Range("H6").End(xlDown)).Offset(0, -1)
Thanks to #cyboashu, I was able to get it working.
I took the examples he gave me and mushed them together to make it do what I wanted.
I realized after posting the question that it was working every other time, consistently.
Anyway, the code I used is
Dim ExcelR1 As Range
Set ExcelR1 = ExcelWS.Range(ExcelWS.Range("H6"), ExcelWS.Range("H6").End(xlDown)).Offset(0, -1)
ExcelR1.Activate
I am trying to use one of the excel hidden field for the purpose of referencing. Basically column(A:A) is hidden and it contains specific IDs that I can use it to reference it to another sheet.
I could have moved the column (A:A) further away so that the user does not see it, but my issue is that I have written too many lines of code already. I guess it is poorly constructed, because if I were to move any of my columns, my entire program would definitely break. I could try to fix it, but that would mean I would have to over analyze my own code and I either wouldn't understand it or wouldn't find my mistake.
So, anyways, I have a Range.Find function, which is looking in the hidden field, but returns nothing. I could try to unhide it, and hide it again, but I want to know that if there is a solution in Excel, then to not ignore the hidden field.
Set myCell = Columns(1).Find("search_string", lookat:=xlWhole, LookIn:=xlFormulas)
Debug.Print myCell.Row
Replace "search_string" to the ID you are looking for.
I'm creating a macro to manipulate a table in excel. I've been able to create code to do what I need, the problem is, my table names might not always be the same.
As an example:
Range("DATA_INPUT4[MFG Catalog]").Copy
This code copies the MFG Catalog column in my table just fine, but the table isn't always going to be named "DATA_INPUT4".
I've played around with a few things to try to get it to work with the active table and I feel like I'm close, but I can't get any progress.
I've tryed:
Sub Copy_Active_Table()
Dim activeTable As String
activeTable = ActiveSheet.ListObjects(1).Name
MsgBox activeTable 'To make sure it's pulling the correct table name
Range("activeTable[MFG Catalog]").Copy
End Sub
This is probably completely wrong, but you can see where I'm going with it.
I have a feeling that I have to Dim activeTable As ListObject but I haven't been able to figure that out either.
This seemingly simple problem is driving me nuts, any help would be awesome.
Thanks,
Brian
For those who look for a solution to reference active table, the solution is:
ActiveCell.ListObject.Name
Other solutions fix problem in the question (using variable to refer to the table) - but the script in the question does NOT refer to the active table - but rather the first table in the sheet (which might be the same as active table).
Answered in the comments,
Yes indeed, you are very close. Try this: Range(activeTable & "[MFG Catalog]").Copy
Ralph
Thanks, Ralph.
Just had a similar problem and ended up here.
Along the lines of #choukkos's answer, you can also use the ListObject's properties (which is even a bit faster as it doesn't need to do the name resolution twice)
ActiveCell.ListObject.ListColumns("MFG Catalog").DataBodyRange.Copy
in the same manner other table data can also be extracted...
Set t = ActiveCell.ListObject
'copy line 3 of the table
t.ListRows(3).Range.Copy
'select current line
t.ListRows(Selection.Row - t.Range.Row).Range.Select
I got stuck at a problem with Excel VBA.
I am supposed to do the kinda easy task of copy/paste a variable range of cells from "sheet2" into the same range in "sheet1".
500 Rows like in my code is far too much, but I tried it this way, to "catch" the variable aspect.
The tricky part is, that the range in "sheet1" is a table(which gets created from TFS).
Sub CopyP()
Sheets("Sheet1").Range("B3:F500").Value = Sheets("Sheet2").Range("B3:F500").Value
SheetObject.ListObjects (ListObjectName)
Range("NAME OF TABLE[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
The [Iteration Path] is a column name of my table, i want to check in this column/with this parameter, if the "row" is empty.
I know the code is far from being good or clean but the table is giving me a hard time copying.
With this code I got another problem: the table gets created from TFS, no problem to copy into that, BUT the name of the table is variable(seems like TFS creates the name), so unless I put the name manually in the code, the "program" cant execute, because of missing range.
Didn't find a way to get a return of the table name somehow.
But I think I am just following the wrong path overall, maybe someone can be bring me on the right track.
My other Idea is to Iterate through the Rows in Sheet 2 to fetch just as much data is needed and then copy them with an iteration into the table. But i guess I would be the same problem with the table-name there.
Every information I find using google , talks about tables where the user can "name" the table. In my case I cant, so I have to work with the name TFS uses for my table.
Further to my comments below your question, I just typed this in notepad. Please amend it to suit your need.
This will give you the names of all tables in the activesheet. If there are multiple tables then you will get multiple names. (UNTESTED AS POSTING FROM PHONE)
Sub sample()
Dim objLB As ListObject, TableName As String
For Each objLB In ActiveSheet.ListObjects
TableName = objLB.Name
Exit For
Next
Range(TableName & "[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub