Internal sheet name as variable based on input in excel cell - vba

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

Related

Excel VBA get table range on unactive worksheet

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

Excel VBA Macro Formula Help Regarding not show formula in formula bar

I want to use if condition formula to calculate the cube via vba macro button. I write code and its working is fine but code is show in formula bar and also working with formula come from vba macro and remain their, I want that this formula did not should be show in formula bar and the calculation should be done only via macro button when i press it. here is my code. Thanks
Sub CalCubMeter()
Worksheets("sheet1").Range("d3:d21").Formula = "=if(c3=C3,PRODUCT($c3^3),""-"")"
End Sub
Something along the lines of:
Public Sub CubeColumnC
Dim wb as Workbook
Dim ws as Worksheet
Dim sourceRange as Range
Dim sourceArr()
Dim i as long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1") 'change to as needed
Set sourceRange = ws.Range("C3:C21")
'Assign column c values to array
sourceArr = Application.Transpose(sourceRange.Value)
With ws
For i = Lbound(sourceArr) To Ubound(sourceArr) 'Write back out to col D
If .Cells(2+i, "C") = vbNullString Then 'if blank
.Cells(2+i, "D") = "-"
Else
.Cells(2+i,"D") = sourceArr(i)^3 'Calc the Cube value
End If
Next i
End With
End Sub
I ignored your C3 = C3 as this is always True, amend as you see fit.
Edit: Application.WorksheetFunction.Power(sourceArr(i),3) changed to sourceArr(i)^3 for Cube value

Want to write formula in cell if x=y, otherwise blank cell, using VBA

Let me better explain. If the value of A1 is "0" then in A2 I want the formula =vlookup(B1,C1:E3,2,0), If the value of A1 is "1", then I simply want a blank cell value for A2. I want this macro to occur upon opening the excel. I thought this would work but it is not
Sub test()
Dim indicator As Value
Dim result As String
indicator = Range("A1").Value
If indicator = 0 Then result = "=VLOOKUP(A3,C1:D3,2,0)"
Range("a2").Value = result
End Sub
in the ThisWorkbook section put this in
Sub Workbook_open()
If Range("A1") = 0 Then
Range("A2").Formula = "=vlookup(B1,C1:E3,2,0)"
ElseIf Range("A1") = 1 Then
Range("A2").ClearContents
Else
End If
End Sub
If you want the code to run on a specific sheet, not the one that happened to be active when the file was last saved, then try this instead:
Sub Workbook_open()
Dim ws As Worksheet
' define the worksheet to be changed here:
Set ws = ThisWorkbook.Worksheets("Sheet5")
If ws.Range("A1") = 0 Then
ws.Range("A2").Formula = "=vlookup(B1,C1:E3,2,0)"
ElseIf Range("A1") = 1 Then
ws.Range("A2").ClearContents
Else
End If
End Sub

VBA rename sheet based on cell value

I want a VBA code that rename my sheetXXX, where XXX is the value in the cell B5 in “Sheet1” sheet. The macro should work for any value in B5.
I tried the following code:
Sub tabname()
Dim sheetXXX As Worksheet
XXX.Name = Worksheets("Sheet1").Range("B5").Value
End Sub
You have to set the object = to the actual worksheet to be renamed.
Sub tabname()
Dim sheetXXX As Worksheet
Set sheetXXX = ActiveWorkbook.Sheets("sheetXXX")
sheetXXX.Name = "Sheet" & Worksheets("Sheet1").Range("B5").Value
End Sub
If sheetXXX is meant to be the active worksheet you would do this.
Sub tabname()
Dim sheetXXX As Worksheet
Set sheetXXX = ActiveWorkbook.Activesheet
sheetXXX.Name = "Sheet" & Worksheets("Sheet1").Range("B5").Value
End Sub

Copy Multiple Ranges from userform to worksheet in another workbook

I am having a hard time with an issue I have with an excel workbook im trying to create that will parse multiple ranges defined in a userform from another workbook. My issue is that when i am attempting to paste the range in the form, the code will only paste the string and not the code itself.
Note: this is a mod attempt of Jan Karel Pieterse's range userform workaround
code below:
Private Sub cmbOK_Click() 'VALIDATION OF RANGE
Dim wbk As Workbook
Dim wst As Worksheet
Dim vbk As Variant
vbk = "[" & Me.cbxWorkbooks.Value & "]" & Me.refSelectCells.Value
If refSelectCells.Text <> "" Then
If TypeName(Selection) = "Range" Then
If IsValidRef(refSelectCells.Text) Then
OK = True
Windows("abc_123_Review_Automation.xlsm").Activate
Worksheets("CalculationSheet").Range("A2") = vbk.Value
End If
End If
End If
Me.Hide
End Sub
Example:
if vbk's value is "[Workbook]sheet1!a4:j365"
I want to paste the contents of that range address into a new workbook at "A2"
Please Help!
Range("A2") returns a range object.
If you want to modify the formula of the cell you should modify Range("A2").FormulaR1C1.
If you want to modify the value you should modifyRange("A2").Value.