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
Related
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
Beginner VBA scripter here. How can I fix my code so that it will search thru Sheet1 for the string array in strSearch and copy those rows into Sheet2?
Also, how can I extend the code to be able to search for a different string array and copy it into another worksheet?
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim lastRow As Long
Dim strSearch As Variant
Dim i As Integer
Set ws1 = Worksheets("Sheet1")
With ws1
.AutoFilterMode = False
lRow = .Range("J" & .Rows.Count).End(xlUp).Row
With .Range("J1:J" & lRow)
On Error Resume Next
strSearch = Array("John","Jim")
.AutoFilter Field:=1, Criteria1:=strSearch
Set copyFrom = .Offset(0).SpecialCells(xlCellTypeVisible).EntireRow
On Error GoTo 0
End With
Set ws2 = Worksheets("Sheet2")
With ws2
On Error Resume Next
lastRow = ws2.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Set Rng = copyFrom.SpecialCells(xlCellTypeConstants)
Rng.Copy .Cells(lastRow + 1, "C")
copyFrom.Delete
On Error GoTo 0
End With
.AutoFilterMode = False
You could iterate through the lines and the array:
Option Explicit
Dim firstRowWs1 As Long
Dim lastRowWs1 As Long
Dim lastRowWs2 As Long
Dim searchColumnWs1 As Integer
Dim i As Integer
Dim check As Variant
Dim strSearch As Variant
Sub test()
lastRowWs1 = ws1.UsedRange.Rows.Count
lastRowWs2 = ws2.UsedRange.Rows.Count
firstRowWs1 = 2
searchColumnWs1 = 1
strSearch = Array("John", "Jim")
For i = firstRowWs1 To lastRowWs1
For Each check In strSearch
If check = ws1.Cells(i, searchColumnWs1).Value Then
ws1.Rows(i).Copy (ws2.Rows(lastRowWs2 + 1))
lastRowWs2 = lastRowWs2 + 1
ws1.Rows(i).Delete shift:=xlUp
i = i - 1
Exit For
End If
Next check
Next i
End Sub
Dim strsearchlocation as integer
strSearchLocation = Sheet1.Cells.Find(what:= strSearch, After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).row
Sheet1.Rows(strSearchLocation).Copy
Finds and copies the row of strSearch
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
I am trying to make a worksheet function that loops through rows more efficient. Here is what I have -
Dim ws as worksheet, wks as worksheet, lastrow as long, i as long
Set ws = Sheets("Sheet1")
Set wks = Sheets("Sheet2")
With ws
Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 to Lastrow
.Range("CS" & i) = Application.Vlookup(ws.Range("B" & i), wks.Range("B2:X100000"),23,false)
next
end with
Thanks!
You should avoid iterating through the rows as much as possible. I would suggest that you instead do a paste of the Vlookup function in the range of the CS column.
This is how it would go if you used the find function:
Dim ws as worksheet, wks as worksheet, lastrow as long, i as long
Set ws = Sheets("Sheet1")
Set wks = Sheets("Sheet2")
Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 to Lastrow
ws.Range("CS" & i) = wks.Range("B:B").find(ws.Range("B" & i).Text, , xlValues).Offset(0, 22).Value
Next i
Let me know if it works.
I would remove all the reading and writing on cells from the loop:
Dim rg_table As Range, rg_lookup As Range, values(), r&
Set rg_table = [Sheet1].UsedRange
Set rg_lookup = [Sheet2!B2:X100000]
' load the values to search
values = rg_table.columns("B").Value
' lookup the values
For r = 2 To UBound(values)
values(r, 1) = Application.VLookup(values(r, 1), rg_lookup, 23, False)
Next
' write the results to the sheet
rg_table.columns("CS") = values
How can I take data from multiple sheets instead of just Sheet1?
Sub CheckRowsWithAutofilter()
Dim DataBlock As Range, Dest As Range
Dim LastRow As Long, LastCol As Long
Dim SheetOne As Worksheet, SheetTwo As Worksheet
'set references up-front
Set SheetOne = ThisWorkbook.Worksheets("Sheet1")
Set SheetTwo = ThisWorkbook.Worksheets("Sheet2")
Set Dest = SheetTwo.Cells(Last + 1, "A")
'enter code here
'identify the "data block" range, which is where
With SheetOne
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set DataBlock = .Range(.Cells(112, 7), .Cells(LastRow, LastCol))
End With
With DataBlock
.SpecialCells(xlCellTypeVisible).Copy Destination:=Dest
End With
End Sub
This should loop through each worksheet named in the vWSs variant array.
Sub CheckRowsWithAutofilter()
Dim lr As Long, lc As Long
Dim SheetTwo As Worksheet
Dim w As Long, vWSs As Variant
'set references up-front
vWSs = Array("Sheet1", "Sheet3", "Sheet4")
Set SheetTwo = ThisWorkbook.Worksheets("Sheet2")
'loop through the worksheets named in vWSs
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
lr = .Range("A" & .Rows.Count).End(xlUp).Row
lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
With .Range(.Cells(112, 7), .Cells(lr, lc))
.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
End With
Next w
End Sub
I cut out some of your variables as they were only used once and sometimes the code to declare and assign them was more than simply making the direct reference.