Do not ignore excel hidden field VBA - excel

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.

Related

How to insert a column in Excel using VBA?

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/

How to delete a row instead of hiding it?

Trying to delete, rather than hide from one worksheet (where the filter button is to another where the catalogue list is. i.e. trying to create something like (best way to describe this) a shopping basket based upon the users selection and reducing the (long) list on the other worksheet after selection.
I am beginning to pull my hair out on this one and after having read and watched many, many articles on deleting rows after filtering on empty cells and today nothing has really helped as there are more issues than solutions when following codes from other, thus far.
Basically, I have a Hugh catalogue on a separate worksheet and if the user says yes, this should be shown and if not if blank (but with a value) it should be deleted. See so very basic script I have that works perfectly, but I have to delete and not hide. It appears EntireRow.delete is something beyond me, as it introduces many many issues, where hide simply worked so smoothly.
Trying many other scripts, they all really fail in simplifying the answer and 99% are actually for a single worksheet and range rather than a specific worksheet and specific columns i.e. E:E (script script below shows more) I am using a table too, so this is a little different too.
For a = 2 To 150
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Rows(a).Hidden = True
End If
Next
Anyone with a brilliant one or two liner to delete rather than hide, or delete all hidden if necessary
Many thanks in advance
Consider:
Sub sjdhfs()
For a = 150 To 2 Step -1
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Cells(a, 5).EntireRow.Delete
End If
Next a
End Sub
Note we run the loop from the bottom to the top.

How to select all names in Excel with VBA

I am working with a large Excel file, where I have defined around 2000 names for cells. In addition the excel contains formulas, these formulas are already entered with references to cells (A23-B24). I want to change the cells references by the defined names (instead of A23-B24 having VARIABLE_100-VARIABLE_120).
I know this is possible by selecting "Apply names" and then select the defined names from the list. Because I have around 2000 defined names, I would like to select all the names at once from the menu, but I cannot find an option, so I have to select one by one. I have been looking if there was an option for enabling the multiple selection on the menu, but I have not found such an option.
A work around for me would be to create a macro that applies to the selected range of formulas the selected names. Something like this:
Sub Macro1()
' Macro1 Macro
Selection.ApplyNames Names:=Array ("ATL_BV_XP", "ATL_BV_XP..EUN", "ATL_PK_XP", _
"ATL_PK_XP..EUN", "CHN_PK_IM", "CHN_PK_IM..EUN", "CHN_PK_IM..SHREUN", _
"E15_AG_AH", "E15_AG_EPA", "E15_AG_SFP", "E15_AG_SFP..CF" _
, "E15_APF_FE"), IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
End Sub
My problem is that because I have a large number of defined names (already wrote like twice that around 2000), listing all names in the macro code becomes really complex. I thought that probably a workaround would be to create a list including all defined names within the code. Does someone has an idea how to do this? I have looking around and some codes suggest a loop while other say it is possible to extract a list. Nonetheless I have not been able to find a way to do it.
The documentation at msdn.microsoft.com says
Names: An array of the names to be applied. If this argument is omitted, all names on the sheet are applied to the range.
see https://msdn.microsoft.com/en-us/library/office/ff196578(v=office.15).aspx
Following on that try this:
Selection.ApplyNames _
IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
This should work depending on the Scope of the Defined Names and their visibility. Try that and let us know the results.
It may already be available to you. Say we have defined the Names Cost, Profit, Tax
When typing a formula, have the Formula Tab open and you can always pull-down those names:
A macro would be nice, but I don't know how to invoke a macro in the middle of editing.

set print area for dynamic range in VBA

I want to write vba code for setting print area. first page is introduction (A1:Q53) and from A54 onwards i want to print upto end of data. kindly suggest the command. there are empty cells on first page hence if i give command to search end of data it stops at first page itself.
Even though your question is not completely clear, I would suggest using either CurrentRegion of the Range A54 or resolve the range you need using the UsedRange property of the sheet.

Freezing certain cells - VBA

I have a question about freezing certain cells. But first let me explain the situation.
I have made a search box in my excel sheet and when you search for a letter or word; the results show up in cells below the search box. Now I want to freeze those cells, so that wherever I go in my sheet. I can always use the search box and see the results.
The cells for the searchbox and results are B2:B25. Those are the ones I want to freeze. Also the only sheet I want to use this on is the sheet "Reading". On the rest of my sheets I do not use a search function.
So my question(s) is : Do I need to put the code inside a module or on that sheet, and how do I do this?
Now I have tried the following
Range(Cells(2,2), Cells(25, 2)).Select
ActiveWindow(or maybe Reading?).FreezePanes = True
Inside a module. But it did not work and I do not know what else to do.
Any help is much appreciated! Since I am very new to VBA.
Almost there. Problem is that the 'range' and 'cells' needs to be directed to the 'Reading' sheet, like so:
Sheets("Reading").Range(Sheets("Reading").Cells(2,2), Sheets("Reading").Cells(25, 2)).Select
ActiveWindow.FreezePanes = True
but if it always is B2->B25, why not use:
Sheets("Reading").Range("B2:B25").Select
ActiveWindow.FreezePanes = True
This should work. Select is not very desirable, because it is very slow, but in this case, you need to (as far as I know).
EDIT
BTW, you can do this from within a code module or from within a sheet, but if you choose to do it from within a sheet, you cannot select another sheet. So just use the range.
EDIT 2
whoopsy, typo corrected. 'Sheet' should have been 'Sheets'

Resources