Renaming an Excel worksheet generates an error - excel

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.

Related

Select headers in table - method range of object _ global failed - works when run with button? but not stepping through

When stepping through my code i get error 1004 on this line.
Range("tablename[[#Headers],[startheadername]:[lastheadername]]").Select
Now the weird thing is, I use it every month without issue via a button.
This error only pops when im stepping through the code.
this works instead
ActiveSheet.ListObjects("Tablename").HeaderRowRange(1).Resize(, 7).Select
The error code involves the words "range of object" which made me think that maybe there was a way to use the object in another manner. my original code was just a simple range reference.
I dont have particular insight into listobjects, i mainly copy pasted and tested. But headerrowrange(1) is the first column header in the tablename, resize(,7) expands it from 1 to 7 and .Select does what it does.
So this new code selects the first 7 table headers without error 1004.
This site got me there https://exceloffthegrid.com/vba-excel-tables-code/

Is it possible to paste a range into a range using cell index reference?

I am very new to VBA but I'm trying to copy a range from one workbook into another.
Within the macro I am already exporting a file with a variable in the file name that generates a version number - the version number corresponds to the column I want to copy the data into, hence the need to use range(cells()) approach. DestCol returns the column number I want to paste into no problem.
I have tried a load of suggestions I've found online but just can't get it to work, including referencing the workbook with the range, paste special etc.
eg. Workbooks().Worksheets().Range(Workbooks().Worksheets().Cells(2,2))
Any help is much appreciated.
Stepping through the macro I can see the 'Cells...' part has the message "Application-defined or Object-Defined error"
Workbooks("adjuster.xlsm").Worksheets("Exp_build").Range("C1:C5000").Copy
Workbooks(FN).Worksheets("Log").Range(Cells(2, DestCol)).PasteSpecial Paste:=x1PasteValues

VBA - Variable Worksheet

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")

VBA - Can't use Sheets object without error

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")

Offset selection in Excel from Access

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

Resources