I wrote a very simple macro, that opens another workbook and select range of data from this workbook.
However, I keep receiving this warning: object doesn't support this property or method
What is wrong?
Sub data()
Dim wb As Workbook
Dim ws As Worksheet
Dim filename As String
Dim lastrow As Integer
Dim lastcolumn As Integer
Dim range_to_copy As Range
'open workbook
filename = "C:\Users\mk\Desktop\sales report\Sales Report.xls"
Set wb = Workbooks.Open(filename)
Set ws = wb.Sheets("data")
lastcolumn = wb.ws.Cells(1, wb.ws.Columns.Count).End(xlToLeft).Column
lastrow = wb.ws.Cells(wb.ws.Roows.Count, 1).End(xlToLeft).Row
range_to_copy = Range("Cells(1,1):Cells(lastrow,lastcolumn)")
End sub
A number of things wrong.
Edit:
Dim lastrow As Integer
Dim lastcolumn As Integer
An Integer can store numbers up to 32,767. This won't be a problem for columns, but will throw an overflow error on the row number. It's better to use the Long data type - numbers up to 2,147,486,647.
The ws variable already references the workbook so you just need:
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastrow = wb.ws.Cells(wb.ws.Roows.Count, 1).End(xlToLeft).Row
Rows only has one o in it.
Edit: xlToLeft looks at the right most cell and works to the left. As you're looking for rows you need to use xlUp which looks at the last cell and works up.
range_to_copy = Range("Cells(1,1):Cells(lastrow,lastcolumn)")
This is an object so you must Set it. The cells references are separated by a comma and should not be held as a string.
Sub data()
Dim wb As Workbook
Dim ws As Worksheet
Dim filename As String
Dim lastrow As Long
Dim lastcolumn As Long
Dim range_to_copy As Range
'open workbook
filename = "C:\Users\mk\Desktop\sales report\Sales Report.xls"
Set wb = Workbooks.Open(filename)
Set ws = wb.Sheets("data")
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = ws.Range(ws.Cells(1, 1), ws.Cells(lastrow, lastcolumn))
End Sub
Note: Every range reference is preceded by the worksheet reference. Without this it will always look at the currently active sheet so if you aren't on the data sheet the following will fail as the second cell reference will look at the activesheet.
ws.Range(ws.Cells(1, 1), Cells(lastrow, lastcolumn))
It might be worth checking out the With....End With code block.
There are several errors in your code.
Try
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = Range(ws.Cells(1, 1), ws.Cells(lastRow, lastcolumn))
Once you defined and set your ws worksheet object, you don't need to refer to the wb object anymore, since your ws worksheet object is fully quallified with Sheets("data") in wb workbook.
Now, all you need to do is use the With statement, like in the code below:
Set ws = wb.Sheets("data")
With ws
lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = .Range(.Cells(1, 1), .Cells(lastrow, lastcolumn))
End With
Related
My question is very similar to the following:
Excel VBA code to select non empty cells
However, I have some empty cells in between. In particular, I'm trying to define a range where the first row needs to be excluded, since it's a header. I also have to exclude the empty cells that follow. I was trying something like
Dim wb as workbook, ws as worksheet
Dim myrange as range
'Defined reference wb and ws
myrange=ws.range("B2",range("B2"),end(xldown))
But this only works if there are not empty cells in between. So, is there a fast and simple way to dynamically select a range that includes non-empty cells, excepted the header?
Try this
'Defined reference wb and ws
Set myrange = ws.range("B2", ws.Range("B" & Rows.Count).End(xlUp))
Don't forget to use the Set keyword
Try the next way, please:
Sub testDefineRange()
Dim ws As Worksheet, lastRow As Long, myrange As Range
Set ws = ActiveSheet 'use here what you need
lastRow = ws.Range("B" & ws.rows.count).End(xlUp).row
'Defined reference ws
Set myrange = ws.Range("B2:B" & lastRow)
myrange.Select
End Sub
I have found this link to be very useful on MANY occasions.
https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba
Ways To Find The Last Row
Sub FindingLastRow()
'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com
Dim sht As Worksheet
Dim LastRow As Long
Set sht = ActiveSheet
'Using Find Function (Provided by Bob Ulmas)
LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Using SpecialCells Function
LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row
'Ctrl + Shift + End
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'Using UsedRange
sht.UsedRange 'Refresh UsedRange
LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
'Using Table Range
LastRow = sht.ListObjects("Table1").Range.Rows.Count
'Using Named Range
LastRow = sht.Range("MyNamedRange").Rows.Count
'Ctrl + Shift + Down (Range should be first cell in data set)
LastRow = sht.Range("A1").CurrentRegion.Rows.Count
End Sub
Ways To Find The Last Column
Sub FindingLastColumn()
'PURPOSE: Different ways to find the last column number of a range
'SOURCE: www.TheSpreadsheetGuru.com
Dim sht As Worksheet
Dim LastColumn As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LastColumn = sht.Cells(7, sht.Columns.Count).End(xlToLeft).Column
'Using UsedRange
sht.UsedRange 'Refresh UsedRange
LastColumn = sht.UsedRange.Columns(sht.UsedRange.Columns.Count).Column
'Using Table Range
LastColumn = sht.ListObjects("Table1").Range.Columns.Count
'Using Named Range
LastColumn = sht.Range("MyNamedRange").Columns.Count
'Ctrl + Shift + Right (Range should be first cell in data set)
LastColumn = sht.Range("A1").CurrentRegion.Columns.Count
End Sub
If it suits your case, I like to use CurrentRegion with Intersect to eliminate the title row.
with ws.range("b2")
set myRange = intersect(.currentregion, .currentregion.offset(1,0))
end with
debug.print myRange.address
I have the following code that selects all used cells starting from the cell specified.
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim ranged_select As Range
Set sht = Worksheets("Worksheet1")
Set StartCell = Range("C2")
'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
But how come for the last line
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
I cannot assign it to a variable? I want the variable to be a range. I have tried assigning this variable Dim used_ranged as Range but it gives me 'Run-time error '91':'
Here is the data and what it looks like when I run the code which just selects the data starting from C2.
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim range_select As Range
Set sht = Worksheets("Worksheet1")
Set StartCell = sht.Range("C2") '<< always be explicit about the parent sheet
'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
'drop the .Select
Set range_select = sht.Range(StartCell, sht.Cells(LastRow, LastColumn))
I am in learning phase in vba. I am trying to store a value in a variable but not able do with Cell and also Range and it throws an 1004 error
Below is my code
Sub myself()
Dim str As String
Dim rock As String
Dim MaxStrLen As Integer
Dim StrLen As String
Dim LastRow As Long
Dim LastCol As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Integer
Dim j As Integer
Dim FilePath As String
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Open "C:\Users\Antony\Desktop\test.txt" For Output As #2
ws1.Activate
With ws1
For i = 0 To LastRow
For j = 0 To LastCol
.Range(.Cells(i, j)).Value = str
Next j
Next i
Print #2, str
End With
Close #2
End Sub
Highlighted line is the 1004 error. Please help to solve and store in a variable in Notepad.
Thanks in advance!
just use
.Cells(i, j).Value = str
BTW you'd better explicitly qualify your range references up to the worksheet object (and, if multiple workbooks are involved, up to the workbook object) , otherwise it would implicitly assumed ActiveSheet (and ActiveWorkbook)
so, instead of
LastRow= Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
use
LastRow= ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
LastCol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
or
With ws1
LastRow= .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
so your code could be refactored as follows:
Option Explicit
Sub myself()
Dim str As String
Dim LastRow As Long
Dim LastCol As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long
Dim j As Long
Dim FilePath As String
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Open "C:\Users\Antony\Desktop\test.txt" For Output As #2
With ws1
LastRow = .Cells(.Rows.count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
For i = 1 To LastRow
For j = 1 To LastCol
str = .Range(.Cells(i, j)).Value
Print #2, str
Next
Next
End With
Close #2
End Sub
I was writing a code to select all the data entries of a Workbook which I 'Open' in a range, but the compiler gives error at the very last line (set up the Range rng)
Dim wb As Workbook
Set wb = Workbooks.Open(Range("C2") & Range("C3"))
'here Range("C2") & Range("C3") contains the location of the file's path
Dim ws As Worksheet
Set ws = wb.ActiveSheet
Dim frow As Long
frow = ws.Range("A" & Rows.count).End(xlUp).Row
Dim rng As Range
Dim frow1 As Long
frow1 = ws.Cells(1, Columns.count).End(xlToLeft).Column
Set rng = wb.ActiveSheet.Range(Cells(1, 1), Cells(frow, frow1))
Try:
Dim frow As Long
frow = ws.Range("A" & ws.Rows.count).End(xlUp).Row
Dim rng As Range
Dim fcol As Long
fcol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(frow, fcol))
Remember that if you are using a set worksheet u have to reference it in all range objects
So there is a workbook that runs a macro that was made by another group in the company and its GARBAGE but they locked it and wont let me modify the VBA. So I am trying to pull the output from that workbook into mine. The headers of the range are in merged rows with the first 11 rows being taken up by the buttons to run the macro.
Here is my current code
Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Range("H65536").End(xlUp).Row
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll
End Sub
My issue is that when i try to select the entire output it sometimes selects what i want (A11 to H then the last row with value) but sometimes it selects A1:H11 and its frustrating as hell to figure out the issue. If there is a better way I would love to know.
EDIT:
Updated code
Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Application.Max(Range("H65536").End(xlUp).Row, 11)
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll
End Sub
r = Application.Max(Range("H65536").End(xlUp).Row, 11) 'wich sheet is the range assiciated with ?
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select 'how is this line usefull at all ?
Set rng = ws.Range(Cells(11, 1), Cells(r, 8)) ' change to ws.Range(ws.Cells(11, 1), ws.Cells(r, 8))
rng.Select 'delete this line
rng.Copy '
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll 'change to ws.Range(ws.Cells(1, 1), ws.Cells(r, 8)).PasteSpecial xlPasteAll
Rather than pulling data from garbage.xlsm into your workbook, copy the entire worksheet into your workbook.
Then in your version of the worksheet, remove the merging, etc.