I want to retrieve the data in listview from below path instead of same file. Can you please advise as to what all changes are required in my code.
myFileNameDir = "C:\Users\GShaikh\Desktop\Book16.xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
Set ws1 = Worksheets("Students")
'Code for retrieving data from same file.
Dim wksSource As Worksheet
Dim rngData As Range
Dim rngCell As Range
Dim LstItem As ListItem
Dim RowCount As Long
Dim ColCount As Long
Dim i As Long
Dim j As Long
Set wksSource = Worksheets("Sheet1")
Set rngData = wksSource.Range("A1").CurrentRegion
For Each rngCell In rngData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=90
Next rngCell
RowCount = rngData.Rows.Count
ColCount = rngData.Columns.Count
For i = 2 To RowCount
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
For j = 2 To ColCount
LstItem.ListSubItems.Add Text:=rngData(i, j).Value
Next j
Next i
You add the data to the ListView from the rngData Range, and here is where you set up that Range:
Set rngData = wksSource.Range("A1").CurrentRegion
If you want to use the data from the workbook that you opened, you should modify rngData to refer to that workbook instead:
Set rngData = ws1.Range("A1").CurrentRegion
Related
I am trying to hide multiple rows in an excel worksheet which are empty using following code however i am getting error message "Argument not optional". What could be wrong in the code?
Sub Attendance_Manday()
Dim sht1 As Worksheet
Dim row_count, col_count As Integer
Dim mainrange As Range
Dim startcell As Range
Set startcell = Range("B1")
Set sht1 = Sheets("Mandays")
row_count = Sheets("Mandays").Cells(Rows.Count, startcell.Column).End(xlUp).Row
col_count = Sheets("Mandays").Cells(startcell.Row, Columns.Count).End(xlToLeft).Offset(1, -2).Column
Set mainrange = sht1.Range(startcell.Address & ":" & sht1.Cells(row_count, col_count).Address)
mainrange.Range.SpecialCells(xlCellTypeBlanks).Rows.Hidden = True
End Sub
Based on your code and assuming first row in your sheet is never empty you could do something like that
Sub Attendance_Manday()
Dim sht1 As Worksheet
Dim row_count As Long, col_count As Long
Dim mainrange As Range
Dim startcell As Range
Set startcell = Range("B1")
Set sht1 = Sheets("Mandays")
row_count = Sheets("Mandays").Cells(Rows.Count, startcell.Column).End(xlUp).Row
col_count = Sheets("Mandays").Cells(startcell.Row, Columns.Count).End(xlToLeft).Offset(1, -2).Column
Set mainrange = sht1.Range(startcell.Address & ":" & sht1.Cells(row_count, col_count).Address)
Dim i As Long
For i = 1 To col_count - 1
mainrange.AutoFilter field:=i, Criteria1:="="
Next i
Dim rg As Range
Set rg = mainrange.SpecialCells(xlCellTypeVisible)
mainrange.AutoFilter
rg.Rows.EntireRow.Hidden = True
rg.Rows(1).EntireRow.Hidden = False
End Sub
An if you turn off screenupdating etc. it should be pretty fast as well
I have 10 worksheets.
I want to create a table for each. every table has a different amount of data, ive been using the following code for each, but i was wondering how to do it with a loop.
I would truly apreciate some help :)
Sub table()
Dim sht As Worksheet
Dim lastrow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = Worksheets("m9")
Set StartCell = Range("A1")
lastrow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
sht.Range(StartCell, sht.Cells(lastrow, LastColumn)).Select
Dim objTable As ListObject
Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
End Sub
Tried the following, but with no luck
Sub loop_test()
Dim i As Integer
Dim ws_num As Integer
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
ws_num = ThisWorkbook.Worksheets.Count
For i = 1 To ws_num
ThisWorkbook.Worksheets(i).Activate
'
Dim lastrow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set StartCell = Range("A1")
lastrow = Cells(Rows.Count, StartCell.Column).End(xlUp).Row
Range(StartCell, Cells(lastrow, LastColumn)).Select
Dim objTable As ListObject
Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
Next
starting_ws.Activate
End Sub
You should avoid Activate and Select statements. The following will loop through all worksheets in the workbook and add a ListObject to each sheet. It will also test to see whether there is an already existing ListObject. If the existing ListObject overlaps with the range that you're going to add the table into to, it will convert it to a range before recreating the ListObject
Sub loop_test()
Dim ws As Worksheet
Dim StartCell As Range, TblRng As Range
Dim LastRow As Long, LastColumn As Long
Dim objTable As ListObject
For Each ws In ThisWorkbook.Sheets
Set objTable = Nothing
With ws
Set StartCell = .Range("A1")
LastRow = .Cells(.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = .Cells(StartCell.Row, .Columns.Count).End(xlToLeft).Column
Set TblRng = .Range(StartCell, .Cells(LastRow, LastColumn))
' Test if table exists on sheet
On Error Resume Next
Set objTable = .ListObjects(1)
On Error GoTo 0
' If table overlaps with TblRng - Convert to Range
If Not Intersect(objTable.Range, TblRng) Is Nothing Then
objTable.Unlist
End If
' Create Table
Set objTable = .ListObjects.Add(xlSrcRange, TblRng, , xlYes)
End With
Next ws
End Sub
Try this. As Zac says, steer clear of activating and selecting and include sheet references.
Sub loop_test()
Dim i As Long 'use Long, integer only goes up to c32k
Dim ws_num As Long
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
ws_num = ThisWorkbook.Worksheets.Count
Dim lastrow As Long
Dim LastColumn As Long
Dim StartCell As Range, r As Range
Dim objTable As ListObject
For i = 1 To ws_num
With ThisWorkbook.Worksheets(i) 'don't need to activate
Set StartCell = .Range("A1")
lastrow = .Cells(.Rows.Count, StartCell.Column).End(xlUp).Row
Set r = .Range(StartCell, .Cells(lastrow, LastColumn))
Set objTable = .ListObjects.Add(xlSrcRange, r, , xlYes)
End With
Next i
End Sub
I have a table with project details and their expiration dates. If the project is expired, it outputs it to another sheet.
I'm trying to make a Refresh button on the Expired project sheet that would loop through that table and extract all of the expired projects.
Sub refresh_expired()
Dim wsMaster As Worksheet: Set wsMaster = Worksheets("MASTER")
Dim wsList As Worksheet: Set wsList = Worksheets("LIST")
Dim tblList As ListObject: Set tblList = wsList.ListObjects("digsafe_list")
Dim tblExp As ListObject: Set tblExp = wsMaster.ListObjects("expired_list")
Dim tblList_row As ListRow
Dim tblExp_row As ListRow
Dim not_empty As String
Dim temp_digsafe As String, temp_work As String
Dim temp_date As Date
Dim tRows As Long, tCols As Long
If tblList.DataBodyRange.Rows.count <> 0 Then
Set tblExp_row = tblExp.ListRows.Add
Set tblList_row = tblList.ListRows.Add
For i = 1 To tblList.DataBodyRange.Rows.count
If tblList_row.Range(7, i).Value < 1 Then
temp_digsafe = tblList_row.Range(1, 1).Value
tblExp_row.Range(1, i).Value = temp_digsafe
temp_date = tblList_row.Range(7, i).Value
tblExp_row.Range(1, 4).Value = temp_date
i = i + 1
End If
Next
End If
End Sub
I have the below code which transfers all visible data from "Prepsheet" to "Contract".
The code refers to each visible section in Prepsheet, resizes the area in contract and then transfers the data.
I want to refer to specific columns within the filtered area, so that I can transfer column specific data individually. For example, I may only want to transfer the 1st and 6th columns. Please can someone assist
Public rnga As Range
Sub test()
Dim wb As Excel.Workbook
Set wb = ActiveWorkbook
Dim sourceWS As Excel.Worksheet
Set sourceWS = Prepsheet
Dim filteredDataRange As Excel.Range
Set filteredDataRange = sourceWS.AutoFilter.Range.Offset(1, 0)
Set filteredDataRange = filteredDataRange.Resize(filteredDataRange.Rows.CountLarge - 1)
Set filteredDataRange = filteredDataRange.SpecialCells(xlCellTypeVisible)
Dim destinationWS As Excel.Worksheet
Dim destinationRow As Long
destinationRow = 1
Dim area As Excel.Range
For Each area In filteredDataRange.Areas
Set rnga = area
MatchSelectionArea
Next area
End Sub
Sub MatchSelectionArea()
Dim rng As Range, cel As Range
Dim nRows As Long
Dim nCols As Long
Set cel = Contract.Range("a1048576").End(xlUp).Offset(1, 0)
nRows = rnga.Rows.Count
nCols = rnga.Columns.Count
Set rng = cel.Resize(nRows, nCols)
rng.Value = rnga.Value
End Sub
You are delving too deeply into the filtered rows and using the number of filtered rows to redefine the filtered range. You can copy straight out of a filtered range and you will only paste the visible rows.
Sub test()
Dim wb As Excel.Workbook, fdRng As Range, v As Long, vCOLs As Variant
Dim sourceWS As Worksheet, destinationWS As Worksheet
Set wb = ActiveWorkbook
Set sourceWS = wb.Worksheets("Prepsheet")
vCOLs = Array(1, 3, 5) 'columns A, C and E
With sourceWS
If .AutoFilterMode Then
With .AutoFilter.Range
With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
For v = LBound(vCOLs) To UBound(vCOLs)
.Columns(vCOLs(v)).Copy _
destination:='YOU HAVE PROVIDED NO DEFINED DESTINATION
Next v
End With
End With
End If
End With
End Sub
Sub MatchSelectionArea()
Dim rng As Range, cel As Range
Dim nRows As Long, nCols As Long
With Worksheets("Contract")
Set cel = .Range("a1048576").End(xlUp).Offset(1, 0)
nRows = rnga.Rows.Count
nCols = rnga.Columns.Count
'cannot determine what this actually does
Set rng = cel.Resize(nRows, nCols)
rng = rnga.Value
End With
End Sub
I'm trying to fill a listview in a userform from a range in Sheet1
This is the code I'm using
Private Sub UserForm_Activate()
'Set some of the properties for the ListView
With Me.ListView1
.HideColumnHeaders = False
.View = lvwReport
End With
'Declare the variables
Dim wksSource As Worksheet
Dim rngData As Range
Dim rngCell As Range
Dim LstItem As ListItem
Dim RowCount As Long
Dim ColCount As Long
Dim i As Long
Dim j As Long
'Set the source worksheet
Set wksSource = Worksheets("Sheet1")
'Set the source range
Set rngData = wksSource.Range("A1").CurrentRegion
'Add the column headers
For Each rngCell In rngData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=90
Next rngCell
'Count the number of rows in the source range
RowCount = rngData.Rows.Count
'Count the number of columns in the source range
ColCount = rngData.Columns.Count
'Fill the ListView
For i = 2 To RowCount
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value) '==> Error here
For j = 2 To ColCount
LstItem.ListSubItems.Add Text:=rngData(i, j).Value
Next j
Next i
End Sub
But the problem is that I always get this error
Run-time error '13':
Type mismatch
Any help plz ?
Thank u in advance
Replace:
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
with:
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Text)
Replace:
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
With:
Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
Try removing "Set LstItem = "