I have searched for a solution but can't seem to find one. If one exists, please point me to it.
The question is how do I name a range in VBA.
wrkSheet.Range("A1").Name = "Test"
Works fine but as soon as I change it to
wrkSheet.Range("A1:B2").Name = "Test"
gives me problems. Note, wrkSheet is a worksheet object that is defined earlier.
Thanks
you can use this to name a range. Not sure why wrkSheet is a string as it should be a worksheet object
Dim ws As Worksheet
Dim r As Range
Set ws = Sheets(1)
Set r = ws.Range("A1:B2")
r.Name = "test"
You're not naming a range there... you giving values to a excel range of cells...
To name a range you do
range = wrkSheet.Range("A1:B2").Name
Then you can do range(1,1) = "test" for example.
Related
I am creating a sheet that grabs data from another workbook and creates a pivot table based off the data.The error im running into is when I try to create the pivot table error '1004' Breaks the code, here is the line of code that breaks. If I attempt to fix the error it will instead run into a type mismatch error
Set rcPT = ActiveSheet.PivotTables.Add(PivotCache:=rcPTCache, TableDestination:=Range("A1"))
I am fairly certain it has something to do with the data range as it works when I use a different range but im not sure, it may be a easy fix and I just need another perspective. Here is the full code.
Dim wbReviewPivots As Workbook
Set wbReviewPivots = ActiveWorkbook
Dim wsPivots As Worksheet
Dim wbData As Workbook
Dim wsData As Worksheet
Dim endCell As Integer
Dim rcPT As PivotTable
Dim rcPTCache As PivotCache
Dim rcPlace As Range
' set variable equal to data sheet and pivot sheet
Set wbData = Workbooks.Open("//Workbook\\")
Set wsPivots = wbReviewPivots.Sheets("RootCausesCleaned")
Set wsData = Sheets("RawData") 'gets the last row within the rawdata sheet
endCell = wsData.Cells(Rows.Count, "A").End(xlUp).Row
'set the data range
wsData.Activate
Dim MyRange1 As Range, MyRange2 As Range, dataRange As Range
Set MyRange1 = Range("A1:E" & endCell)
Set MyRange2 = Range("G1:AA" & endCell)
Set dataRange = Application.Union(MyRange1, MyRange2)
'create root cause pivot chart
wbReviewPivots.Sheets("RootCausesCleaned").Activate
Set rcPTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=dataRange)
Set rcPT = ActiveSheet.PivotTables.Add(PivotCache:=rcPTCache, TableDestination:=Range("A1"))
With rcPT
.PivotFields("Cause").Orientation = xlRowField
.PivotFields("Cause (Sub Category)").Orientation = xlRowField
.PivotFields("Task ID").Orientation = xlDataField
End With
The dataRange excludes the G column because if its included another error is run into, Its not a required column so it doesn't matter if the columns excluded but I do think its making it difficult to create the pivot tables.
The excel table object could also be used, but this still runs into the same error.
You can't create a pivot table from non-contiguous ranges like you're trying to do by excluding column G.
Your Type Mismatch error is from trying to declare a PivotCache using a Range object for SourceData. This question, or this question, shows you how to use a String reference.
endCell should be a Long, not an Integer. (Just a thought, maybe use endRow or some other name, endCell sounds like a Range variable and might be misleading.)
Since dataRange is in a different workbook, you can use its Address with External:=True to get its full String reference including the workbook name. I'd use the xlR1C1 reference style since it sounds like A1-style can cause errors with larger data sets.
So to fix your errors, make these tweaks...
Dim dataAddress as String
Set dataRange = wsData.Range("A1:AA" & endCell) ' or maybe endRow
dataAddress = dataRange.Address(ReferenceStyle:=xlR1C1, External:=True)
Then within Set rcPTCache = ..., SourceData:=dataAddress.
I'm intending to conduct a macro which will open up a workbook from the specified path, and loop through its worksheets which has the names "Januari, Februari, Mars" specifically, to deduct the value from C34. C34 has a value recorded there every time, so it shouldn't change. However I want to copy it to the current worksheet, where the first target should be at AA73, the second at AA74 etc. My code is
Sub Test()
Dim myHeadings
Dim i As Long
Dim path As String
path = "C:\pathtofile\file.xlsx"
Dim currentWb As Workbook
Set currentWb = ActiveWorkbook
Dim openWb As Workbook
Set openWb = Workbooks.Open(path)
Dim openWs As Worksheet
myHeadings = Array("Januari", "Februari", "Mars")
For i = 0 To UBound(myHeadings)
Set openWs = openWb.Sheets("&i")
currentWb.Sheets("Indata").Range("AA73+Application.Match(i,Array,False)").Value = openWs.Range("C34").Value
Next i
End Sub
However the compiler says that the subscript is out of range at the row with
Set openWs = openWb.Sheets("&i")
Here I've tried to do "i", i, &i among other things, but it haven't changed. Also I've tried to use "ThisWorkbook" instead of "ActiveWorkbook" but it didn't help either. Does anybody have an input as to how to achieve this in a more proper way?
EDIT: Adapting to the response from Dave, it works to import the sheets. However I get an error in:
currentWb.Sheets("Indata").Range("AA73+Application.Match(i,Array,False)").Value = openWs.Range("C34").Value
Where I get Automation Error -2147221080 (800401a8) at said code snippet.
You have already put your sheet names into an array, so you can just call the sheet name from the array as:
Set openWs = openWb.Sheets(myHeadings(i))
I just started coding a few days back and am trying to use all dim variables, since that's what everyone has been saying to use. So, I am trying to clear formats using current region (basically all cells containing value or formatting). Here is my code and I get a compile error and VBA highlights the "Entire' portion of the last code. Any thoughts? I'm new and I can't figure out what I'm doing wrong.
Sub ClearFormatting()
Dim ws as Worksheet
Dim Entire As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set Entire = Range("A1").CurrentRegion
ws.entire.ClearFormats
End Sub
Sub ClearFormatting()
Dim ws as Worksheet
Dim Entire As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set Entire = ws.Range("A1").CurrentRegion
Entire.ClearFormats 'no ws.
End Sub
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
I already spent hours on this problem, but I didn't succeed in finding a working solution.
Here is my problem description:
I want to loop through a certain range of cells in one workbook and copy values to another workbook. Depending on the current column in the first workbook, I copy the values into a different sheet in the second workbook.
When I execute my code, I always get the runtime error 439: object does not support this method or property.
My code looks more or less like this:
Sub trial()
Dim Group As Range
Dim Mat As Range
Dim CurCell_1 As Range
Dim CurCell_2 As Range
Application.ScreenUpdating = False
Set CurCell_1 = Range("B3") 'starting point in wb 1
For Each Group in Workbooks("My_WB_1").Worksheets("My_Sheet").Range("B4:P4")
Set CurCell_2 = Range("B4") 'starting point in wb 2
For Each Mat in Workbooks("My_WB_1").Worksheets("My_Sheet").Range("A5:A29")
Set CurCell_1 = Cells(Mat.Row, Group.Column) 'Set current cell in the loop
If Not IsEmpty(CurCell_1)
Workbooks("My_WB_2").Worksheets(CStr(Group.Value)).CurCell_2.Value = Workbooks("My_WB_1").Worksheets("My_Sheet").CurCell_1.Value 'Here it break with runtime error '438 object does not support this method or property
CurCell_2 = CurCell_2.Offset(1,0) 'Move one cell down
End If
Next
Next
Application.ScreenUpdating = True
End Sub
I've done extensive research and I know how to copy values from one workbook to another if you're using explicit names for your objects (sheets & ranges), but I don't know why it does not work like I implemented it using variables.
I also searched on stackoverlow and -obviously- Google, but I didn't find a similar problem which would answer my question.
So my question is:
Could you tell me where the error in my code is or if there is another easier way to accomplish the same using a different way?
This is my first question here, so I hope everything is fine with the format of my code, the question asked and the information provided. Otherwise let me know.
5 Things...
1) You don't need this line
Set CurCell_1 = Range("B3") 'starting point in wb 1
This line is pointless as you are setting it inside the loop
2) You are setting this in a loop every time
Set CurCell_2 = Range("B4")
Why would you be doing that? It will simply overwrite the values every time. Also which sheet is this range in??? (See Point 5)
3)CurCell_2 is a Range and as JohnB pointed it out, it is not a method.
Change
Workbooks("My_WB_2").Worksheets(CStr(Group.Value)).CurCell_2.Value = Workbooks("My_WB_1").Worksheets("My_Sheet").CurCell_1.Value
to
CurCell_2.Value = CurCell_1.Value
4) You cannot assign range by just setting an "=" sign
CurCell_2 = CurCell_2.Offset(1,0)
Change it to
Set CurCell_2 = CurCell_2.Offset(1,0)
5) Always specify full declarations when working with two or more objects so that there is less confusion. Your code can also be written as
Option Explicit
Sub trial()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Group As Range, Mat As Range
Dim CurCell_1 As Range, CurCell_2 As Range
Application.ScreenUpdating = False
'~~> Change as applicable
Set wb1 = Workbooks("My_WB_1")
Set wb2 = Workbooks("My_WB_2")
Set ws1 = wb1.Sheets("My_Sheet")
Set ws2 = wb2.Sheets("Sheet2") '<~~ Change as required
For Each Group In ws1.Range("B4:P4")
'~~> Why this?
Set CurCell_2 = ws2.Range("B4")
For Each Mat In ws1.Range("A5:A29")
Set CurCell_1 = ws1.Cells(Mat.Row, Group.Column)
If Not IsEmpty(CurCell_1) Then
CurCell_2.Value = CurCell_1.Value
Set CurCell_2 = CurCell_2.Offset(1)
End If
Next
Next
Application.ScreenUpdating = True
End Sub
Workbooks("My_WB_2").Worksheets(CStr(Group.Value)).CurCell_2.Value
This will not work, since CurCell_2 is not a method of Worksheet, but a variable. Replace by
Workbooks("My_WB_2").Worksheets(CStr(Group.Value)).Range("B4").Value