I have an Excel spreadsheet that is designed to facilitate the bulk creation of new user accounts in an information system I help administer. Due to the fact that we have had issues in the past with people providing invalid data in various columns, I have setup data validation rules and, in particular, data validation input messages for the entire table to help guide the spreadsheet users in providing the correct data.
A problem I foresee cropping up is there are a total of 16 fields that a user is expected to fill out, some users may be annoyed at the input message boxes that pop up each time they select a cell in the table.
My question is this: Is there away to hide all of the data validation input messages in my table using VBA?
Using the record macro feature and some references in msdn.microsoft.com I have come up with the following code that I think is on the right track:
Sub test()
ActiveWorkbook.Sheets("DataEntry").ListObjects("Data").Range.Select
With Selection.Validation
.ShowInput = False
End With
End Sub
Right now I get the following error:
Run-time error '1004': Application-defined or object-defined error
Any help that you could provide would be greatly appreciated!
Related
At the moment I have created a spreadsheet which takes a bunch of inputs, runs them through a list of formulas, and then spits out the results onto a "report" worksheet.
I've been manually saving each of these reports as separate CSVs but I was hoping for a better method moving forward as it is getting quite tiring to have to open 10 CSVs when i do my monthly reports.
I am looking for a way to start saving all of these reports into a "database". My hope to to have one cell be for an user entry name and for two buttons. One to save the current report under the name entered by the user, and two to remove old records. I would then be able to revisit old entries by selecting them in the dropdown.
I've dabbled with VBA and Macros in the past but this is a little more complicated than what I've dealt with in the past. Looking for some help/direction.
Thanks for your time!
Depending on how your reports need to be used, you might find it satisfactory to simply make your data into one big Excel Table ( Insert Tab > Table ). When you do this, Excel will automatically fill-down any formulas that you enter in a column, and also show the formula using the headers instead of A1-style references.
I use this format, adding Y under Remove from Active List on each line that is already done. Then whenever I save the file or look at it for today's status, I filter out what's old and just look at the new. The other filters enable copy-pasting or printing whatever arrangement I like.
The filters and other things in the table can be referenced in VBA as Sheets("ThisSheet").ListObjects(1), which is an object with a number of useful properties and methods.
For VBA information, read more here: https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables
This is my code for auto-filtering the table to hide inactive items at time of save. You add it at ThisWorkbook in the VBA editor:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Activate
SelectedCell = ActiveCell.Address 'this saves your screen selection for after the filtering
ActiveSheet.ListObjects(1).Range(1, 1).Select
If ActiveSheet.ListObjects(1).AutoFilter.FilterMode = True Then
ActiveSheet.ListObjects.Item(1).AutoFilter.ShowAllData
End If
A = ActiveSheet.ListObjects(1).Range.Rows(1).Find("Remove from List").Column - _
ActiveSheet.ListObjects(1).Range.Column + 1
ActiveSheet.ListObjects(1).Range.AutoFilter field:=A, Criteria1:="="
Range(SelectedCell).Select
End sub
I have a dynamic query in a sheet that uses a parameter in cell C1. So, In column A there are codes, and in column B are those code descriptions and based on the value in cell C1, the query will refresh to display codes related to the value in C1. When the value in C1 changes, I get a "Run-Time Error 1004: MS Excel is refreshing some data. Please try again later", every time. When I select "End" it goes away and refreshes the query successfully, after a couple seconds, Does anyone know the reason behind this? Can I get rid of it?
There is a search feature. when the user clicks "Search" to search for the codes they're looking for, the search button is assigned a macro that essentially just navigates to the sheet which displays the codes. However, in that macro, I added this bit of code:
Sheets("department_lookup").Cells(1, 3).Value = Sheets("lookup").Cells(2, 2).Value
With Sheets("department_lookup") .Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=.Cells(1, 3).Value, VisibleDropDown:=True End With
I found this occurs when you are using a combination of Excel native queries and VBA DB code. For me, I found I couldn't access the database using an OLEDBConnection when Excel was refreshing using it's native refresh function.
SOLUTION:
I had to turn BackgroundQuery = False on the native queries.
(This can be done from the Query properties using the GUI)
EXPLANATION:
BackgroundQuery allows you to use the spreadsheet while it is refreshing data. This is okay for a user who wants to use a spreadsheet but not for a VBA macro that wants to talk to that same database. When set to false it causes Excel to hang during a refresh. This stops your operations from colliding.
If this isn't your exact scenario look for similar collisions where 2 queries are stepping upon eachother.
For people like me who knows a little bit about ODBC, from Excel, go to Data tab. Click on Queries & Connections. Then, right-click each connection tile, click Properties. Under usage, untick Enable background refresh. That should do the trick. At least, for me, that worked. :D
I have a pivot table, created manually. There are ways users need to be able to filter it which can't be done through Report Filters or Slicers, so I thought VBA would be the way to go. I've built a skeletal user form, and am now at the tricky stage of writing the code behind it.
I'm trying to use the PivotFilters function, but this isn't working. I will admit that I am an utter novice at VBA, so it may be a simple error I've made, or perhaps PivotFilters aren't the right tool for the job? Here is an example:
'ok button - this is the big one, telling the button what to do with the selected options!
Private Sub CommandButton1_Click()
'targeting our familiar pivot table
Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables("PivotTable1")
'Whether to filter out low sample size brands
Dim pvtFieldSasz As PivotField
Set pvtFieldSasz = pvt.PivotFields("SampleSize")
If OptionButton1 = True Then
pvt.pvtFieldSasz.PivotFilters.ClearAllFilters
pvt.pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
End If
End
The above example is something that can be done with Report Filters or Slicers, but I wanted to put all the relevant options in the user form, to make it a one-stop-shop for the user.
The error message I get is "Run-time error '438': Object doesn't support the property or method".
I haven't moved on to the trickier ones yet, as I want to master the basics. As an example of what I'd like to achieve in a different filter:
The filter would refer to one of three separate columns, depending on the option selected by the user. The user would have three option buttons, of which they can only select one at a time. If they select OptionButton1, I would like the pivot table to be filtered on field X = 1. If they select OptionButton2, it should filter on Field Y = 1. OptionButton3 corresponds to Field Z = 1. None of these fields X, Y or Z will actually be displayed in the pivot table as a row or column, but they are part of the source data and are included in the selection behind the pivot table.
Thank you for your patience helping this stumbling novice - very grateful for any tips!
The problem is in these 2 lines:
pvt.pvtFieldSasz.PivotFilters.ClearAllFilters
pvt.pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
First, pvtFieldSasz already represents the pivot field. Trying to specify it with pvt.pvtFieldSasz won't work because pvtFieldSasz is not a property or method of the pivottable, pvt. Instead, you should just use pvtFieldSasz. This will also be a problem in the immediately following statement.
Second, the ClearAllFilters method does not apply to the PivotFilters Property of the pivotField. Instead, it applies to the pivotField itself.
Combining these two issues for working code:
pvtFieldSasz.ClearAllFilters
pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
As a sidenote, I'd also specify the sheet instead of using ActiveSheet as ActiveSheet can have issue, so
Set pvt = Sheets("Sheet1").PivotTables("PivotTable1")
I'm recording a macro to automate some Excel reports and have encountered the following bug whenever I try and run an iserror(search) formula:
Run-time error '1004': Application-defined or object-defined error
I have two lists. The formula iterates through the first list and compares the values with those of the second list, hiding any matching values.
The formula in Excel is like this only with a wider criteria range:
=AND(ISERROR(SEARCH($B$3212,B2)),ISERROR(SEARCH($B$3213,B2)))
It works perfectly when I insert the formula directly into the spreadsheet cell however I get an error when I record and later run the macro using the same formula.
EDIT 2
I got the formula insertion to work through the macro but now I cannot filter the data as before, even when I do it manually without the macro.
Below is a link to a picture giving an example of the type of lookup I'm trying to achieve, previously it worked perfectly and removed all the rows which contained a string from the 'to remove list' now I cannot get it to filter at all. I've tried removing the macro after saving in notepad in case the file had become corrupted but it still does not filter as before. What could be causing this?
This is how the lookup works
Cell [A13] would contain the aforementioned ISERROR formula in this example.
This formula doesn't translate well to VBA in its current form. You should use the VBA Instr function instead of the worksheet function Search.
Function FindSubstring() As Boolean
Dim rngFindText As Range
Dim rngWithinText As Range
Set rngFindText = Sheet1.Range("B3212")
Set rngWithinText = Sheet1.Range("B2")
FindSubstring = InStr(rngWithinText, rngFindText)
End Function
Sub foobar()
Debug.Print FindSubstring
End Sub
You are asking Excel a question to tell you to find the contents of $B$3212 in B2 and to find if again.
Usually the SEARCH is used to find the contents of one thing in another, by using it again the AND statement you are asking it again ... and for what?
Hence the question does not make sense.
What I think you might be asking if just once and if there is an error meaning it did not find it there in this instance for it to return 0.
=IF(ISERROR(SEARCH($B$3212,B2)),0,SEARCH($B$3212,B2))
I figured this one out, the original 1004 error was caused by vba only partially recording the formula, the solution involved simply going into the debugger to find which line hadn't been translated correctly and editing that line. I then had to edit the formula so as to be able to filter out values acording to my criteria and ended up with a formula closer to this:
=AND(ISERROR(SEARCH("Value1",B2)), ISERROR(SEARCH("Value2",B2)))
This is code from Excel 2010. All of the code resides within the workbook itself. Information is gathered using internal forms and the code is run.
I have code that retrieves data from a spreadsheet and populates an object with that data. The row number is dynamic as it is dependent on the form input. The column is by the header, not the column number. The following code works perfectly but for two anomalies:
cTank.RowForTankSpecs = rNum
cTank.MP = .Cells(rNum, Range("MP").Column).Value
cTank.Form = .Cells(rNum, Range("formName").Column).Value
cTank.TankProcess = .Cells(rNum, Range("Process").Column).Value
cTank.Location = .Cells(rNum, Range("Location").Column).Value
cTank.TankName = .Cells(rNum, Range("Tanks").Column).Value
cTank.tankID = .Cells(rNum, Range("TankID").Column).Value
First:
The cTank.TankName is retrieving information from a column named "Tanks". That column does not exist. The actual column header is "Tank". But, it is retrieving the correct information. If I change the name to what it really is (Tank), it does not work.
Second:
When the cTank.TankID line is executed, I get the following error on the Range("TankID"):
Runtime Error 1004: Method 'Range' of object '_Global' failed
This one has the appropriate header (column header), but it is not recognizing the range.
I have tried simple things such as changing the order of the code, but it doesn't help. As earlier stated, the other lines work. Later in the program, information is gathered in the same manner but using another worksheet from the same workbook, and none of them are working. I've double checked that strings are strings and integers are integers, etc. I've double checked the column headers match the range names. Nothing seems to jump out at me.
I would appreciate any input you may have on the situation.
Thanks in advance.
Steve
Ok. Being pretty sure my code was correct, I went to the spreadsheet itself. For some reason it was recognizing only certain columns and it was recognizing one of them incorrectly. So I started highlighting the columns that worked and also the columns that didn't. What I noticed was that on the columns that were being recognized, that column header was displayed where the cell location is normally displayed whereas on the columns that were not being recognized, the cell location (i.e. A1, A2, etc.) for the header was being displayed and not the header title itself. The incorrect label was showing up for one of them. As it turns out, the mislabeled column was one that I had used for a form dropdown menu. So, I checked the name manager, and the ones that were working were listed. So anyway, using the name manager, I added named ranges using the headers. Now, when I select the columns, the column header(named range) appears in that window and now, the code works.
Thanks guys for your input. I really appreciate it.
Two things you can do:
Do not use use Range, but as it seems you are using names, use Names("Yourname").Referstorange.
OR
Make sure your names are set up correctly using the Name Manager in Data Ribbon.