I have an input sheet in Excel where the use enters a number of account details. Once the form is filled in they press a button that generates an input file using VBA. The first step is that a new worksheet is generated and the name of this sheet in a combination of the clients name and the a hard coded word. As you can imagine this sheet needs to be a variable because this input sheet is being used by a number of people on a number of accounts.
The code I have used to generate the new worksheets is:
ActiveWorkbook.Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = Trim(Sheets("Account Input").Range("B8").Value) & "_ACC"
I'm now trying to save this as a variable so that I can call the variable throughout my code. The syntax I have tried is:
Set WsAcc = Trim(Sheets("Account Input").Range("B8").Value) & "_ACC"
The problem is I get a run-time error 424 object required. I have tried it without the trim and still get an error. Run-time eroor 1004 application-defined or object-defined error.
I know there must be a better way to achieve the desired results, I'm just not too sure.
Thanks in advance.
Replace:
Set WsAcc = Trim(Sheets("Account Input").Range("B8").Value) & "_ACC"
with:
Set WsAcc = Sheets(Trim(Sheets("Account Input").Range("B8").Value) & "_ACC")
Related
I am writing a VBA code to enter students' marks automatically from an excel sheet into a website. I am able to log in, navigate to the required page. But when I am trying to enter the marks using VBA into the table, I get the following error.
Run-time error '91':
"Object variable or with block variable not set"
at the line:
doc.getElementById(celID).Value = ThisWorkbook.Sheets("Marks").Range(ceL).Value
where celID is obtained using: celID = "marks-" & regno & "-" & qno and I get it as "marks-171FA04001-1"
The HTML code for the page is:
Web page HTML code
I need to verify the table's cell name and enter the marks because the name of the cell changes for different students like marks-rollno-questiono. So I need my code to verify if I am entering the marks for the respective student and for the respective question number. Please help me. I am using a for loop for entering the marks of all the questions of all the students.
VBA Code for Data Entry
Screenshot of the table:
![3]: https://i.stack.imgur.com/wIBL7.png
I'm currently working on a macro that modifies a standard format of spreadsheet. This will be used for many spreadsheets by several people in my company. So I've created an addin that everyone should be able to install and use.
However not all the spreadsheets will have the same name for each sheet.
Originally I tried referring to the sheets as
Sheet1.Range...
This gives
Run-time error '424':
Object required.
I can make it work with
Worksheets('name_of_worksheet').Range
But this requires the user to change that every time they wish to use the addin in a different spreadsheet, or to rename the sheets.
Is there another way of referencing the 'first' and 'second' worksheets? They will always be in that order and created in that order.
I believe this is to do with having saved the macro as an addin since I've never had this issue.
Thank you!
Edit:
The reformatting of the spreadsheet depends on data found in sheet 1 and sheet 2. So within the macro I need to be able to refer to both of those sheets. The name of the sheets will not always be the same, so I would rather not have to use:
Worksheets('name_of_worksheet').Range
Using Active Sheet is also not ideal since there will only be 1 active sheet, and I need to access both sheets.
Sheets(1).Range
does not work either, I get the error:
Run-time error '424':
Object required.
You could make a ugly hack and loop two sheets.
i = 1
For Each Sht In ThisWorkbook.Sheets
If i = 3 Then Exit For
Debug.Print Sht.Name
i = i + 1
Next Sht
This loops all sheets and when two sheets has been looped the code exits the for loop.
In the loop you could save the sheet names to variables or perhaps use this as the base of your code and add your code instead of the debug line.
I'm using Excel for Office 365 MSO 64-bit.
I want to write a VBA macro that selects different worksheets in a workbook based on the worksheet's name.
For example, I have two lines of VBA code that activate a workbook and then select a specific sheet in the workbook by the sheet's name.
Windows("myworkbook").Activate
Sheets("mysheet").Select
However, I have to work with some sheets that contain icons or emojis in them. For example, there is a worksheet that has this name: "🚑 Patient".
If I try to paste the icon/emoji into VBA like this: Sheets("🚑 Patient").Select, the icon does not show up in the VBA editor. Instead, I get Sheets("????? Patient").select.
I have also tried to use ChrW() to encode? the ambulance character (see here: https://www.compart.com/en/unicode/U+1F691)
When I run this macro below), I get an invalid procedure call or argument as noted below.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(128657) ' get invalid procedure call or argument here
Sheets(x & " Patient").Activate
End Sub
I also tried code for ambulance... also tried ChrW(&H1F691), but I get the same error.
My suspicion is that I am using the wrong argument for ChrW(), but I'm lost.
edit: So, the docs say that my argument for ChrW() is out of range. That helps explain the error, but I'm still missing a work-around.
Question: Is there a way to refer to use VBA to select worksheets that have an icon/emoji as part of their name?
I know you can also refer to worksheets by index number like this Sheets(3).Select.
However, there will be instances where I don't know the index of the sheet ahead of time, but I will know the name of the sheet, so it is preferable for me to call the worksheets by name.
Thank you.
In addition to the self-answered response, when working in a single workbook, the coder can assign a CodeName to the sheet in the VBA IDE, and then use that CodeName directly. This is really only valid if the Sheet is not re-created (i.e. is a permanent sheet in the book) at any stage, because a new/copied sheet will be automatically given a new CodeName by Excel.
For example, if given the CodeName shtPatient (see picture bellow), the code could be:
Sub SelectWeirdSheet()
' Windows("MYWorkbook.xlsx").Activate '<-- this approach has limitations
shtPatient.Activate ' See my comment below about the limitation - this will not work as expected in this example.
End Sub
Note: https://stackoverflow.com/a/10718179/9101981 explains why not to use Activate, but I have left the code as-is for the purposes of this answer. Also look at Using Worksheet CodeName and Avoiding .Select & .Activate. Another limitation noted is that the CodeName is only valid for the workbook that the code is in - so may not be applicable in this case.
I have highlighted the CodeName parts of the IDE in the image below, see how "Test Patient" is not called "Sheet7", but instead has a meaningful name that I gave it in the properties window below.
In order to properly address the emoji, it should be split into two separate unicode characters.
In this case, it would be x = ChrW(&HD83D) & ChrW(&HDE91)
Those two unicode characters make up the ambulance emoji.
So, this Macro now works.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(&HD83D) & ChrW(&HDE91)
Sheets(x & " Patient").Activate
End Sub
Found the solution on reddit of all places https://www.reddit.com/r/excel/comments/6pq1r1/vba_how_can_i_write_emojis_using_chrw/
I am trying to rename an excel sheet to the contents of a cell on that sheet using VBA.
I have created a separate module called Rename_Sheets and various posts online suggest the following code:
Sheets(3).Name = Sheet3.Range("A5")
or very similar variations thereof.
Using debug.print, both parts of the above code return the expected results, i.e. the sheet name and the cell text.
When I run the code as quoted I get the message "Application-defined or object defined error".
I don't understand why I get the error message.
u have a typo
replace
Sheet3.Range("A5")
to
Sheets(3).Range("A5")
good luck
Sheet3.Range("A5") returns a Range Object. What you want instead is the Value Property of the returned Range Object:
Sheets(3).Name = Sheet3.Range("A5").Value
Note though, that once the code runs once and Sheet3 is renamed, it will start throwing an error again because "Sheet3" will not exist. If you made it Sheets(3).Range("A5").Value similar to the other side of the equal sign, this code would would work more than once.
I have an Excel file with one sheet called Master.
I have a button to delete a value from the database. This is what I have written that's relevant:
Sub delete_this()
On Error GoTo ErrorCatch
simple = Sheets("Master")
.........
ErrorCatch
MsgBox(Err.Description)
End Sub
It fails immediately when I use Sheets, saying "Application-defined or object-defined error." However I'm using other code that I know works as a reference and they called this no problem (though their file had multiple sheets).
Also in general I'm new to VBA and find it pretty unintuitive with its error messages, and finding out variable values. So any advice there would also be appreciated.
As mentioned in a comment by Scott Craner, you need to use the Set statement whenever assigning an object to a variable when using VBA. This little gotcha doesn't exist in VB.NET, and is easy to forget about these days.
Set simple = Sheets("Master")