Macro to Create combobox in Excel anywhere - excel

I want to create a Macro that allows comboboxes to be placed in every cell that I select. I am able to create a Macro places the combobox in the same cell, where I record it. It gives it a range, like "N15", so every time I execute the macro, the comboboxes stack up in the same cell. Since i am new in VB, i am requesting if someone can tell me the trick to place it in the active cell.
I have a reference cell link. I found a helpful person in one of the forums, who has supplied a code to "refresh" the links, when you copy-paste. It would be nice to just combine this along with the combobox.
When I add ActiveCell, it gives me an error: "Object doesn't support this property or method".
This the code the macro of a simple copy-paste creates:
Sub AAA()
'
' AAA Macro
'
'
Selection.Copy
ActiveCell.Offset(-3, 1).Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Shapes.Range(Array("ComboBox4")).Select
Application.Run "ExcelTemplate_DayOne.xlsm!Sheet1.Test"
End Sub
The Range("A1") is what will ensure that every time i run the macro, the combobox will go and get added to that same cell. If I change Activesheet.Paste to ActiveCell.Paste, it gives me the above error, I mentioned.

To use the active cell, use ActiveCell
For example, to get the row of the active cell, use : ActiveCell.Row
To get the column : ActiveCell.Column
To put something in that very cell you could either do :
ActiveCell = "SOMETHING!"
OR :
Cells( ActiveCell.Row, ActiveCell.Columns ) = "SOMETHING TOO !"

Related

Assigned macro to control button that works on its own worksheet

I have several worksheets that have a Control Button that is assigned the same macro. I need the macro to only work on the worksheet that it is on. It is currently sending me back to the first worksheet when I click on these buttons, regardless of which worksheet I am on. I am new to VBA so I am having a hard time understanding all of the macro, but mainly I don't understand the Application.Goto line and think that is where my trouble is. This code is written by someone else, but I am only trying to reverse-engineer / fix it for my own use. I have no idea why it is using the reference of "FirstTime". It is not one of the names of the worksheets or excel filename.
Sub Break()
Application.Goto Reference:="FirstTime"
Do Until ActiveCell = ""
If ActiveCell <> "" Then
ActiveCell.Offset(1, 0).Select
End If
Loop
If ActiveCell.Offset(0, -1) = "" Then
ActiveCell.Offset(0, -4).Select
ActiveCell.FormulaR1C1 = InputBox("Enter client engagement")
ActiveCell.Offset(0, 3).Select
ActiveCell.FormulaR1C1 = Now()
Else
ActiveCell.FormulaR1C1 = Now()
ActiveCell.Offset(0, -2).Select
ActiveCell.FormulaR1C1 = InputBox("Work performed/tasks completed")
End If
Range("A1").Select
End Sub
I've tried using other strings or removing any Reference from Application.GoTo, but it only gives me a 1004 error. It also seems to need this line, otherwise it also causes an error. The rest of the code/macro works as intended as far as I can, it's just a matter of going to the wrong worksheet when the Control Button is clicked.
I think you exactly understood your own problem by yourself.
When you execute Application.Goto Reference:="FirstTime", it goes to the range that was named FirstTime, which most likely in what you call "the first worksheet".
On the left of the formula bar, as shown in the screenshow below, you have a dropdown, you can use it to check the name.
If I am right so far, follow this procedure:
In the sheet where you want your macro to apply, select the range you want to call FirstTime.This range should most likely cover as many rows/columns as the one you should have spotted with my instructions above (it may even be the very same address).
Go to menu Formula > Name manager.
Click New.
Fill the window with:
Name = FirstTime
Scope = The worksheet you are currently on.
Refers to = The range you have selected (the worksheet on that range is expected to be the same as for field Scope).
This will make it so the new name you are defining has higher priority when executing Application.Goto.

Make it easy to give cells names based on a value in a cell

I am looking for a way to name Cells based on values in another cell. I've tried to record a macro to do this but just does not work out. I've got a way to get the cells and needed names into an array like this (note that there MANY more field than shown below, I have several worksheets to do this on, and all the field names are not the same as below):
....B...........................................C..............................................D
Field Label .................Cell Address...............................New Cell Name
First Name...........................I3.......................................FirstName
Last Name...........................L3.......................................LastName
Address 1.............................I5.......................................Address1
Address 2.............................I6.......................................Address2
City.......................................I7.......................................City
State.....................................I8.......................................State
When I try to record a macro (and I've tried it several different ways), where I select and copy the desired cell name and paste it into the name Box in excel, I get the code below. It does not work in the end, plus it gets specific with the cell names. I need it to be general so I can apply it to any array in any worksheet.
Sub nameFields()
'
' nameFields Macro
'
Selection.copy
Range("I3").Select
Application.Goto Reference:="FirstName"
Range("D3").Select
Application.CutCopyMode = False
Selection.copy
Range("L3").Select
Application.Goto Reference:="LastName"
Range("D4").Select
Application.CutCopyMode = False
Selection.copy
Range("I5").Select
Application.CutCopyMode = False
ActiveWorkbook.Names.Add Name:="Address1", RefersToR1C1:="=Sheet1!R5C9"
End Sub
(I have also looked for a way to do it right in excel, with something like naming the first cell in the array and then dragging down to cell to auto fill in the named ranges (like Field1, Field2, etc), but there does not seem to a way to do that either).
Any suggestions? Obviously, I am not a VBA wizzard. I'd go ahead and just trudge through it manually, but there are lots of fields and lots of workbooks. Plus, I'm thinking I can combine an answer from this into a side project about making an easily customizable entry form in excel. anyway, thanks in advance.

Check a condition and bring a value from all the sheets of a file

I´ve a file with about 50 tabs, and each with a variable name.
Each tab includes a standard form (15 columns, 1000 or more rows).
I need to check each row:
If column D has a non-null value and column B is greater than some other value, I need to bring the value of column A in a different tab.
I´ve to admit I thought this would be simple but I can´t find a way to make it work.
Any advise would be much appreciated!
Based on the detail you've provided, my understanding is that in every worksheet you want to bring forward some value from a base tab. Thus, the following formula would work:
=IF(D2>0,IF(B2>100,Sheet2!A2,"2nd False"),"1st False")
As you copy this down across rows, it will fetch the corresponding value on the same row in another sheet, which as far as I can see is what you're after. If it isn't, then yes you would need to bring in a vlookup.
In terms of automation, either highlight all your tabs or record a macro with the following steps:
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
Range("E2").Select
Selection.Copy
ActiveSheet.Next.Select
Range("E2").Select
ActiveSheet.Paste
Selection.AutoFill Destination:=Range("L2:L288")
Range("E2:E288").Select
Range("E2").Select
Application.Run "'Your File.xlsx'!Macro2"
End Sub
Hope that is what you're after and good luck

how to highlight top row in every excel sheet using Vbscript?

I have a Vbscript code that generates multiple sheets in a new excel file. But automatically the last entry of each sheet is highlighted i.e. control resides with the last row. So for every sheet, one has to scroll all the way up. I don't actually want to highlight, I just need the control at the top. How can I set the control in each sheet to the first entry/row? Thanks for your time.
You can adapt this VBA sub to your needs:
Sub JumpAndPosition()
For i = 1 To Worksheets.Count
Worksheets(i).Activate
Range("A1").Select
Application.Goto Reference:=ActiveCell.Address(ReferenceStyle:=xlR1C1), Scroll:=True
Next i
End Sub
' Assuming your Excel object variable is named 'objExcel'...
Dim Sheet
For Each Sheet In objExcel.ActiveWorkbook.Sheets
Sheet.Cells(1, 1).Select
Next
You are using Excel and there is no reason to ask anyone these types of questions because you can record it in Excel.
Alt + T, M, R
then Home key then Up Arrow. Stop recording.
Gee look what Excel wrote
Selection.End(xlUp).Select
or if you had of recorded Go To dialog
Application.Goto Reference:="R1C1"
or if you had of recorded Ctrl + Home
Range("A1").Select

Excel VBA: Replace Range('Cell).Select with an If Statement

I am using Excel Visual Basic for Applications (VBA)
I have a Macro that is selecting a set of cells, cutting them and pasting them into a defined cell in another sheet that the user selects. However, I need to have the macro look for empty rows (represented as a 0) in the user-chosen spreadsheet, as data may already be in the spot where it is getting pasted.
My Code:
Sub Button11_Click()
'
' Button11_Click Macro
'
'
Range("A2:K2").Select
Selection.Cut
Sheets(Application.InputBox("Type desired Team")).Select
Range("B4").Select
Sheets("ImporterSheet").Select
End Sub
Basically,
Range(B4).Select
needs to be replaced with
Range(If ActiveCell <> 0 then ActiveCell.Offset(1,0). If ActiveCell = 0 then paste data here)
Try this:
Range(Range("B:B").Find(0, [B1]).Address).Select
It selects the first cell with 0 in it in Column B.
Is this what you need?
BTW, try going through THIS which is definitely a good read to improve your coding.

Resources