Get Checkbox location and name? - excel

Is it possible to have VBA return the location of a checkbox, the linked cell, and the name of the checkbox?
I am building a spreadsheet that has 100's of check boxes. It is crazy linking all these cells, then having to have helper cells to fill data in a report. What I am thinking is having VBA return the location, linked cell, and checkbox name. If I can get those, I can automate most of this. But I am hoping to have one macro work on all checkboxes. Kind of an on change type of command and if that change is a checkbox being marked then it would pull that data and write it where it needs to be.
So in turn, I get rid of the helper cells, and Only have to name the checkbox. That name would be the value I want filled.
I can modify code and work with macros, but not sure how to get the data. Any help is appreciated, or if there's a post somewhere that would help, please point me in the right direction.

Yes, it is possible.
This is an extremely basic bit of code that gets them for you, what you do with that is up to you.
Public Sub PrintCheckboxes()
Dim objShape As Shape
For Each objShape In Sheet1.Shapes
With objShape
If .FormControlType = xlCheckBox Then
Debug.Print "Checkbox Name = " & .Name & ", Title = " & .AlternativeText & ", Linked Cell = " & .ControlFormat.LinkedCell & ", Top Left Cell = " & .TopLeftCell.Address
End If
End With
Next
End Sub
Adapt accordingly.

Related

Select method of Range class failed. A range address is found by msgBox, but won't ".Select"

I am working with named ranges (mostly in Sheet 2) and am in the process of creating a summary in a sheet named "Signups". I am mostly cutting and pasting code that has worked in other parts of the program. For some reason VBA will not Select a range in Sheet 2.
In the code below, "SignupPairs" is a range in sheet "Signups" and "NewMem" is a range in "Sheet2".
''''''''''
MsgBox "SignupPairs 2,1 " & Range("SignupPairs")(2, 1)
Range("SignupPairs")(2, 1).Select
MsgBox "NewMem 1,1 " & Range("NewMem")(1, 1)
'Sheets("Sheet2").Range("NewMem")(1, 1).Select 'Doesn't work.
Range("NewMem")(1, 1).Select
''''''''''
The first four lines have been added for debugging purposes. The last line is the one that brought me to a screeching halt. Line four was an attempt to be specific about the sheet I wanted to use; it didn't work. MsgBox correctly reports the contents of cells in "SignupPairs" and "NewMem" but I can't select NewMem(1,1) in order to do a "Range(Selection, Selection.End(xlDown)).Rows.Count.
For bonus points: Does anyone have a link, or a reference, for a good primer or manual on VBA so I can educate myself and not have to ask these simple questions? VBA for Dummies only got me into this mess.
P.S. Clicking "F1" on "Select" in VBA Editor sends me to a page on Select Case which seems like a dead end.
Since you are new to VBA, I highly recommend always writing down what you intend to do as comments in your code. This will help helpers and future you.
I am not sure what you want to do with the data in your summary sheet. I recommend that you create a reference to your summary sheet and then you can use arrays from your named ranges.
Here's a way that you can get both Sign Up Pairs and New Mem into arrays and then msg box out what you were looking at (but I assume that you want to match them or do something else)
Sub doSomethingWithSignups()
Dim ws As Worksheet, signUps As Variant, newMems As Variant
' these arrays will have whatever data you have identified in the Named Ranges
signUps = ThisWorkbook.Names("SignupPairs").RefersToRange.Value2
newMems = ThisWorkbook.Names("NewMems").RefersToRange.Value2
' you can do anything with these arrays now
MsgBox("SignupPairs 2,1 " & signUps(2, 1))
MsgBox("NewMem 1,1 " & newMems(1, 1))
End Sub

How do I create a link in Excel, with a formula in it, to another Workbook?

I'm not sure if this is even possible in Excel but this is what I need to do:
I have a column with a list of hotels and then another column which needs to pull data from each individual hotel's excel file. For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:
='G:\Hotels\Paris\Paris - Monthly\[Paris_summary_2018.xlsm]Feb'!$CD$89
I have lots of hotels I need to do this for in different sheets. So I need the link to dynamically refer to whatever hotel is in column A and the title of the sheet, for example could I do something like this?
=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89
where A2 has the string "Paris". Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title
I am also open to using VBA for this.
As long as you don't mind using VBA, you can easily generate the links with something like this:
Sub generate_hotel_links()
Dim r As Range, c As Range
Dim s As String
' This is the range which all the hotel-locations are in
Set r = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
On Error Resume Next
For Each c In r
' Generate the formula based on the current cell we are in
s = "=" & Chr(39) & "G:\Hotels\" & CStr(c) & "\" & CStr(c) & " - Monthly\[" & CStr(c) & "_summary_2018.xlsm]Feb" & Chr(39) & "!$CD$89"
' ...and put it in the neighbouring cell
c.Offset(0, 1).Formula = s
Next c
On Error Goto 0
End Sub
On Error Resume Next will make the macro continue no matter what error pops up - the ideal case would be some more robust error handling, or a check for if the workbook / sheet actually exists before attempting to write the formula, but I'll leave it to you to attempt to write this if you feel the need improve the macro.
If you want to just use generic Excel formulas, I'd advice having a look at the question and answers I posted in the comments to your question.
I am not sure if you can use HYPERLINK.
Like this:
=HYPERLINK("G:\Hotels\"&A2&"\"&A2&" - Monthly\["&A2&"_summary_2018.xlsm]Feb!"&$CD$89)

VBA fills whole table instead of defined cell

I ran into a crazy problem that I don't understand and for which I did not yet find a workaround.
First I have an excel macro filling various sheets on a workbook depending on information from other workbooks. I want to include a hyperlink to a website, containing the value in a specified cell in one workbook, while only displaying the value. I try to insert the formula as following
=HYPERLINK("https://example.com/value","value")
This hyperlink is only available for a certain cell, so it should only appear where a condition is true.
Now for the crazy part: If I fill the cell where I want to insert the hyperlink with a normal string or int, it works just fine and VBA only fills the cell I specified. When I insert a Hyperlink, it fills up the whole column on my worksheet, although everything else is kept in the same way.
Please find a snippet of the code below:
RequestID = Workbook1.ActiveSheet.Cells(i, 1).Text 'Request number
Workbook2.Sheets("Sheet1").Cells(y, 4) = RequestID
This only fills cell(y,4) with the correct request number as expected
Workbook2.Sheets("Sheet1").Cells(y, 4) = "RequestID"
This only fills cell(y,4) with the String "RequestID" as expected
Workbook2.Sheets("Sheet1").Cells(y, 4) = "=HYPERLINK(" & Chr(34) &"https://example.com/" & RequestID & Chr(34) & "," & Chr(34) & RequestID & Chr(34) & ")"
This fills up my whole table except for the header with the correctly working hyperlink.
I dont really understand why and I can't think of any possible problem. Maybe someone here has a hint for me, or knows a workaround.
It worked as expected for me when using a normal cell. But when I tried it in a Formatted Table (aka ListObject) then it fill the entire column, because it is a formula and not a value. If this is the case then try setting
Application.AutoCorrect.AutoFillFormulasInLists = False
before you put the Hyperlink in the cell (and then set it back to True)

Use cell value/contents to determine an external reference file or sheet name?

I'm working on a cell formula that returns (via VLOOKUP?) a cell in a closed workbook (thus I believe INDIRECT will not work). The issue is that I want to use a value in the active sheet to determine the name of the sheet in the reference workbook and can't figure it out. Here's the best I've got.
=VLOOKUP($A3,'[Other Workbook.xlsm]ObsDFW'!$1:$800,COLUMNS($D4:D4)+3)
ObsDAL is the name of one of the sheets in the "Other Workbook". What I can't figure out is how to keep the "Obs" part of that name constant, but take the "DFW" from a cell value.
Using bad code, I want it to be:
=...[Master Statistics.xlsm]("Obs" & A1)'!$1:$800...
If context is helpful, the "Other Workbook" is full of hourly weather observations, separated into one sheet for each of a series of airports. I'm trying to pull this info into another file/workbook so I don't have to specify each airport specifically in the code many times over.
Thanks in advance!
You could try this VBA approach. This way you are adjusting Vlookup formula based on your dynamic_part (sheetname)
Sub VlookupClosedWorkbook()
Dim dynamic_part As Variant
dynamic_part = Range("B1").Value 'You should enter in cell B1 dynamic part of sheet name
For x = 3 To Range("A" & Rows.Count).End(xlUp).Row
Range("B" & x).Value = "=VLOOKUP(A" & x & ",'[Other Workbook.xlsm]Obs" & dynamic_part & "'!$1:$800,COLUMNS($D4:D4)+3,FALSE)"
Next x
End Sub
Assign this macro to shape and fire it after you change your dynamic part. When you trigger it for the first time, make sure that you have both Workbooks open.

Visual Basic Excel create Checkbox in Code

I've added a button to an Excel file which, when clicked, reads a text file and populates a column with lines from a text file. I need to add a checkbox to cells adjacent to certain lines, depending on what the line contains.
Can I create components like checkboxes in code, and if so, how?
Any responses are appreciated.
While the link #Siva provided is certainly valid I just prefer to have an answer on StackOverflow instead of an external link. Hence, here is the solution you might be looking for:
Option Explicit
Public Sub tmpSO()
Dim i As Long
Dim chk As CheckBox
With ThisWorkbook.Worksheets(1)
.CheckBoxes.Delete
For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
If .Cells(i, "A").Value2 = "need checkbox" Then
Set chk = .CheckBoxes.Add(Left:=.Cells(i, "B").Left, Top:=.Cells(i, "B").Top, Width:=.Cells(i, "B").Width, Height:=10)
chk.OnAction = "runThisSub"
chk.Name = "CheckBowInRow" & i
chk.Caption = "CheckBowInRow" & i
End If
Next i
End With
End Sub
Sub runThisSub()
MsgBox "You clicked the checkbox " & Application.Caller _
& Chr(10) & "in cell " & ThisWorkbook.Worksheets(1).CheckBoxes(Application.Caller).TopLeftCell.Address
End Sub
Copy both subs into a Module into your Excel file and change in the first sub
the sheet where the text is imported to (here it is Worksheet(1)),
the column where the condition can be found (here column A), and also
what the condition is (here the value in column A must be need checkbox).
The code will now look through all cells in column A in sheet Worksheet(1) and check if the value is need checkbox. If so, the code will automatically add a checkbox into column B adjacent to that cell.
If you click on any of the newly created checkboxes then the second sub fires up and will show you in a message box which checkbox in which row has been clicked.

Resources