I would like to make an autofit to the referenced cells from another sheet, so everytime when I add some infos on one sheet my row height expands, the code below makes that possible:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.WrapText = True
Target.EntireRow.AutoFit
End Sub
But as I say this is possible only if I manually typing something anywhere in my workbook, but what about when I have on another sheet (same workbook) some functions that are referencing those values? the fields are not expanding even though it is a right match...
For instance I am having this infos in one sheet (wrapped) as one value:
Column A
AAABBBCCCDDD
EEEFFFGGGHHH
and when using this function on another sheet to make them referenced:
=IFNA(IF(ISBLANK(INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"";INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"")
it gives me everything in one row but not expanding or wrapped as I want:
Column A
AAABBBCCCDDDEEEFFFGGGHHH
It has to be automatically wrapped for the sake of dynamic document that I am providing, without loss of infos or that some infos are missing or "hidden".
You can use Workbook_SheetCalculate event. A simple example will be like below.
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
With Worksheets("Sheet2")
.Range("D2:D5").WrapText = True
.Range("D2:D5").EntireRow.AutoFit
End With
End Sub
So, if you have any formula to range D2:D5 on sheet2 and when you will change any value to any sheet of workbook and if it changes any value to D2:D5 then that cell row will automatically fit.
Edit: For full sheet you can try below codes.
With Worksheets("Sheet2")
.Cells.WrapText = True
.Cells.EntireRow.AutoFit
End With
Microsoft reference here
Related
I added a simple Previous Sheet macro to file and it worked normally, until it started to return #NAME error, and i can't figure out why.
I meant to use it with named single cell ranges which are consistent across the workbook, I'd used it successfully before naming the ranges and didn't think it would have any impact on the fuction. After naming the ranges though it no longer works, not even for regular non named ranges.
I have tested this by creating a new workbook, filling some sheets and trying it out, and it still returns a #NAME error. When i evaluate the function, the error appears at the very first step: recognizing the function. However, when i type into the formula bar, the programs offers me the formula normally.
I have also tried referring to the named cells by its cell, and even adding the worksheet name before the cell (eg "prevsheet(previoussheetname!a1), or prevsheet(thissheetname!a1)). I have even, in a last ditch effort, tried adding double quotes before the cell name.
For full disclosure, i have also another macro subroutine that uses references to previous and next sheets, but as it wouldnt recognize the function itself (which should have been an early sign), it makes use of relative referencing (ie activesheet(index - 1, activesheet(index + 1)). At the time i didn't think it would mess up the function, but as i grow ever more desperate and confused, maybe thats a possibility.
the PrevSheet() code i was using:
Function PrevSheet(RCell As Range)
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
If xIndex > 1 Then _
PrevSheet = Worksheets(xIndex - 1).Range(RCell.Address)
End Function
And as it is now, as suggested by Chris Neilsen
Function PrevSheet(RCell As Range) As Variant
Application.Volatile
PrevSheet = RCell.Worksheet.Previous.Range(RCell.Address).Value
End Function
As suggested by Chris Neilsen i have edited the named ranges to look like this:
!(nothing)$column$row with its scope set to Workbook
The named range is not available at the range browser.
Only cell B1 is named. It is called "name"
PrevSheet() does not work with either range.
Macros are enabled
Anyone with a better understanding of vba, macros and excel can tell me why this is happening and how do i fix it so it returns the value of the specified cell in the first sheet to the left of sheet the function is typed in? (ie, in sheet4, =prevsheet(A1) will return the value of cell A1 in sheet3)
I hope my question is clearer now!
Your code appears to work if it is placed in a Standard Module:
Public Function PrevSheet(RCell As Range) As Variant
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
MsgBox xIndex
If xIndex > 1 Then
PrevSheet = Worksheets(xIndex - 1).Range(RCell.Address)
End If
End Function
For example, in the worksheet:
I have assigned cell A7 the Name junk and the 666 is the value in the previous sheet's cell A7.
This will work, if you define your Named Ranges correctly. There are several ways this can be done, but here's one that is IMO simplest.
Since you say ...use it with named single cell ranges which are consistent across the workbook. you can create a single Named Range, Workbook scope, that will refer to a cell (or cells) on the sheet which references the name.
Lets say you want to refer to cell A1. In Name Manager, create a Name, lets say YourNamedRange workbook scope, Reference =!$A$1 (Note the ! without a sheet reference).
When you add a formula to a sheet (eg =YourNamedRange) it will refer to cell A1 on the sheet containing the formula.
Applying it to your UDF, just use =PrevSheet(YourNamedRange)
Your UDF works (mostly) as is, but will fail if a different Workbook is active. To fix that, use
Function PrevSheet(RCell As Range)
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
If xIndex > 1 Then
With RCell.Worksheet.Parent 'The workbook containing RCell
PrevSheet = .Worksheets(xIndex - 1).Range(RCell.Address)
End With
End If
End Function
There is also a WorksheetProperty called Previous that does much the same thing, so you can refactor as
Function PrevSheet(RCell As Range) As Variant
Application.Volatile
PrevSheet = RCell.Worksheet.Previous.Range(RCell.Address).Value
End Function
on one Excel sheet I have a combobox.
On another sheet, I have a table with a named column ("KontoNr") which should feed into the combobox. The table and column are named in the name manager and are shown as =tabKontenplan and KontoNr =tabKontenplan[KontoNr].
Now I am unsuccessfully trying to fill the comboxbox like this:
combobox.listfillrange = "=tabKontenplan![KontoNr]"
And
combobox.listfillrange = "=KontoNr"
also does not work.
There ist no error, the combobox just remains empty... why is that?
I added an empty ActiveX ListBox control on a worksheet, then typed a couple of random values in a Range on that worksheet, named that range VALUES, and then added this code in the worksheet's code-behind:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ListBox1.ListFillRange = "VALUES"
End Sub
And that's all I needed to do. Remove the = in front of your named range, and it "just works". Make sure the defined name is in scope, and all will go well.
If the named range is scoped to the other worksheet, it won't work. Delete the name and re-create it in workbook scope.
Assuming that "another sheet" is Sheet2,
no good: combobox.listfillrange = "=" & Sheet2.Range("KontoNr").Address
A corrected answer:
combobox.listfillrange = "=Sheet2!" & Sheet2.Range("KontoNr").Address
I tried looking at other similiar questions and solutions but as an Excel beginner I couldn't quite figure it out.
So I have the following macro:
Sub Worksheet_Change(ByVal Target As Range)
Dim wsNew As Worksheet
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("B46:B99")) Is Nothing Then
ThisWorkbook.Sheets("LT").Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
End Sub
It opens a new sheet in the same workbook and I'd need to auto populate certain cells with data from the main sheet. Main sheet: http://i.imgur.com/RJe44hQ.jpg new sheet: http://i.imgur.com/eatbg6j.jpg . The cells I need copied are in red.
Thanks in advance for any help! Really new to all this..
Since you don't specify which value(s) you need to pull from the main sheet I can't get too specific, but in general there are three approaches to take.
1) If the data is in contiguous range(s) of cells on both sheets, you can just copy the data from the main sheet after creating the new sheet, and then paste the values to the correct target range
2) If the data isn't contiguous on both sheets, then your next best option would be to have the value for each target cell set based on the value of the corresponding cell on the main page. Ex: To set A2 on Sheet2 to the value of B4 on Sheet1 you would use Worksheets("Sheet2").Range("A2").value = Worksheets("Sheet1").Range("B4").value
3) This one also works if the data isn't contiguous, but gets to be troublesome if there are more than ~5 values to copy. You can create an appropriate variable (string for text, long/int for numbers, etc.), set that before creating the new sheet, and then use them to set the appropriate cells once the new sheet is created.
I am using Excel VBA to make an simple app, I have a CELL with Data Validation make it a "ComboBox", can I use VBA code to set the selectedIndex for it? and how?
If I understood correctly and you need to "put some value into CELL when worksheet with CELL on it is opened" then try to insert following code into ThisWorkbook module:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "%%SheetWithCELL%%" Then
Sh.Range("CELL").Value = "%%NeededValue%%"
End If
End Sub
Where
%%SheetWithCELL%% - Name of sheet where CELL is located;
%%NeededValue%% - Value, which you need to insert into CELL.
P.S. Code assumes that "CELL" is the actual name of some cell (named range)
Function bd()
Sheets("sheet1").Range("A1").value=1
End Function
This is my function.
Why is it that when i enter =bd() into any cell in sheet1, the data in A1 does not change to 1? I do not want to use the button to change the value.
Why it does not work:
If you're calling your function from an Excel formula, your function becomes a User-Defined-Function (=UDF).
UDF have to follow special rules - the main one being that it cannot by any means change anything in Excel - it shall only return a value! Your function clearly violates this rules.
(For reference: the other main restriction is that it shall also not access any data outside the parameters that were passed to it when calling the function. Thus, you cannot use MyUDF=Range("A1").Value - but only something like MyUDF=rngParam1.Value*2!
Alternative approach:
If you want to change the worksheet but don't want to use a button, you need to think of some kind of trigger event. Look at the possible Worksheet and workbook events - you'll find a list of events here - and detailed instruction how to use them here.
For instance, you could use the Activate of Sheet1. For this place this code in the Sheet1 code module:
Private Sub Worksheet_Activate()
Range("A1").Value = 1
End Sub
Now every time that sheet1 gets activated, A1 will be reset!
Try This
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Any Sheet and press enter the cell A1 of Sheet one will Change to 1
OR
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sheets("sheet1").Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Sheet one and press enter the cell A1 of Sheet one will Auto Change to 1
Hope this works