Subscript out of range Midcoded error - vba

Usually with a subscript out of range error, something is misspelled. This time I can guarantee that it is not misspelled. I am declaring workbook and sheets names as variables to be used later for simple copy paste stuff. But when I define one of my sheets as Set main=wb.Sheets("Primary") I get the error, subscript out of range. I did't believe it, so I copied what I wrote and pasted that as the Sheet name. Same error. Therefore, their must be an issue with how I set up my Workbook/Worksheets. Any help much appreciated.
Sub copyPaste()
Dim wb As Workbook
Dim main As Sheets
Dim hypo As Sheets
Set wb = ThisWorkbook
Set main = wb.Sheets("Primary")
Set hypo = wb.Sheets("Hypo")
Application.CutCopyMode = False
wb.main.Range("D22:D46").Value = wb.main.Range("D22:D46").Value
End Sub

You want Worksheet (singular) not Sheets (plural) (Sheets is a collection of worksheets)
Dim main As WorkSheet
Dim hypo As WorkSheet
and here you dont use the workbook object because it's already defined as part of the worksheet reference. Change it to this
main.Range("D22:D46").Value = main.Range("D22:D46").Value

Related

VBA: Referencing a Worksheet in the Active Workbook

While this seems very basic, I am continually getting an error message while trying to select a cell in a certain sheet on my workbook in my Macro. Does any one know why this will not work? I'm getting error message Run Time Error '1004'.
The sheets name is "Sheet1"and my code is below:
Application.ActiveWorkbook.Worksheets("Sheet1").Range("N2").Select
It's bad practice to use ActiveWorkbook when you don't need to. It's always better to set your workbooks and worksheets to actual variables that you can call on. I think your code is activating another workbook then trying to select a range in a worksheet it can't find.
Sub TryThis()
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = Workbooks("myWorkbook.xlsm")
Set ws = wbk.Worksheets("Sheet1")
'Now when we say "ws." it is actually "Workbooks("myWorkbook.xlsm").Worksheets("Sheet1")."
'This is okay to start with but it's better to work with the cells directly
ws.Select
Range("N2").Select
Selection = "myText"
'This is much faster and you won't have to worry about what is currently selected
ws.Range("N2") = "myText"
End Sub

Specify the workbook when working on a worksheet in VBA

I would like to get the value of cell. I have multiple workbooks, and some of the workbooks have sheets with the same name.
For example, test.xlsx and test2.xlsx both has a sheet named sheet1.xlsx
So, when working on a sheet, I would like to specify the workbook. I use wb.sh.*expression* all the time, and I am surprised that this does not work.
What am I missing here conceptually?
Code:
set wb1 = Workbooks("Test1.xlsx")
Set sh1 = Worksheets("Sheet1")
Debug.Print wb1.sh1.Range("A1").Value
Code which would work, but is not specific enough:
set wb1 = Workbooks("Test1.xlsx")
Set sh1 = Worksheets("Sheet1")
Debug.Print sh1.Range("A1").Value
Note: Test1.xlsx has a sheet named Sheet1
When you open the workbook, the Open method gives you the Workbook object reference - hold on to it instead of discarding it:
Dim wb As Workbook
Set wb = Workbooks.Open(path)
That way you never need to query the Workbooks collection and supply a hard-coded file name to get your workbook.
Next up, the Worksheet reference:
Set sh1 = Worksheets("Sheet1")
The Worksheets collection is a property that belongs to a Workbook object. Indeed, that's not "specific" enough: if you don't qualify the property call, then you're implicitly referring to ActiveWorkbook - which may or may not be the workbook you need to be using.
That's where that wb reference comes into play:
Set sh1 = wb.Worksheets("Sheet1")
Lastly, what you have here:
Debug.Print wb1.sh1.Range("A1").Value
Is not only illegal, it's overkill: sh1 already knows what Workbook object it belongs to - you can get that object reference through sh1.Parent and compare it to the wb reference:
Debug.Assert sh1.Parent Is wb
sh1 is a local variable, not a member of the Workbook interface: that's why you can't do wb1.sh1.
I use wb.sh.expression all the time
If your code ever worked, I guarantee you don't.

Excel VBA to Work Across Workbooks

I am very new to VBA coding and don't have very good understanding of what I am doing to be honest. But here I go.
I am looking to see if:
Can VBA codes have dyname values? So instead of the code saying execute on a set sheet (e.g "Sheet1") that value changes depending a value in a certain cell.
To trigger a VBA on another workbook. For example I want to run a VBA from Workbook A that triggers a VBA on Workbook B.
To fully explain I want to open Workbook A (and Workbook B if needed, it doesn't matter) and click a button that runs a VBA on Workbook B but on a certain Sheet depending on the value of a cell in Excel A (if the cell says "sheet3" the VBA runs on "sheet3" on Workbook B). I also want cells in Workbook A to reference cells in Workbook B but the the sheet name to by dynamic. For example I have pasted the basic cell reference bellow but instead of having Sheet1 I want it to change depending on the value in a cell.
='[Workbook B.xlsx]Sheet1'!$A$4
I know this sounds very complicates and confusing, but if I could get any help that would be greatly appreciated.
Sub ReportStepOne()
Dim myRow As Long
myRow = 4
Rows(myRow).Value = Rows(myRow).Value
Dim rng As Range
Set rng = Range("A4:AC200")
rng.Cut rng.Offset(1, 0)
Range("A1:AC1").Copy Range("A4:AC4")
End Sub
I want to:
edit this code to make it fire on a certain sheet
make it so the sheet name is referenced to whatever is in cell A o Sheet2 in Report.xlsm.
Run a macro in Report.xlsm that runs the above script (which is called "StepOne" in a file called "Historical Data.xlsm"
The code below takes the value of cell A4 on sheet2 in Reports.xlsm and sets the ws variable to the sheet in Historical data.xlsm which is then used for the rest of the code. If possible I'd advise against having your subs spread out over multiple projects but that is just my opinion. I think it is easier to use proper referencing like below.
Since you want a button trigger on the Report.xlsm I'd suggest moving this code to that workbook. If properly referenced it you can open, edit, save and close any workbook from a single project which again, in my opinion is easier than calling subs in a different project.
Sub ReportStepOne()
Dim wbHis As Workbook, wbRep As Workbook
Dim strWsName As String
Dim ws As Worksheet
Set wbHis = Workbooks("Historical data.xlsm")
Set wbRep = Workbooks("Reports.xlsm")
strWsName = wbRep.Worksheets("Sheet2").Cells(4, 1)
Set ws = wbHis.Worksheets(strWsName)
With ws
With .Rows(4)
.Value = .Value
End With
With .Range("A4:AC200")
.Cut .Offset(1, 0)
End With
.Range("A1:AC1").Copy .Range("A4:AC4")
End With
End Sub
To trigger a VBA on another workbook
Option Explicit
Sub RunVBA()
Dim xlApp As Excel.Application
Dim xlWorkBook As Workbook
Set xlApp = New Excel.Application
Set xlWorkBook = xlApp.Workbooks.Open("C:\Users\Om3r\Desktop\Book1.xlsm")
xlApp.Visible = True
xlWorkBook.Application.Run "Module1.SubName" ' Modulename.Subname
End Sub
To reference worksheet use
Sub CopyRange()
'// From sheet1 to sheet2
Worksheets(2).Range("A1").Value = Worksheets(1).Range("A1").Value
End Sub

vba create a new sheet and change name

I am a VBA new user. My purpose is copy a existing worksheet to a new one. And then change the new worksheet name to a proper name. However, I got the VBA Error Message like,
vba runtime error 9 subscript out of range(at the line Set new_ws =Active...)
The VBA code will be the following; thanks in advance
Sub CreateWS()
Dim ws As Worksheet
Dim new_ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Bus Voltage")
ws.Name = "Bus Voltage_All"
ws.Copy Worksheets(Sheets.Count)
Set new_ws = ActiveWorkbook.Worksheets("Bus Voltage_All(2)")
new_ws.Name = "Bus Voltage"
End Sub
It looks to me like you have an issue with a space.
Your line Set new_ws = ActiveWorkbook.Worksheets("Bus Voltage_All(2)")
Should be
Set new_ws = ActiveWorkbook.Worksheets("Bus Voltage_All (2)")
Notice the space after _All? You were missing that in your code.
A (potentially) helpful tidbit is that when you use the .Copy method, your new worksheet is automatically activated, so you can use this code instead (to avoid errors with the space):
ws.Copy Worksheets(Sheets.Count)
Set new_ws = ActiveSheet
And, on the topic of referencing sheets at all (though not directly related to your question), you can use something called the CodeName to reference the sheet.
For example:
The CodeNames are Sheet1, Sheet2, etc.
The code:
Worksheets("Old Data").Activate
And
Sheet1.Activate
are equivalent.
The major benefit to referencing sheets this way is that if your worksheet name changes, your code won't break.

Using cell value as range VBA

I am trying to have a range (call it "A5:L10") be picked up from a cell. In other words, my code looks something similar to that below:
Dim summ_rng1 As String
summ_rng1 = Sheet11.Cells(17, 3).Value
Workbooks(wb).Sheets(summ).Range(summ_rng1).......
Where summ_rng1 = "A5:L10"
I have done this same thing for the workbooks and sheets in my code and it works fine, but when I try to replace the range reference with the variable summ_rng1 it does not work.
Any idea on how to get the code to run with the range value as a variable, like that above? Thanks for the help!
Your code is working for me. I think it is related to your wb object, which may contain the workbook itself, rather than the name of the workbook. Try this :
Sub testSub()
Dim myRange As String
Dim wb As Workbook
Set wb = ThisWorkbook
myRange = wb.Sheets(1).Cells(1, 1)
wb.Sheets(1).Range(myRange).Select
End Sub