Excel VBA get table range on unactive worksheet - vba

I cannot figure out why this statement does not work.
Rng = Worksheets(sheetName).ListObjects(table).Range.Select
I have a sheet "sheetX" with a button that invokes a subprocess "export_json" in the global workspace "Thisworkbook". I want the subprocess in "Thisworkbook" to reference a table range on "sheetX" at "A2" but it gives an error "Application-defined or Object-defined error". I do not want to use Application.Goto
Why is that? I'm overlooking something basic
Public Sub CommandButton1_Click()
sheet = ActiveSheet.Name
Call ThisWorkbook.export_json(sheet)
End Sub
Public Sub export_json(sheetName)
table = ThisWorkbook.get_table(Worksheets(sheetName).Range("A2"))
Rng = Worksheets(sheetName).ListObjects(table).Range.Select
Rng = Selection.Address
table is of type string and sheet is the correct sheet name of type string so that is not the problem.

Try the code below, there's no need to use Select.
Also, it is not clear to me why you keep the code of your Function in ThisWorkbook module, instead of a regular module.
Public Sub CommandButton1_Click()
Dim TableRangeString As String
TableRangeString = ThisWorkbook.export_json(ActiveSheet.Name)
' for debug
MsgBox TableRangeString
End Sub
Public Function export_json(sheetName) As String
' use the function to return the Range.Address by changing it to return a String
Dim Rng As Range
Set Rng = Worksheets(sheetName).Range("A2").ListObject.Range
export_json = Rng.Address
End Function

Your syntaxing looks a little off, when your trying declare Rng in the function export_json() you should pass it as a string.
Public Sub CommandButton1_Click()
Dim sheetX As String
sheetX = ActiveSheet.Name
Call export_json("sheetX")
End Sub
Private Function export_json(sheetName As String)
table = ThisWorkbook.get_table(Worksheets(sheetName).Range("A2"))
Worksheets(sheetName).ListObjects(table).Range.Select
End Function

Related

VBA Dim and Set Range global

I have a probably easy problem using variables globally:
I want to Dim and Set various ranges in order to use them in different subs.
Simple Example given:
Sub Variables()
Dim rng1 as Range
Set rng1 = Worksheets("Sheet1").Cells
Dim rng2 as Range
Set rng2 = Worksheets("Sheet2").Cells
End Sub
Then in order to use my variables in other subs I tried to call my Variable sub, like:
Sub calc()
Call Variables
Dim i as Integer
i = rng1.Find("Hello").Column
With Sheet1.Cells(1, i)
.Value = "World"
End With
I get the Error "Object required". I also tried to define my Variables as Public Sub but it still doesn't work.
I hope you know how to solve this problem or how to give a different approach!
Thanks!
Option Explicit
Dim rng1 As Range
Dim rng2 As Range
Sub Variables()
Set rng1 = Worksheets("Sheet1").Cells
Set rng2 = Worksheets("Sheet2").Cells
End Sub
Sub calc()
Variables
Dim i As Integer
i = rng1.Find("Hello").Column
Sheet1.Cells(1, i).Value = "World"
End Sub
You declared inside the Sub, you can't able to use them in other Sub, so that's why it gives you an error.
The solution here is put them at the top of your Procedure.

Application.Caller / Storing worksheet name as string

Im trying to store the CurrentWorksheet name in order to reference it in a different Sub routine.
The code I currently have is as follows:
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = Worksheets(CurrentWorksheet).Range("A30:L30")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function:
Function CurrentWorksheet()
CurrentSheet = Application.Caller.Worksheet.Name
End Function
I need to try to reference the "CurrentSheet" variable in the "InsertNewBill" Sub routine.
The function of this is to insert a new line of cells between "A30:L30" on the currently selected worksheet.
Thanks in advance
I didn't plan to write this as answer, but to explain to Batteredburrito how to deal with objects rather than names:
Option Explicit
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = currentWorksheet.Range("A31:AC31")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function
Function currentWorksheet() As Worksheet
set currentWorksheet = ActiveSheet
End Function
This is doing exactly the same as your version (so you can stick with that), it's just to show how to deal with objects. But there are situations where it is much better to deal with objects that with name - especially if you are dealing with multiple Workbooks (that might have the same sheet names).
And, of course, in the end you could get rid of the function completely by simply writing
...
Set rng = ActiveSheet.Range("A31:AC31")
...
Thank you all for your help and direction.
I have resolved the issue with the following
Insert new cells between a range of cells on button press:
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = Worksheets(CurrentWorksheet).Range("A31:AC31")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function:
Function CurrentWorksheet()
CurrentWorksheet = ActiveSheet.Name
End Function

vba assigning sub routine to variable

I have the following sub routine (in module10).
Sub varWorksheet(wksht As String)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(wksht)
Set ws = Nothing
End Sub
I want to be able to pass this sub routine as a reference to a variable with something like this rather than have to declare it explicitly in each routine:
Set ws = module10.varWorksheet("Sheet1")
I'm getting a compilation error -> expected Function or Variable.
You shoud use a function like this.
Function varWorksheet(wksht As String) As Worksheet
On Error Resume Next
Set varWorksheet = ThisWorkbook.Sheets(wksht)
End Function
It will return nothing if the worksheet doesn't exist. This works fine.
Sub Test()
Dim ws As Worksheet
Set ws = Modul10.varWorksheet("Tabelle4")
If ws Is Nothing Then
Debug.Print "No worksheet"
Else
' what ever you want
End If
End Sub

Error passing array to listbox as a parameter

Objective : Create a userform and take a user input, and then from user input put it in a list and when you click the list it automatically find it in the whole workbook.
Something Like this:
I saw this post: Match in whole workbook
And I created something out of that:
Public Sub CommandButton3_Click()
Dim TempArray As Variant
Dim RealArray()
TempArray = Application.InputBox("Select a range", "Obtain Range Object", Type:=64)
ArrayRows = UBound(TempArray)
ReDim RealArray(1 To ArrayRows)
For i = 1 To ArrayRows
RealArray(i) = TempArray(i, 1)
Next i
MsgBox "The number if rows selected are " & ArrayRows
ListBox1.List = RealArray
ListBox1 Arraay:=RealArray
End Sub
Public Sub ListBox1_Click(ByRef Arraay() As Variant)
Dim Sh As Worksheet
Dim something As Range
Dim ArrayRows As Long
For Each Sh In ThisWorkbook.Worksheets
With Sh.UsedRange
For i = 1 To ArrayRows
Set something = RealArray.Find(What:=RealArray(i))
If Not something Is Nothing Then
Do Until something Is Nothing
test = something.Value
Set something = .FindNext(something)
Loop
End If
Next i
End With
Set something = Nothing
Next
End Sub
After creating this, I get an error regarding the second sub.
procedure declaration does not match description of event or procedure having the same name
The Listbox click event doesn't take any parameters.
Private Sub ListBox1_Click()
End Sub
If you want to pass an array between sub then you can so it this way
Dim MyArray() As Variant
Public Sub CommandButton3_Click()
'~~> Initialize array
End Sub
Private Sub ListBox1_Click()
'~~> Use array here
'~~> Also put an error check if the array is initialized or not
End Sub

Internal sheet name as variable based on input in excel cell

I have the following VBA code:
Sub test()
Dim Variable As String
Variable = Sheet1.Range("A1").Value
Sheet2.Select
Range(Variable).Select
End Sub
Value in cell A1 = B2:B3
The code works perfectly so far. Now I want to make the "Sheet2" part as a variable in the same way as I did for the range. I want to write the internal sheet name into an excel cell to make it variable. I tried the following way so far but it did not work.
Sub test()
Dim Variable As String
Variable = Sheet1.Range("A1").Value
Variable2 = Sheet1.Range("A2").Value
Variable2.Select
Range(Variable).Select
End Sub
Value in cell A1 = B2:B3
Value in cell A2 = Sheet2
Do you have any idea how I can solve this issue?
Thanks for any help in advance.
Sheets(Variable2).Range(Variable).Select
EDIT
If you want to get the Sheetname by entering the Codename, use a function like this to return the right sheet name:
Function getSheet(codename_ As String) As String
'returns the Sheet name corresponding to the Codename
Dim ws As Worksheet
For Each ws In Worksheets
If ws.codename = codename_ Then
getSheet = ws.Name
Exit Function
End If
Next ws
End Function
Then you can use this like Sheets(getSheet("CODENAME")).Select
try with below code
Sub test()
Dim Variable As String
Variable = Sheet1.Range("A1").Value
Variable2 = Sheet1.Range("A2").Value
Worksheets(Variable2).Select
Range(Variable).Select
End Sub
EDIT - 1
Sheets(Val(Replace(variable2, "Sheet", ""))).Select