I'm trying to load a combobox with an array (range) of labels from another worksheet. It works if I use "A4:PB4" in the range instead of the Cells method. Not sure why this doesn't work.
Private Sub ComboBox1_GotFocus()
Dim myArray As Variant
lastcol = Worksheets("data").Range("A4").End(xlToRight).Column
myArray = WorksheetFunction.Transpose(Worksheets("data").Range(Cells(4, 1), Cells(4, lastcol)))
With Me.ComboBox1
.List = myArray
End With
End Sub
Private Sub ComboBox1_GotFocus()
Dim myArray As Variant
lastcol = Worksheets("data").Range("A4").End(xlToRight).Column
With Worksheets("data")
Set SourceRng = .Range(.Cells(4, 1), .Cells(4, lastcol))
End With
myArray = WorksheetFunction.Transpose(SourceRng)
With Me.ComboBox1
.List = myArray
End With
End Sub
Related
I have a code that copies data from different workbooks to an array and transfers that data to a table I want to keep the filter method with a slicer on the workbook I copy that data from but to copy only the filtered data.
on the workbook I am copying to I want only the filtered that to be seen
Does anyone have suggestions?
I commented out the offset because the code doesn't work i need the offset in order not to copy the header row
Sub readingarray()
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim arr As Variant
Dim tbl As Range
Set tbl = Workbooks("test.xlsm").Worksheets("shibuz").Range("T4").CurrentRegion.SpecialCells(xlCellTypeVisible)
'arr = tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count)
arr = tbl
Set table_list_object = Workbooks("shibuzim 2 updated.xlsm").Worksheets("shibuz").ListObjects("LeaveTracker")
Set table_object_row = table_list_object.ListRows.Add
Dim rowcount As Long, columncount As Long
rowcount = UBound(arr, 1)
columncount = UBound(arr, 2)
table_object_row.Range(1, 1).Resize(rowcount, columncount).Value = arr
End Sub
solved it, hopes this might help
Option Explicit
Sub readingarray()
Application.DisplayAlerts = False
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim arr
Dim Itm
Dim rng As Range
Dim stringarray As Variant
Dim rowcount As Long, columncount As Long
stringarray = Array("test.xlsm", "test 2.xlsm")
On Error Resume Next
For Each Itm In stringarray
arr = GetArrayFromFilteredRange(Workbooks(Itm).Worksheets("shibuz").ListObjects("LeaveTracker").DataBodyRange.SpecialCells(xlCellTypeVisible))
Set table_list_object = Workbooks("shibuzim 2 updated.xlsm").Worksheets("shibuz").ListObjects("LeaveTracker")
Set table_object_row = table_list_object.ListRows.Add
rowcount = UBound(arr, 1)
columncount = UBound(arr, 2)
table_object_row.Range(1, 1).Resize(rowcount - 1, columncount - 1).Value = arr
Next Itm
On Error GoTo 0
End Sub
Function GetArrayFromFilteredRange(rng As Range) As Variant
Dim arr As Variant
helper.Cells.Clear
rng.Copy helper.Range("A1")
arr = helper.UsedRange.Value
GetArrayFromFilteredRange = arr
End Function
I'm trying to sum numbers that are in an array.
I get the attached error message.
What is the syntax for using Sumif within an array?
Sub SumNumbers()
Dim arr As Variant
arr = Range("A1").CurrentRegion.Value
Dim iMax As Long
iMax = UBound(arr, 1)
Debug.Print WorksheetFunction.SumIf(Range(arr(1, 1), arr(iMax, 1)), "A", Range(arr(1, 2), arr(iMax, 2)))
End Sub
SumIf function works on ranges... At least its first parameter must be a range.
Please, try the next code:
Sub testSumif()
Dim sh As Worksheet, rngB As Range, rngC As Range, lastRow As Long
Set sh = ActiveSheet 'use here the sheet you need
lastRow = sh.Range("B" & sh.Rows.count).End(xlUp).row
Set rngB = sh.Range("B2:B" & lastRow)
Set rngC = sh.Range("C2:C" & lastRow)
Debug.Print WorksheetFunction.SumIf(rngB, "A", rngC)
End Sub
If you insist to use the CurrentRegion, try the next code, please:
Sub testSumifBis()
Dim sh As Worksheet, rng As Range, rngB As Range, rngC As Range
Set sh = ActiveSheet 'use here the sheet you need
Set rng = sh.Range("B2").CurrentRegion
Set rngB = rng.Columns(1)
Set rngC = rng.Columns(2)
Debug.Print WorksheetFunction.SumIf(rngB, "A", rngC)
End Sub
Sumif vs Loop vs Match'n'Loop
I would use the first solution.
The Code
Option Explicit
Sub sumIfRange()
Dim rg As Range
' Define Current Region range.
Set rg = Range("A1").CurrentRegion
' Only use data from 'A2' to 'Bwhatever' (no headers).
Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
Dim Result As Double
Result = Application.SumIf(rg.Columns(1), "A", rg.Columns(2))
Debug.Print Result
End Sub
Sub sumIfLoop()
Dim rg As Range
' Define Current Region range.
Set rg = Range("A1").CurrentRegion
' Only use data from 'A2' to 'Bwhatever' (no headers).
Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
' Only now write to array.
Dim Data As Variant: Data = rg.Resize(, 2).Value
Dim Result As Double
Dim i As Long
Dim j As Long
For i = 1 To UBound(Data, 1)
If Not IsError(Data(i, 1)) Then
If Data(i, 1) = "A" Then
If IsNumeric(Data(i, 2)) Then
Result = Result + Data(i, 2)
End If
End If
End If
Next i
Debug.Print Result
End Sub
Sub sumIfMatchLoop()
Dim rg As Range
' Define Current Region range.
Set rg = Range("A1").CurrentRegion
' Only use data from 'A2' to 'Bwhatever' (no headers).
Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
' Only now write to arrays.
Dim lData As Variant: lData = rg.Columns(1).Value
Dim rData As Variant: rData = rg.Columns(2).Value
Dim mData As Variant: mData = Application.Match(lData, Array("A"), 0)
Erase lData
Dim Result As Double
Dim i As Long
For i = 1 To UBound(mData)
If IsNumeric(mData(i, 1)) Then
Result = Result + rData(i, 1)
End If
Next i
Debug.Print Result
End Sub
I have the following problem: I imported a .csv file with my data into a separate worksheet called "Import".
In this QueryTable, I have the second column called "KW", which indicates the weeknumber for every row.
Now I wanted to populate an array with the cell values from the second column.
I need to make it dynamic, because the length of the array changes with each import.
So far I made the code below:
Sub PopulatingArrayVariable()
Dim myArray() As Variant
Dim TempArray() As Variant
Dim myTable As ListObject
Dim x As Long
Sheets("Import").Activate
Set myTable = ActiveSheet.ListObjects("database_all")
TempArray = myTable.DataBodyRange.Columns(2)
myArray = Application.Transpose(TempArray)
For x = LBound(myArray) To UBound(myArray)
Debug.Print myArray(x)
Next x
End Sub
I get the "runtime error 13": types not compatible
I get the error, but I don't know what exactly I need to change. Can someone please help me solve this?
Fix the Dim and use Set:
Sub PopulatingArrayVariable()
Dim myArray() As Variant '*** will be a 1D VBA array
Dim TempArray As Range '*** typical 2D range variable as part of a column
Dim myTable As ListObject
Dim x As Long
Sheets("Import").Activate
Set myTable = ActiveSheet.ListObjects("database_all")
Set TempArray = myTable.DataBodyRange.Columns(2)
myArray = Application.Transpose(TempArray)
For x = LBound(myArray) To UBound(myArray)
Debug.Print myArray(x)
Next x
End Sub
I would use an arraylist instead as it provides more flexibility. For example you can do something like this:
Sub test()
Dim arr As Object
Dim i As Long
Dim j As Long
Dim lastRow As Long
Set arr = CreateObject("System.Collections.ArrayList")
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
j = 2
'Filling arrayLists
For i = 2 To lastRow
arr.Add ActiveSheet.Cells(i, 2)
Next i
'Read from arrayLists
For i = 0 To arr.Count - 1
Cells(j, 3) = arr(i)
j = j + 1
Next i
End Sub
Customize the code as required. It should resolve your issue.
I want to use an array of strings that will replace the Worksheet object inside my loop, but I cant seem to figure it out.
If I declare SheetX as Variant, then I get the Object Required Error
If I declare SheetX as Object, then I get Compile Error: For Each variable on arrays must be variant
Sub DeleteAllData()
'SheetsArray = ["BalanceSheetTransposed", "IncomeStatementTransposed", "CashflowStatement"]
Dim SheetsArray(0 To 2) As Variant
Dim SheetX As Object
SheetsArray(0) = "BalanceSheetTransposed"
SheetsArray(1) = "IncomeStatementTransposed"
SheetsArray(2) = "CashflowStatement"
For Each SheetX In SheetsArray
lastrow = SheetX.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = SheetX.Cells(1, Columns.Count).End(xlToLeft).Column
SheetX.Range("A2", Cells(lastrow, lastcolumn)).ClearContents
Next SheetX
End Sub
Out of my head 'cause I don't have Excel in this machine. Loop through the strings and set worksheet object.
Sub DeleteAllData()
Dim SheetsArray(0 To 2) As String
Dim SheetX As Worksheet
Dim name as String
SheetsArray(0) = "BalanceSheetTransposed"
SheetsArray(1) = "IncomeStatementTransposed"
SheetsArray(2) = "CashflowStatement"
For Each name In SheetsArray
set SheetX = ActiveWorkbook.worksheets(name)
lastrow = SheetX.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = SheetX.Cells(1, Columns.Count).End(xlToLeft).Column
SheetX.Range("A2", Cells(lastrow, lastcolumn)).ClearContents
Next
End Sub
Your major problem was that you were trying to treat the strings stored in the array as if they were worksheets, but they are just strings.
The simplest way to get around it is to use Worksheets(SheetsArray) to return the worksheets that have the names you want to use, and then loop through those worksheets:
Sub DeleteAllData()
Dim SheetX As Worksheet
Dim lastRow As Long
Dim lastColumn As Long
Dim SheetsArray(0 To 2) As Variant
SheetsArray(0) = "BalanceSheetTransposed"
SheetsArray(1) = "IncomeStatementTransposed"
SheetsArray(2) = "CashflowStatement"
'An alternative to the previous 4 lines would be
'Dim SheetsArray As Variant
'SheetsArray = Array("BalanceSheetTransposed", _
' "IncomeStatementTransposed", _
' "CashflowStatement")
'Loop through the worksheets referred to in the array
For Each SheetX In Worksheets(SheetsArray)
With SheetX ' avoids some typing
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Existing code would have had issue with the unqualified Cells
'reference in the following line. You should always qualify Cells
'to specify which sheet you mean, because it defaults to
'ActiveSheet.Cells
.Range("A2", .Cells(lastRow, lastColumn)).ClearContents
End With
Next SheetX
End Sub
The Array has to be passed to the Sheets object :
Sub DeleteAllData()
Dim ws As Worksheet
For Each ws In Sheets(Array("BalanceSheetTransposed", "IncomeStatementTransposed", _
"CashflowStatement"))
s.UsedRange.Offset(1).ClearContents
Next
End Sub
Let's say I have something like "1-34-52", I want to split them into an array, however, while Test1 works, it gives me an String() type array. How do I put the numbers in "1-34-52" into a Long() type array? Can I redim type of an array in VBA?
Sub Test1()
Dim arr As Variant
arr = Split("1-34-52", "-")
Debug.Print TypeName(arr), TypeName(arr(0))
End Sub
Sub Test2()
Dim arr() As Long
arr = Split("1-34-52") 'You get type mismatch error
End Sub
You can Redim an array of Variants. Since Variants can hold integer values, there is no problem:
Sub dural()
ary = Split("1-34-52", "-")
ReDim lary(0 To UBound(ary))
For i = 0 To UBound(ary)
lary(i) = CLng(ary(i))
Next i
End Sub
Note:
Sub dural()
ary = Split("1-34-52", "-")
Dim lary() As Long
ReDim lary(0 To UBound(ary))
For i = 0 To UBound(ary)
lary(i) = CLng(ary(i))
Next i
End Sub
will also work.
You can loop through the array and populate a new one:
Sub Test1()
Dim arr As Variant, LongArr() As Long, X As Long
arr = Split("1-34-52", "-")
ReDim LongArr(UBound(arr))
For X = LBound(arr) To UBound(arr)
LongArr(X) = CLng(arr(X))
Next
Debug.Print TypeName(arr), TypeName(arr(0))
Debug.Print TypeName(LongArr), TypeName(LongArr(0))
End Sub