What does this vba code mean? - vba

I'm trying to duplicate some code a former coworker of mine wrote in an excel spread sheet, but I'm a little new to VBA so I'm having trouble parsing it out. Here's the part of the code I'm confused about.
Private Sub cbWriteIES_Click()
Dim Hangle, Vangle As Double
Dim header(1 To 15) As String
'ASSIGN HEADER TO VARIABLE
For i = 1 To 15
header(i) = Sheet1.[header].Cells(i, 1) & Sheet1.[header].Cells(i, 2)
Next i
Can someone explain to me what's happening in the for loop? It runs fine in the original workbook, but when I copy and paste into my workbook it throws a 424 object required error. It looks to me like he's associated an object with Sheet1 called [header] but I can't seem to find where he did that. Any insight into this would be greatly appreciated. Thanks!

"header" is a named range on sheet1. If yo uare using excel 2010 you can go to the "Formulas" tab and click "Name Manager" to see the named range.
The for loop is setting the array "header" to the first 15 rows in the second column of the range named "header" on sheet1.
The 424 Object Required error is coming up because you do not have a range named "header" on sheet1 in the workbook you copied this to.

Related

excel listObject issue

I have inherited a spreadsheet full of VBA I know next to nothing about.
When the data is refreshed, the spreadsheet re-formats itself via the VBA. I had to change some tab names and table names and now when the auto reformat kicks in it gives an error as it is looking for the old table name.
Sorry if this s a bad explanation/terminology, I really never get involved with VBA usually!
So the error I get is
Run-time error '450': Wrong number of arguments or invalid property assignment
debug takes me to this line
For Each rptTable In sID.ListObjects
rptTable.TableStyle = teamName <--- this is the highlighted line in debug
oldSize = rptTable.ListRows.Count
If oldSize < teamSize Then
teamName has the new value, rptTable.TableStyle has the old value.
the variables are defined as follow i believe:
Sub FormatSheet(sID As Worksheet)
'Clear debugWindow
Call clearDebugConsole
Application.StatusBar = "Formatting Teamsheet ... "
'Change Active Sheet to the Sheet being Formatted
Worksheets(sID.Name).Activate
Debug.Print "* Entered FormatSheet Sub"
' Set Variables
....
Dim rptTable As ListObject
I basically trying to work out how the ListObject is being populated and where the old name is being picked up from? a global search in the excel spreadsheet doesn't pick it up
Well, it seems that teamname has a value that does not correspond to anything in TableStyles.
stop the code on that line and see what value variable teamname holds. There needs to be value that exists in your table styles. My guess is there is empty string. You need to assign it something like "TableStyleMedium2" etc.. You can find these names in the design ribbon -> table styles. Use the value without spaces.

Excel error - object doesn't support this property or method

This line of code:
Y.Sheets(1).Cells(i, 1) = Y.Sheets(1).Cells(i, 1).value & " (" & Right(UserAndEid, Len(UserAndEid) - InStr(1, UserAndEid, "Emp") - 7) & ")"
Throws this error:
Excel (2007) VBA Object doesn't support this property or method
Y is a declared workbook variable. Sheets(1) is the sheet I am addressing (not using the sheet caption name as it is changed by the same procedure).
UserAndEid is a piece of text taken from a cell in another declared workbook.
All I am doing is adding the peice of text to that which is already in the cell in row "A". I had it working yesterday, am sure I haven't changed anything, but today it will not work.
Any ideas?
Edit
UserAndEid is declared as a string but will contain numbers.
Dim UserAndEid As String
The whole line is highlighted in yellow in the debugger.
Thanks to all for the pointers,they helped me find the issue:
Sheets(1)
I remembered that I had added a chart to the workbook and this was Chart1.
As Sheets includes Charts and Worksheets the addition of Chart1 made Sheets(1) ambiguous, as it could no longer only be referring to 'Worksheets1'.
I have to say that the error message could have helped more here - if a variable is ambiguous the error message will say so, why not in the case of objects?

Subscript out of range when copying charts from excel to word

Hi i'm facing a subscript out of range error when I tried to copy a chart from excel sheet to word document. I checked the worksheet name it seems to be fine. I tried changing to ThisWorkbook.Sheets(wsname) but i received an application define type error. How do I go about resolving this issue.
With ThisWorkbook.Worksheets(wsName)
.ChartObjects(1).Activate
ActiveChart.ChartArea.Copy
End With
The name of the worksheet I'm trying to reference
I have finally figured out what's the problem.
Since I'm referencing a chartsheet and not a worksheet, I have to use .Charts instead of .Worksheets. In addition since it is a chartsheet, the chartsheet itself is a chart object, hence .ChartObjects(1) is not required, only .ChartArea.Copy
Correct Code:
With ThisWorkbook.Charts(wsName)
.ChartArea.Copy
End With

Excel VBA - Can I manipulate data on another workbook?

Background:
Every month I need to run a report. However, before I can do this, I must export some data in to excel. Once the data is in excel, I have to alter some columns and get the format correct before it is appended to the end of another document to do some analysis on.
What I would like:
I would like to have the document that I append the data to open. I will then export the data in to excel from my program (excel just opens with the data and it is not saved anywhere) and from my larger document, run a VBA script that will alter the data on the other workbook (Book1) so that it can be copied over to the analysis document when in the correct format.
What I have so far:
I have started basic. So far all I am trying to do is set all the cells to the correct height to make it easier to read. However, when I run this code, I get:
Run-time error '9':
Subscript out of range
The code I have so far is:
Sub Data_Ready_For_Transfer()
' Format all cell heights to 15
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
Cells.RowHeight = 15
End With
End Sub
It appears to be having issues with the With Workbooks("Book1.xlsm").Worksheets("Sheet1") part of the code. I have also tried With Workbooks("Book1").Worksheets("Sheet1") and I have tried this with the open, unsaved document and a saved version of the workbook.
Am I missing something obvious?
As follow up from comments, workbook Book1 was opened in another instance of Application object.
In that case this code should work (for already opened workbook):
Sub Data_Ready_For_Transfer()
Dim wb As Workbook
Set wb = GetObject("Book1")
'if we get workbook instance then
If Not wb Is Nothing Then
With wb.Worksheets("Sheet1")
.Cells.RowHeight = 15
End With
End If
End Sub
One more issue, I've changed Cells.RowHeight = 15 to .Cells.RowHeight = 15 to specify, that Cells belongs to workbook Book1 sheet Sheet1.

Subscript out of range Error after renaming sheets

I have done a small project, which consists of 5 excel sheet in, code is working fine and I am getting exact result also, but if I rename sheets from sheet1 to some other name I am getting Subscript out of range Error.
What is the reason for this and what needs to be done to overcome this. Please help.
Below is the code
Public Sub amount_final()
Dim Row1Crnt As Long
Dim Row2Crnt As Long
With Sheets("sheet4")
Row1Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
Row1Crnt = 2
With Sheets("sheet3")
Row2Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
There is nothing wrong with the code per se. You will get Subscript out of range error if Excel is not able to find a particular sheet which is quite obvious since you renamed it. For example, if you rename your sheet "Sheet3" to "SheetXYZ" then Excel will not be able to find it.
The only way to avoid these kind of errors is to use CODENAME of the sheets. See Snapshot
Here we have a sheet which has a name "Sample Name before Renaming"
So consider this code
Sheets("Sample Name before Renaming").Range("A1").Value = "Blah Blah"
The same code can be written as
Sheet2.Range("A1").Value = "Blah Blah"
Now no matter how many times you rename the sheet, the above code will always work :)
HTH
Sid
The basic issue is that you are referring to sheets using their common names and not their codenames. Whenever you refer to Sheets("sheet4"), you are relying on the sheet having that name in Excel. Codenames are the names assigned in Visual Basic so the end user does not interact with them/as a developer you can change the Excel names any time you like
Using code names is covered at around 9:40 in this Excel help video. You'll note they are quicker to type than the Excel names as do not require the 'Sheets()' qualifier
I couldn't see Sheets("Sheet1") in your code sample but you can switch to codenames for all sheets very quickly by finding/replacing all examples of e.g. 'Sheets("Sheet2").' with 'Sheet2.'
Refer to each sheet by their code names instead. They are set to Sheet1, Sheet2 etc as default, but you can rename them in the Properties window for each sheet if you want. This way you can write your code like below instead, regardless of what you name the sheets.
With Sheet1
Row1Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
Row1Crnt = 2
With Sheet2
Row2Last = .Cells(Rows.Count, "B").End(xlUp).Row
End With
etc...
I wanted to share my experience battling this problem. Here is the mistake I committed:
Dim DailyWSNameNew As String
lastrow = Sheets("DailyWSNameNew").Range("A65536").End(xlUp).Row + 1 -- This is wrong as I included a placeholder worksheet name in quotes
Correction:
lastrow = Sheets(DailyWSNameNew).Range("A65536").End(xlUp).Row + 1
This solved it.
I encountered this error earlier today but could not use any solution above, I did however eventually managed to solve it myself.
My situation was that I had a list contained in column A. For each cell with a value I stored the value in a variable, created a new sheet and named the sheet according to the value stored in the variable.
A bit later in the code I tried to select the newly created sheet by using the code:
Sheets(ValueVariable).Select
I encountered the "Subscript out of range" error and I couldn't figure out why. I've used similar code before with success.
I did however solve it by casting the variable as a string. Declaring the variable as a string did not seem to work for me.
So, if anyone else encounter this error and want something to try, perhaps this will work for you:
Sheets(Cstr(ValueVariable)).Select