Losing ActiveCell.Value when closing TextBox from which value copied - excel

So I'm using VBA UserForm in Excel to capture values. Problem is that after assigning value to ActiveCell and after value initially appearing in cell, The value disappears when I Close TextBox. I Think I Understand why this happens, just Don't know how to preserve value after closing TextBox. Thanks in Advance.
Paul L

Thanks astander
Thanks for responding but i figured out how to avoid problem . A solution is to avoid having to close TextBox indefinately. Doing this by using modal parameter of Show method as shown below
UserForm1.Show (vbModeless)
Not most elegant solution maybe but it works for now. Anyway I am still at Tinkering stage of working with VBA. Trying to teach myself using book and online resources.Thanks again
Paul L

Is your textbox bound to a cell? If so, just avoiud that and drop the textbox value into the cell manually.

I found the easiest way to do this is with variables. Eg
Sub textmove()
Dim textdata
textdata = txtbox.text
Range("A1").value = textdata
End Sub
Not sure if this helps, but I have always found it easier to do this in two stages (get the data, then assign the data to a cell). Im sure there is a more correct and efficient way to do it, however this has never failed me!

Related

How to insert a column in Excel using VBA?

Please help me with the code to insert a column in excel using vba. Here is what I am doing -
Sheet1.Range("A:A").EntireColumn.insert
That code works fine for me.
Sheet1.Range("A:A").Insert works also. Range("A:A") is already referencing an EntireColumn.
There are a few things to check if it's not working for you:
You're referencing an object called Sheet1. Is that definitely the codename of the worksheet you want to change? Make sure you understand the difference between sheet name and codename. You could try referencing it by the sheet's name instead: Worksheets("Tabname").Range("A:A")...
Is the worksheet protected? That would give you an error if it is.
Is there any data in the right-most column of the spreadsheet? That would also cause an error as Excel doesn't know what to do with it. If you're not 100% sure, select the entire right-most column of the sheet and hit delete to remove anything that might cause an issue.
Lastly, can you insert a column manually? i.e. select left most column, [right-click] and [Insert]?
I think you got the Sheets property wrong, this works for me :
Sheets(1).Range("A:A").EntireColumn.Insert
You should clearly mention what you are trying to do; what type of problem you are facing.
However, you should try out the below code. hope this will help
Sheet1.Range("A:A").EntireColumn.Insert Shift:=xlToRight
while inserting a new row or column make sure to refer to the entire row or column. Check if there are any marge cells at the right of column A. That can possibly be causing the problem.
Sub TryMe()
Columns(1).Insert
End Sub
Also, this is a very generic question. If you can Google something and figure it out in a fraction of the time that it takes to craft a question and post it on SO, just Google it. If your question is very unique, and after hitting multiple dead ends using Google, then ask the SO community. I found the 3 links below using a Google search that took around 1 second.
https://www.automateexcel.com/vba/insert-row-column/
https://excelchamps.com/vba/insert-column/
https://www.educba.com/vba-insert-column/

How to bring top a specific textbox

I have a userform that contains a lot of objects.I am trying to widen some of the textboxs depending on text lenght and upon exit return textbox to it's original size.My problem is when i change the width,textbox next to it stays on front.I tried to find something on object properties that would help but nothing worked.Thanks for the help beforehand.
You need to change the Z order:
Textbox1.ZOrder msoBringToFront
for example.

VBA UserForm Dynamic Resize Event with Multipages

Good Morning!
I started off by reading this :
http://www.andypope.info/vba/resizeform.htm
And while this was very informative, I was wondering if someone one would be able to point me in the right direction to help being able to solve my inquiry. I want to dynamically set height & width values of a userform depending on what multipage selected (presumably by click event). Would it be something like this?
Sub pageX_click
height.value = 23
width.value = 50
End Sub
I assume it might be more complicated than that, but if someone would be willing to point me in the right direction I can tinker till i find the correct solution.
Other question- due to the differing sizes, would I need to statically set which pages is opened each time? That way i dont get random size issues/errors?
Here is the solution- it was literally as simple as I thought
Private Sub MultiPage1_Change()
If MultiPage1.Value = 0 Then
ToolBoxForm.Height = 560.25
ToolBoxForm.Width = 652.5
End If
'lather, rinse, repeat for each page
End Sub

Simple vba program in Excel

Sub TEST()
If cells(i, "R").Value <> "UK" Then
cells(i, "R").Interior.ColorIndex = 3
End If
End Sub
If I run this program it throws application defined error \
I am new to Excel (beginner)
How to correct this error!!!
Thanks In advance
I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.
This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!
First you have to define i variable
for example: Dim i as variant

VBA Excel dataform not displaying the right input and fields

I have the following VBA code, which should show a dataform from another hidden sheet.
Sub CoverageBssEntry()
Application.ScreenUpdating = False
Sheets("myhiddensheet").Select
Range("myTable[#All]").Select
ActiveSheet.ShowDataForm
End Sub
When I run this, the data form does not containt the labels and inputboxes of this table.
Any help is really appreciated, because it is driving me nuts! My only other option is to spend time to build custom made user forms, while this would do perfectly.
You cannot select a hidden sheet. And anyway the .Select statements are not necessary
Try
Sheets("myhiddensheet").ShowDataForm
The fix is to use:
ActiveSheet.Cells(x,y).Select
prior calling the .ShowDataForm, works like a jiffy!
I think there are genuine constraints with the ShowDataForm command. It works fine if invoked outside of VBA while in a specific range. But once coded into VBA, it will only return the form for the first table in the referenced worksheet, even if a macro was recorded to perform that action.
I cannot tell why. Maybe because the showdataform event is tied to the worksheet and not to the table or list selected when it is called. Sorry guys. Maybe microsoft will improve this in the future.

Resources